Re: Cursors and recursion - Mailing list pgsql-sql

From Michael Fuhr
Subject Re: Cursors and recursion
Date
Msg-id 20051229024816.GA32249@winnie.fuhr.org
Whole thread Raw
In response to Re: Cursors and recursion  (Don Croata <el.croata@gmail.com>)
List pgsql-sql
On Wed, Dec 28, 2005 at 04:37:21PM -0300, Don Croata wrote:
> Please, if someone recalls a link, book, piece of code or anything with info
> about this technique for PL/PgSQL (8.1), please let us know. We've been
> searching into google, groups.google, http://archives.postgresql.org and
> http://www.postgresql.org/docs/8.1/interactive with no results. Most of the
> answers are related to unclosed cursors for the "ERROR: cursor ... already
> in use" message.

See the "Cursors" section of the PL/pgSQL documentation and read
about unbound cursors:

http://www.postgresql.org/docs/8.1/interactive/plpgsql-cursors.html

"Note:  A bound cursor variable is initialized to the string value
representing its name, so that the portal name is the same as the
cursor variable name, unless the programmer overrides it by assignment
before opening the cursor.  But an unbound cursor variable defaults
to the null value initially, so it will receive an automatically-generated
unique name, unless overridden."

For example, instead of
 DECLARE     cur CURSOR FOR SELECT ...; BEGIN     OPEN cur;

use
 DECLARE     cur refcursor; BEGIN     OPEN cur FOR SELECT ...;

But as I mentioned in a previous post, it's usually easier to use
FOR-IN-LOOP.

-- 
Michael Fuhr


pgsql-sql by date:

Previous
From: "George Pavlov"
Date:
Subject: Re: DISTINCT with NULLs and INT fields
Next
From: Tom Lane
Date:
Subject: Re: DISTINCT with NULLs and INT fields