refursor from dynamic query

From: Toby Tremayne <lists(at)devfrag(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: refursor from dynamic query
Date: 2002-12-11 21:59:03
Message-ID: 192308440964.20021212085903@devfrag.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi all,

just subscribed to the list - I've been writing my first
function/stored procedure and I've hit a bit of a problem. I'm
trying to build a dynamic query, then execute it but I need to
return a refcursor containing the records the dynamic query finds.
My code is below - I'd appreciate it if anyone could point me in
the right direction....

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

-- local variables
rc REFCURSOR;
row_data poem_option_def%ROWTYPE;
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;

OPEN rc FOR EXECUTE qString;

RETURN rc;
end;
' LANGUAGE 'plpgsql';

cheers,
Toby

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

Life is poetry - write it in your own words.

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

Toby Tremayne
Senior Technical Consultant
Code Poet and Zen Master of the Heavy Sleep
Lyricist Software
www.lyricist.com.au
0416 048 090
ICQ: 13107913

Browse pgsql-general by date

  From Date Subject
Next Message Ian Barwick 2002-12-11 22:25:56 Re: 7.2.x -> 7.3 upgrade docs?
Previous Message Thomas O'Connell 2002-12-11 20:43:27 7.2.x -> 7.3 upgrade docs?