Re: ROWTYPE initialization question

From: "Jim C(dot) Nasby" <jim(at)nasby(dot)net>
To: Alban Hertroys <alban(at)magproductions(dot)nl>
Cc: Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Re: ROWTYPE initialization question
Date: 2006-11-14 19:37:18
Message-ID: 20061114193717.GN90133@nasby.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)nasby(dot)net
EnterpriseDB http://enterprisedb.com 512.569.9461 (cell)

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jim C. Nasby 2006-11-14 19:42:37 Re: SQL Join for a Calculation
Previous Message Ardian Xharra 2006-11-14 19:36:48 Re: Data corruption