Re: BUG #2451: Short column names return no values within function - Mailing list pgsql-bugs

From Tom Lane
Subject Re: BUG #2451: Short column names return no values within function
Date
Msg-id 14843.1148399417@sss.pgh.pa.us
Whole thread Raw
In response to BUG #2451: Short column names return no values within function  ("Alex Weslowski" <aweslowski@rpa.com>)
List pgsql-bugs
"Alex Weslowski" <aweslowski@rpa.com> writes:
> DECLARE
>     peg VARCHAR(5) := '';
>     rs03 VARCHAR(3) := '';
>     rs12 VARCHAR(3) := '';
>     rec RECORD;
> BEGIN
>     FOR rec IN SELECT Symbol, RS03, RS12, Peg
>     FROM TestBug WHERE Date=d
>     ORDER BY RS12 DESC LOOP

The problem is that you've got local variables shadowing the field names
you want to use.  What the SQL engine sees from that is

    SELECT Symbol, $1, $2, $3
    FROM TestBug WHERE Date=$4
    ORDER BY $5 DESC

and it just assigns random ?columnN? names to the record columns (which
are going to have useless empty-string values anyway).

Use different local variable names, or qualify the field references in
the SELECT, eg TestBug.RS03.

            regards, tom lane

pgsql-bugs by date:

Previous
From: "qinyan"
Date:
Subject: missing or erroneous pg_hba.conf file
Next
From: Michael Fuhr
Date:
Subject: Re: BUG #2451: Short column names return no values within function