Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to assign values to 2 variable as below in Postgres function.

a := select col1 from tbl where ...
b := select col2 from tbl where ...

How can I assign 2 values to 2 variables in one line command?


a,b := select col1,col2 from tbl where ...
share|improve this question
up vote 18 down vote accepted

As said in "40.5.3. Executing a Query with a Single-row Result" (emphasis mine):

The result of a SQL command yielding a single row (possibly of multiple columns) can be assigned to a record variable, row-type variable, or list of scalar variables. This is done by writing the base SQL command and adding an INTO clause.

So this should work:

SELECT col1, col2 INTO a, b FROM tbl WHERE...;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.