return setof record from function with dynamic query

From: Toby Tremayne <tobyt(at)toll(dot)com(dot)au>
To: pgsql-general(at)postgresql(dot)org
Subject: return setof record from function with dynamic query
Date: 2002-12-18 06:18:56
Message-ID: 867309281.20021218171856@toll.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

-----BEGIN PGP SIGNED MESSAGE-----
Hash: MD5

Hi all,

I've been beating my head against the desk for days on this, and
I've been completely unable to find docs on it. I have a function
(code below) that creates and executes a dynamic query. I want to
pass the results of that query as a recordset to the caller - I can
do it as a refcursor (but via odbc a refcursor just appears as an
empty recordset, no use at all.), but when I use the function code
below I get the error:

ERROR: A column definition list is required for functions returning RECORD

I'd really appreciate it if someone could show me how to pass back
a set of records please???

Toby

CREATE FUNCTION poptions (INTEGER)
RETURNS setof record AS '
DECLARE
-- parameters
p_author_id ALIAS FOR $1;

-- local variables
row_data poem_option_def%ROWTYPE;
newrows record;
i integer;
qString varchar(4000);

BEGIN
qString := ''SELECT p.poem_id,p.poem_title'';

FOR row_data IN SELECT option_id,option_name FROM poem_option_def LOOP
qString := qString || '', (SELECT po.setting FROM poem_option po WHERE po.poem_id = p.poem_id and po.option_id = '' || row_data.option_id || '') AS '' || row_data.option_name;
END LOOP;

qString := qString || '' FROM poem p WHERE p.author_id = '' || p_author_id;

FOR newrows IN EXECUTE qString loop
RETURN NEXT newrows
END LOOP;

RETURN;
end;
' LANGUAGE 'plpgsql';

---------------------------------------

Life is poetry -
write it in your own words.

---------------------------------------

Toby Tremayne
Technical Team Lead
Code Poet and Zen Master of the Heavy Sleep
Toll Solutions
154 Moray St
Sth Melbourne
VIC 3205
+61 3 9697 2317
0416 048 090
ICQ: 13107913

-----BEGIN PGP SIGNATURE-----
Version: 2.6

iQCVAwUAPgATV0YhrxxXvPlFAQH1NAQAr+sRBxO3fOjaJR/CgZZRMdUYLF26alpE
DyP/V4H0Shf2sJomUFblO6KGcU8x/jYSky8xJNHnFftUDDsyX+nlcI5qNyS8ABtS
BJ3EQq8AaW69S+F6QeKFaoXDR/pSWr36+gZr+KeAy4jUMYGtVkUSEXlDiKl1kZv4
o+/1vtl8gi8=
=wf+S
-----END PGP SIGNATURE-----

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Arindam Haldar 2002-12-18 06:19:39 unsubscribe
Previous Message Pramod R. Bhagwat 2002-12-18 06:16:07 datetime