return setof record from function with dynamic query

Поиск
Список
Период
Сортировка
От Toby Tremayne
Тема return setof record from function with dynamic query
Дата
Msg-id 867309281.20021218171856@toll.com.au
обсуждение исходный текст
Ответы Re: return setof record from function with dynamic query  (Stephan Szabo <sszabo@megazone23.bigpanda.com>)
Re: return setof record from function with dynamic query  (Masaru Sugawara <rk73@sea.plala.or.jp>)
Список 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-----


В списке pgsql-general по дате отправления:

Предыдущее
От: "Pramod R. Bhagwat"
Дата:
Сообщение: datetime
Следующее
От: "Envex Developments"
Дата:
Сообщение: SQL alternative to pg_dump