Re: Odd behavior of SELECT INTO in PL/pgSQL

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: depstein(at)alliedtesting(dot)com
Cc: pgsql-bugs(at)postgresql(dot)org, pgagarinov(at)alliedtesting(dot)com
Subject: Re: Odd behavior of SELECT INTO in PL/pgSQL
Date: 2010-07-20 13:08:08
Message-ID: AANLkTils2bEjae8AhBj7No_N3y3c2-dJjrCR3JTCXez1@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hello,

this is solved in new PostgreSQL 9.0

postgres=# create table test_table(id int);
CREATE TABLE
postgres=# CREATE OR REPLACE FUNCTION select_test()
postgres-#
postgres-# RETURNS void AS
postgres-#
postgres-# $BODY$
postgres$#
postgres$# DECLARE
postgres$#
postgres$# id integer = -1;
postgres$#
postgres$# BEGIN
postgres$#
postgres$# select max(id) into id from test_table;
postgres$#
postgres$# END
postgres$#
postgres$# $BODY$
postgres-#
postgres-# LANGUAGE 'plpgsql' VOLATILE
postgres-#
postgres-# COST 100;
CREATE FUNCTION
postgres=# select select_test();
ERROR: column reference "id" is ambiguous
LINE 1: select max(id) from test_table
^
DETAIL: It could refer to either a PL/pgSQL variable or a table column.
QUERY: select max(id) from test_table
CONTEXT: PL/pgSQL function "select_test" line 8 at SQL statement
postgres=#

Regards
Pavel Stehule

2010/7/20 <depstein(at)alliedtesting(dot)com>:
> PostgreSQL 8.4
>
>
>
> Here is a PL/pgSQL procedure:
>
>
>
> CREATE OR REPLACE FUNCTION select_test()
>
>   RETURNS void AS
>
> $BODY$
>
> DECLARE
>
> id integer = -1;
>
> BEGIN
>
> select max(id) into id from test_table;
>
> END
>
> $BODY$
>
>   LANGUAGE 'plpgsql' VOLATILE
>
>   COST 100;
>
>
>
> test_table is some table with an integer column ‘id’.
>
>
>
> The above procedure has an obvious mistake: the variable ‘id’ has the same
> name as a column in test_table. The select statement should have generated
> an error, preferably at function creation time.
>
>
>
> What actually happens is that no error is thrown, the select result is not
> assigned to the variable ‘id’, and the function always returns -1.

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message krishnakant.bagwe 2010-07-21 06:04:03 Data extraction via sql from postgres 8.2 to oracle9i.
Previous Message depstein 2010-07-20 12:43:21 Odd behavior of SELECT INTO in PL/pgSQL