Re: ROWTYPE initialization question - Mailing list pgsql-general

From Jim C. Nasby
Subject Re: ROWTYPE initialization question
Date
Msg-id 20061114193717.GN90133@nasby.net
Whole thread Raw
In response to ROWTYPE initialization question  (Alban Hertroys <alban@magproductions.nl>)
Responses Re: ROWTYPE initialization question  (Alban Hertroys <alban@magproductions.nl>)
List pgsql-general
On Thu, Nov 09, 2006 at 04:37:23PM +0100, Alban Hertroys wrote:
> 'lo list,
>
> I have a plpgsql SP where I loop through a cursor. I have an internal
> variable that keeps the previous row, so that I can compare it with the
> current row in the cursor.
> Like so;
>
> DECLARE
>     current table%ROWTYPE;
>     previous table%ROWTYPE;
> BEGIN
>     LOOP
>     FETCH tableCur INTO current;
>
>     -- Do stuff
>
> But, in this loop I need to compare the previous row to the current one.
> To do that I need to know whether a row was assigned to 'previous', or
> there'll be very little to compare (if it doesn't throw an error).
> How do I check for that?
>
> I guess it would be like this, but I'd like to be sure.
>
>     IF previous IS NOT NULL
>     THEN
>         -- Compare previous and current column values
>     END IF
>
>     previous := current;
>     END LOOP;
> END;

Try it and see. :) I think that will work.

Might be better to just capture the error.
--
Jim Nasby                                            jim@nasby.net
EnterpriseDB      http://enterprisedb.com      512.569.9461 (cell)

pgsql-general by date:

Previous
From: "Dave Page"
Date:
Subject: Re: Trouble migrating from PostgreSQL --> Oracle
Next
From: "Jim C. Nasby"
Date:
Subject: Re: SQL Join for a Calculation