I’m trying to create a function that returns a dynamic setof data type. The data types are all created before hand and can be called as the 4th variable.
CREATE OR REPLACE FUNCTION gethistoryrecord(text, text, text, text)
RETURNS setof $4 AS
$BODY$
declare
r record;
begin
for r in EXECUTE 'SELECT * FROM ' || $1 || ' where ref_id = ' || $2 || ' and create_date < ' || quote_literal($3) || '::timestamp and (archive_date is null or archive_date >= ' || quote_literal($3) || '::timestamp)' loop
return next r;
end loop;
return;
end
$BODY$
LANGUAGE plpgsql;
I will call the function like
select * from gethistoryrecord('view_all_history','3540','2012-08-21 17:43:39.855852','holder_name')
Is it in any way possible that I do not have to declare the output, and not getting the error
a column definition list is required for functions returning “record”
you can specify output record, but it is what you want probably. PostgreSQL has relative strictly typed system and isn’t possible to write too generic SRF functions.