0

Why am I getting this error?

set session my.vars.id = SELECT "Name" FROM "Client" WHERE "ID" = 1;
SELECT * FROM "Client" WHERE "Name" = current_setting('my.vars.id');
...
ERROR:  syntax error at or near "SELECT"
LINE 1: set session my.vars.id = SELECT "Name" FROM "Client" WHERE "...
                                 ^

1 Answer 1

5

Because you can only use constants in a SET command.

Try this:

SELECT set_config('my.vars.id', "Name", FALSE)
FROM "Client" WHERE "ID" = 1;
Sign up to request clarification or add additional context in comments.

7 Comments

I am trying to set a variable equal to a query's response. I then want to use that variable in many different querys. Would you mind showing me how your answer helps with this? Sorry I just don't know what set_config does
SELECT set_config('var1', (SELECT "Name" FROM "Client" WHERE "ID" = 1), TRUE); SELECT * FROM "Client" WHERE "Name" = current_setting('var1'); Is this corrent? What does the TRUE do?
It does exactly what you want - set my.vars.id to the result of your query. Please read the documentation for details. TRUE changes the value only for the duration of the current transaction.
When you say ..value only for the duration of the current transaction do you mean transaction = script? The value is changed for the current script?
No, I mean a database transaction. Did you try my answer? Does it work as you want?
|

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.