plpgsql - Functions with tablename as argument in postgresql -


how create function in postgresql tablename argument , function returns result set of table passed argument query "select * table". here table argument passed function.

what want possible , utterly useless.

the function asking this:

create function selectall(tbl name) returns setof record $$ begin   return query execute format('select * %i', tbl); end;  $$ language plpgsql; 

you need set returning function (srf) because table may have multiple rows. needs return record because different tables return different sets of columns. can not use srf in select list:

test=# select selectall('student'); error:  set-valued function called in context cannot accept set context:  pl/pgsql function selectall(name) line 3 @ return query 

you can use row source, query becomes longer simple select * student. can not use so:

test=# select * selectall('student'); error:  column definition list required functions returning "record" line 1: select * selectall('student'); 

you can use it specifying alias , column definitions:

test=# select * selectall('student') t(id int, first_name text, col3 boolean, ...); 

now compare to:

test=# select * student; 

Comments