PostgreSQL,在存储过程中存储多个变量

时间:2014-10-24 13:19:58

标签: sql postgresql stored-procedures postgresql-9.2

如何在存储过程中存储查询中的多个变量?

对于一个变量,它可以很容易地完成,但是如果它对同一个查询不止一个怎么办?

declare num1 int;
declare num2 int;
select number1 into num1 from table_a where id = 1;

-- This one is not correct
select number1 into num1, number2 into num2 from table_a where id = 1;

有没有简单的方法可以不使用cursor variable

1 个答案:

答案 0 :(得分:4)

Quote from the manual:

  

其中target可以是记录变量,行变量或逗号分隔列表的简单变量

所以它应该是:

select number1, number2 
   into num1, num2 
from table_a where id = 1;

或者,您可以使用记录变量:

declare result_rec record;
...

select number1, number2 
    into result_rec
from table_a where id = 1;