Re: use cursor in a function

From: Rory Campbell-Lange <rory(at)campbell-lange(dot)net>
To: Joe Conway <mail(at)joeconway(dot)com>
Cc: Postgresql Novice List <pgsql-novice(at)postgresql(dot)org>
Subject: Re: use cursor in a function
Date: 2003-06-17 12:34:21
Message-ID: 20030617123421.GC6349@campbell-lange.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On 17/06/03, Joe Conway (mail(at)joeconway(dot)com) wrote:
> Rory Campbell-Lange wrote:
> >Is this sensible, efficient, feasible? I have not used cursors before.
> >
> >Or should I simply do 2 separate functions, 1 for NUM_ROWS, the
> >other for the result set I want?
>
> It's hard to offer advice (at least for me) without more context. What
> are you trying to accomplish (in more detail), and in what type of
> application (web, fat client, etc)?

Its a web page <page> of <pages> scenario.

I need to find the total number of rows returned by a specific WHERE,
and turn that into <pages>.

I also need to return a set of rows, using LIMIT and OFFSET to grab a
subset of the rows and return these to the client.

I wondered if I could do this with one call rather than two. My complete
ignorance of how to use cursors is demonstrated in my second imaginary
example! I realise I can do more or less what I want in a single query
by using a LEFT OUTER JOIN to count(n_id) on a duplicate WHERE query.

Just wondered if cursors provide a cleaner looking query to do that.

Thanks!
Rory

Instead of going (psuedocode):

SELECT into record
n_id
FROM
table
WHERE
complex_where;

numrows := NUMROWS;

FOR resulter IN
numrows, this, that, tother
FROM
table
WHERE
complex_where
LIMIT
limit
OFFSET
offset
LOOP

return next resulter;

END LOOP;

I imagine going:

OPEN mycursor FOR
SELECT
this, that, tother
FROM
table
WHERE
complex_where;

numrows := mycursor->NUMROWS (!);
cursor_move(offset);
cursor_get(limit);

somehow get cursor values into a RECORD...

--
Rory Campbell-Lange
<rory(at)campbell-lange(dot)net>
<www.campbell-lange.net>

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Harry Broomhall 2003-06-17 14:08:08 Commit within a PL/PGSQL procedure
Previous Message Brent Howard 2003-06-17 12:28:45 Issues starting postgres on Solaris