【问题标题】:PostgreSQL, Storing multiple variables in the stored procedurePostgreSQL,在存储过程中存储多个变量
【发布时间】:2014-10-24 13:19:57
【问题描述】:

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

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

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的简单方法?

【问题讨论】:

  • 在这种情况下我更喜欢使用record 变量。

标签: sql postgresql stored-procedures postgresql-9.2


【解决方案1】:

Quote from the manual:

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

应该是这样的:

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;

【讨论】:

  • 使用record,看起来更好(恕我直言)FOR result_rec IN SELECT * FROM table_a WHERE id=1 LOOP ... END LOOP
猜你喜欢
  • 2013-07-11
  • 1970-01-01
  • 1970-01-01
  • 2013-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多