skip to Main Content

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


  1. Try this syntax.

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

    enter image description here

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search