0

我使用了 EXECUTE(用于动态 sql)和 SETOF(结果作为列表返回),但这是错误的 :(

create table test as
select 1 id, 'safd' data1,'sagd' data2
union
select 2 id, 'hdfg' data1,'sdsf' data2;

create or replace function test2(a varchar) returns SETOF record as
$BODY$
declare x record;
begin
for x in execute a loop
RETURN NEXT x;
end loop;
return;
end;
$BODY$
LANGUAGE 'plpgsql' VOLATILE;

select * from test2('select * from test');
4

3 回答 3

1

您必须提前知道返回记录的结构

select * from test2('select * from test') s(a int, b text, c text);
 a |  b   |  c   
---+------+------
 1 | safd | sagd
 2 | hdfg | sdsf

或者,如果返回的集合始终是一组测试表,则使用 Akash 建议的解决方案。

于 2013-05-09T05:21:26.473 回答
1

代替

create or replace function test2(a varchar) returns SETOF RECORD as

create or replace function test2(a varchar) returns SETOF test as
                                                          ^^^^ name of table (it specifies the datatypes of the set)    
于 2013-05-09T02:27:12.293 回答
0

您需要添加一些 OUT 参数。

CREATE FUNCTION test2(a character varying, OUT id integer, OUT data1 text, OUT data2 text) RETURNS SETOF record
LANGUAGE plpgsql
AS $$
begin
RETURN QUERY EXECUTE a;
end;
$$;
于 2013-05-09T01:37:52.020 回答