【发布时间】:2016-08-26 03:16:19
【问题描述】:
看看这个简单的 PL/pgSQL 函数:
create or replace function test() returns void()
as $$
declare
a int;
begin
select more_than_one_value into a from my_table;
-- do some other job using a
end;
$$
language plpgsql;
调用此函数时,只选择一个行,因为select into 只将一个值传递给a,尽管more_than_one_value 列有很多值。
是否有任何便捷的方法可以从表中选择多个值,然后将它们分配给变量 a ?我知道create temp table as select 是一种方法。我正在寻找替代品。
【问题讨论】:
-
您可以将它们存储在一个数组中。但是,如果您想将这些值插入到
another_table中,只需执行insert into another_table (col_1) select some_column from my_table即可,无需循环并且快得多 -
您可以在
select a,b,c,d into a1,b1,c1,d1 from my_table limit 1;定义任意数量的变量。 -
我假设
column more_than_one_value has many values你的意思是表my_table有很多行?顺便说一句,a尚未在您的示例中声明,既不是参数也不是变量。
标签: postgresql variable-assignment plpgsql