Skip to content
Advertisement

PostgreSQL 11 – using format to assign to variable

I have been awake for well beyond my schedule and I have been stuck with this issue for a long time, I don’t even know what I am looking for to solve, but I wish to use format to insert values that I’ll be using for column names, and then executing it… but it keeps giving me errors no matter how much I try changing it :c

Heres the part that im trying to do something that doesnt work, but i think you get the idea what im trying to achieve

and heres the full code for the brave

Advertisement

Answer

As documented in the manual you need to use into together with EXECUTE to store the result into a variable. This can handle multiple columns/variables as well, so you only need a single EXECUTE to get both values.

For clarity you should reference parameters by name, not by position.

Note the $1 inside the string for format() is a parameter placeholder used when the SQL statement is executed, and will be replaced with the value of the variable specified in the USING clause.

Variable assignment is also more efficient without a SELECT:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement