pgAdmin Create Variable

Viewed 2601

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;
                          ^
1 Answers

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!".

Related