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;
                          ^
0

1 Answer 1

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

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

2 Comments

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!
it's showing a error while..... DO $$ DECLARE x1 text = 'Hello'; x2 text = 'World'; BEGIN select x1; END; $$ LANGUAGE plpgsql;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.