0

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?

Vinyl Warmth
  • 2,226
  • 3
  • 25
  • 50

2 Answers2

1

Try this syntax.

DO
$$
DECLARE
  x integer := 1;
  y integer := x + 1;
$$

enter image description here

Raya
  • 11
  • 1
  • 6
1

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

Cetin Basoz
  • 22,495
  • 3
  • 31
  • 39