0

I used EXECUTE(for dynamic sql) and SETOF(result is returning as list), but it is the wrong :(

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');
1
  • ERROR: a column definition list is required for functions returning "record" LINE 1: select * from test2('select * from test'); ^ ********** Error ********** ERROR: a column definition list is required for functions returning "record" SQL state: 42601 Character: 15 Commented May 9, 2013 at 1:24

3 Answers 3

1

You will have to know in advance the structure of the returned record

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

Or if the returned set will always be a set of the test table then use the Akash's proposed solution.

Sign up to request clarification or add additional context in comments.

Comments

1

replace

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

with

create or replace function test2(a varchar) returns SETOF test as
                                                          ^^^^ name of table (it specifies the datatypes of the set)    

Comments

0

You need to add some OUT params.

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;
$$;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.