postgresql - Calling set-returning function with each element in array -
i have set-returning function (srf) accepts integer argument , returns set of rows table. phone call using select * tst.mysrf(3);
, , manipulate returned value if table.
what execute on each element of array; however, when phone call using select * tst.mysrf(unnest(array[3,4]));
, error returned "set-valued function called in context cannot take set". if instead phone call using select tst.mysrf(unnest(array[3,4]));
, set of type tst.tbl
.
table definition:
drop table tst.tbl cascade; create table tst.tbl ( id serial not null, txt text not null, primary key (id) ); insert tst.tbl(txt) values ('a'), ('b'), ('c'), ('d');
function definition:
create or replace function tst.mysrf( in p_id integer ) returns setof tst.tbl language plpgsql $body$ declare begin homecoming query select id, txt tst.tbl id = p_id; end; $body$;
calls:
select * tst.mysrf(3)
returns table, expected. select tst.mysrf(unnest(array[3,4]))
returns table single column of type tst.tbl
, expected. select * tst.mysrf(unnest(array[3,4]))
returns error described above, had expected table.
to avoid "table of single column" problem, need explicitly expand srf results (row).*
notation
select (tst.mysrf(unnest(array[3,4]))).*;
if understood @depesz, lateral
provide more efficient or straightforward way accomplish same result.
postgresql plpgsql
No comments:
Post a Comment