2

I am trying to create a simple variable in pgAdmin (PostgreSQL) but it is not working. Can someone help me out please? I am probably just doing something dumb.

DECLARE @Variable;
...error
ERROR:  syntax error at or near "@"
LINE 88:  DECLARE @Variable;
                  ^

DECLARE @Variable text;
...error
ERROR:  syntax error at or near "@"
LINE 88:  DECLARE @Variable text;
                  ^

DECLARE Variable;
...error
ERROR:  syntax error at or near ";"
LINE 88:  DECLARE Variable;
                          ^
Freddy Bonda
  • 1,189
  • 6
  • 15
  • 33

1 Answers1

4

Looks like you're trying to use SQL Server syntax in Postgres. That won't work.

Don't prepend @ to the variable name. And you have to declare all in one DECLARE block preceding the BEGIN ... END; block.

For example:

DO
$$
DECLARE
  x1 text = 'Hello';
  x2 text = 'World';
BEGIN
  RAISE NOTICE '%', x1 || ' ' || x2 || '!';
END;
$$
LANGUAGE plpgsql;

That raises a notice with the text of "Hello World!".

sticky bit
  • 36,626
  • 12
  • 31
  • 42
  • 1
    This really really sucks. I mean I just want to create one simple variable to use in my other calls. And now I need to do all this...but thank you :) Appreciate it! – Freddy Bonda Jan 16 '19 at 13:04
  • it's showing a error while..... DO $$ DECLARE x1 text = 'Hello'; x2 text = 'World'; BEGIN select x1; END; $$ LANGUAGE plpgsql; – Rejwanul Reja Jun 09 '20 at 10:20