Re: Different results in a loop with RECORD vs ROWTYPE... - Mailing list pgsql-bugs

From Tom Lane
Subject Re: Different results in a loop with RECORD vs ROWTYPE...
Date
Msg-id 13405.1053661679@sss.pgh.pa.us
Whole thread Raw
In response to Re: Different results in a loop with RECORD vs ROWTYPE...  (Sean Chittenden <sean@chittenden.org>)
Responses Re: Different results in a loop with RECORD vs ROWTYPE...
List pgsql-bugs
Sean Chittenden <sean@chittenden.org> writes:
> CREATE TABLE s.c (
>     x BIGINT NOT NULL,
>     y BIGINT NOT NULL,
>     w INT NOT NULL DEFAULT 1::INT
> );

> DECLARE
>     r_c s.c%ROWTYPE; -- RECORD;
> BEGIN
>     FOR r_c IN SELECT d.y FROM s.c d WHERE d.x = NEW.x LOOP
>         PERFORM s.add_y_to_x(r_c.y,NEW.z);

It seems to me that the rowtype of this SELECT's result is (y bigint).
When you declare r_c as RECORD, it adopts that rowtype, and so the
reference to r_c.y in the PERFORM delivers the value you want.  But
when you declare r_c as s.c%ROWTYPE, that is (x bigint, y bigint, w int),
the result of the SELECT's first column is delivered into r_c.x and then
the other two columns are set to null.  So r_c.y is null in the PERFORM.

I think this is basically pilot error, though one could certainly argue
that the system ought to be complaining that the SELECT didn't deliver
enough columns to fill the rowtype variable.  Any thoughts?  Can anyone
report what Oracle's pl/sql does in comparable situations?

            regards, tom lane

pgsql-bugs by date:

Previous
From: Sean Chittenden
Date:
Subject: Re: Different results in a loop with RECORD vs ROWTYPE...
Next
From: Sean Chittenden
Date:
Subject: Re: Different results in a loop with RECORD vs ROWTYPE...