Following is my function
created in PostgreSQL 9.2
CREATE OR REPLACE function fn_exp_settl_cr
(branchid int
,vrid int
,ddate date
,acid int
)
returns
TABLE ( trno character varying(15), trdate timestamp without time zone, netamt money, duedate
timestamp without time zone,tranid integer, acyrid integer, custrefno
charactervarying(26), orderdate timestamp without time zone) as $$
declare
sql text;
begin
execute 'select string_agg(s.qry,'' UNION ALL '') from (select ''SELECT
TrNo,TrDate,NetAmt,DueDate,TranId,AcYrId,custrefno,orderdate FROM '||t.relname||' WHERE
AdjInBill = false And ExpSettled = false And (BranchID = 1) AND (VrId = 29) and
DueDate <= ''2014-07-24'' and AcId=76 '' qry from (select relname from
pg_stat_user_tables where relname ilike ''gtab09%'') t )s ' into sql;
execute sql;
end;
$$ language plpgsql
It creates successfully but when calling this function, i.e.:
select * from fn_exp_settl_cr(1,2,'2014-07-24',4)
I get the following error:
ERROR: missing FROM-clause entry for table "t" LINE 1: ...DueDate,TranId,AcYrId,custrefno,orderdate FROM '||t.relname|...