I need to return multiple fields from dynamic query.I am referring this link below code what I am doing so far
CREATE OR REPLACE FUNCTION get_names(ind_zone text,dst text,nmind text,ind text,implst text,rsvr text,n34b text,prjapr text)
RETURNS SETOF RECORD AS
$BODY$
DECLARE
sql text;
BEGIN
sql:='select indzone,dstr,nmindar,plno,pltar,nmalt from plotboundary where indzone='|| quote_literal(ind_zone)||' AND dstr='||quote_literal(dst)||' AND nmindar='||quote_literal(nmind)||' AND indx='|| quote_literal(ind);
IF implst IS NOT NULL THEN
sql := sql || ' AND ';
sql := sql ||' implst = ' || quote_literal(implst);
END IF;
IF rsvr IS NOT NULL THEN
sql := sql || ' AND ';
sql := sql ||' rsvr = ' || quote_literal(rsvr);
END IF;
IF n34b IS NOT NULL THEN
sql := sql || ' AND ';
sql := sql ||' n34b = ' || quote_literal(n34b);
END IF;
IF prjapr IS NOT NULL THEN
sql := sql || ' AND ';
sql := sql ||' prjapr = ' || quote_literal(prjapr);
END IF;
RETURN QUERY EXECUTE sql;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION get_names( text,
text, text, text, text, text, text, text)
OWNER TO postgres;
calling function like
select * from get_names('Tumakuru Zone','Tumakuru','Vasanthanarasapura 2nd Phase','Allotted','Yet to Start',null,null,null)
AS t(indzone text,dstr text,nmindar text,plno text,pltar text,nmalt text);
I am getting error like
ERROR: structure of query does not match function result type
DETAIL: Returned type character varying(20) does not match expected type text in column 1.
CONTEXT: PL/pgSQL function get_names(text,text,text,text,text,text,text,text) line 22 at RETURN QUERY