sql - Returning results from a function in 'select statement' format -
i have function looks this:
create or replace function mffcu.test_ty_hey() returns setof record language plpgsql $function$ declare cname1 text; sql2 text; begin cname1 in select array_to_string(useme, ', ') ( select array_agg(column_name) useme from( select column_name::text information_schema.columns table_name = 'crosstab_183' , ordinal_position != 1 ) fin ) fine loop sql2 := 'select distinct array['|| cname1 ||'] mffcu.crosstab_183'; execute sql2; end loop; end; $function$
i call function this:
select mffcu.test_ty_hey()
how return results of sql2
query without creating table/temporary table?
while @pavel right, of course, convoluted function untangled to:
create or replace function mffcu.test_ty_hey() returns setof text[] language plpgsql $func$ declare cname1 text; begin cname1 in select column_name::text information_schema.columns table_name = 'crosstab_183' , table_schema = 'mffcu' , ordinal_position <> 1 loop return query execute format('select distinct array[%i::text] mffcu.crosstab_183', cname1); end loop; end $func$
format()
requires postgresql 9.1 or later. in 9.0 can substitute with:
execute 'select distinct array['|| quote_ident(cname1) ||'::text] mffcu.crosstab_183';
call:
select * from mffcu.test_ty_hey();
by casting each column text
arrive @ consistent data type can used declare return
type. compromise has made return various data types 1 function. every data type can cast text
, that's obvious common ground.
btw, have trouble imagining array
wrapper around every single value should for. suppose drop that.
Comments
Post a Comment