Re: Cursor names in a self-nested function

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: "Kidd, David M" <d(dot)kidd(at)imperial(dot)ac(dot)uk>
Cc: "pgsql-sql(at)postgresql(dot)org" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Cursor names in a self-nested function
Date: 2011-08-18 15:36:07
Message-ID: CAFj8pRA363gGDweH4Qgur0c7QWZwiYe2V1xMNbH+GiGZxLG8Sw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hello

you can use a refcursor type

http://developer.postgresql.org/pgdocs/postgres/plpgsql-cursors.html

Regards

Pavel Stehule

2011/8/18 Kidd, David M <d(dot)kidd(at)imperial(dot)ac(dot)uk>:
> Hi,
>
> I am trying to write a function that contains a cursor and iteratively calls
> itself.
>
> It is along the lines of,
>
> CREATE FUNCTON test(id integer) RETURNS TEXT AS
> $BODY$
> DECLARE
>   mycursor CURSOR FOR SELECT * FROM myfunction(id);
>   newid INTEGER;
>   out = TEXT;
> BEGIN
>   out := '';
>   OPEN mycursor;
>   LOOP
>     FETCH my_cursor INTO newid;
>     out := out || test (newid);
>   END LOOP;
>   RETURN out;
> END;
>  $BODY$
> LANGUAGE 'plpgsql' VOLATILE
>
> This returns an ERROR stating that "mycursor" is already in use.
>
> I understand this occurs because cursor names must be unique across, as well
> as within, functions.
>
> So, my question is whether there is a way I can dynamically declare a cursor
> name, for example by appending a incremental number or guid to make the name
> unique?
> Just trying to concatenate two passed arguments in the DECLARE statement
> unsurprisingly fails.
>
> Any other solutions are of cause welcome.
>
> Many thanks,
>
>  - David
>
>
>
> David M. Kidd
>
> Research Associate
> Center for Population Biology
> Silwood Park Campus
> Imperial College London
> 0207 594 2470
>
>

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2011-08-18 16:15:33 Re: Cursor names in a self-nested function
Previous Message Kidd, David M 2011-08-18 14:53:18 Cursor names in a self-nested function