I want to declare and assign variables on a Postgres 12.10 instance.
According to the Postgres documentation I should be able to declare and assign variables as follows
DECLARE
x integer := 1;
y integer := x + 1;
but I get the error
ERROR: syntax error at or near "integer"
LINE 2: x integer := 1;
^
SQL state: 42601
Character: 13
Can anyone see what I’m doing wrong?
2
Answers
Try this syntax.
That code from documentation was only a sample showing you that you could use the value of a former declared value in a latter variable assignment and not meant to be run alone. You should use that in a function. ie:
Check this DBFiddle demo