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?

6
  • 1
    What is your full code? Commented Dec 7, 2022 at 11:50
  • That is the full code Commented Dec 7, 2022 at 11:52
  • Then that's no surprise. That segment of code alone doesn't make any sense and is not syntactically correct. Commented Dec 7, 2022 at 11:53
  • Ok great, can you show me what I'm missing please? Commented Dec 7, 2022 at 11:54
  • That is just a sample from a part of a function. Try using that in a full function code. Commented Dec 7, 2022 at 11:55

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

1

Try this syntax.

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

enter image description here

5 Comments

I'm getting the same error
Updated my answer, try that.
Please do not post code as images. See here for more details why: meta.stackoverflow.com/questions/285551
I didn't. I showed that it didn't throw his error. The code is above it.
@Raya still, an image consumes much more storage than text :) Welcome to SO.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.