0

I try this:

myvar := select "name" from "mytable" where "id" = 15;

But it causes an error. How can I assign the variable correctly?

3
  • 1
    select "name" into myvar from "mytable" where "id" = 15; Commented Dec 23, 2016 at 10:58
  • Thanks, it worked. Commented Dec 23, 2016 at 11:00
  • postgresql.org/docs/current/static/… Commented Dec 23, 2016 at 11:03

2 Answers 2

1

select "name" into myvar from "mytable" where "id" = 15;

or

myvar := (select "name" from "mytable" where "id" = 15);

Sign up to request clarification or add additional context in comments.

Comments

1

It will work when you are using the INTO Keyword like

select "name" into myvar from "mytable" where "id" = 15;

But its possible that this returns more than one row. (Throws an Error in Oracle-SQL, cant speak for Postgres)

I think somithing like

select "name" into myvar from "mytable" where "id" = 15 limit 1;

would be better to fit your question title.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.