Postgresql Postgres动态sql和列表结果

Postgresql Postgres动态sql和列表结果,postgresql,dynamic,record,Postgresql,Dynamic,Record,我使用了EXECUTE(用于动态sql)和SETOF(结果作为列表返回),但它是错误的:( 您需要添加一些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; $$; 替换 create or rep

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


您需要添加一些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;
$$;
替换

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


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

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

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

错误:返回“记录”行1的函数需要列定义列表:从测试2中选择*(“从测试中选择*);^*********错误*********错误:返回“记录”SQL状态的函数需要列定义列表:42601字符:15
create or replace function test2(a varchar) returns SETOF test as
                                                          ^^^^ name of table (it specifies the datatypes of the set)    
select * from test2('select * from test') s(a int, b text, c text);
 a |  b   |  c   
---+------+------
 1 | safd | sagd
 2 | hdfg | sdsf