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

标签 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;

有没有不使用游标变量的简单方法?

最佳答案

Quote from the manual:

where target can be a record variable, a row variable, or a comma-separated list of simple variables

所以应该是:

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;

关于sql - PostgreSQL,在存储过程中存储多个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26548721/

相关文章:

sql - 查找表中聚合值之间的最大值

SQL Server : Combine columns abort same values

postgresql - 如何配置 postgresql 使其接受登录名+密码身份验证?

ruby-on-rails - 使用 ActiveRecord 和 Postgres 分组

SQL 存储过程

mysql - 无法创建存储过程mysql语法错误

c++ - 如何减少JSON对象?

SQL Server,其中字段是int?

java - Oracle 和 PostgreSQL 中针对 Java(Hibernate) Float 类型的正确列类型

mysql - 评估 MySQL 存储过程中的字符串