Don
Don

Reputation: 4192

How to declare a variable in PostgreSQL?

I have several queries in a query window in pgAdmin, many using the same value(s). Is there a way to declare a variable, myVar to use in query statements?

SELECT * FROM table WHERE user = myVar;

INSERT INTO table(user) VALUES (myVar);

Upvotes: 1

Views: 8892

Answers (1)

FancyXun
FancyXun

Reputation: 1298

A simple example:

DO $$
DECLARE  
   myVar VARCHAR := myValue;   
BEGIN  
   INSERT INTO table(user) VALUES (myVar);
END $$;

Upvotes: 2

Related Questions