Re: BUG #6031: Bug with plpgsql function and RETURNS TABLE

From: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
To: Preston M(dot) Price <preston(at)clearwateranalytics(dot)com>
Cc: pgsql-bugs <pgsql-bugs(at)postgresql(dot)org>
Subject: Re: BUG #6031: Bug with plpgsql function and RETURNS TABLE
Date: 2011-05-20 20:15:58
Message-ID: 1305922486-sup-532@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Excerpts from Preston M. Price's message of vie may 20 12:12:09 -0400 2011:

> If I create a plpgsql function and use RETURNS TABLE
> the returned result set is filled with null values rather than the values
> from the table.

> select * from TEST_FUNC();
>
> This query yields 5 rows of null rather than the values from the test table.

Yeah, known problem. Don't use the column name as variable name; or you
can qualify the column name in the query, like this:

alvherre=# CREATE or replace FUNCTION TEST_FUNC() RETURNS TABLE(val int) AS $$
BEGIN
RETURN QUERY SELECT test.val FROM test;
END;
$$ LANGUAGE plpgsql;
CREATE FUNCTION

alvherre=# select * from TEST_FUNC();
val
-----
1
2
3
4
5
(5 filas)

--
Álvaro Herrera <alvherre(at)commandprompt(dot)com>
The PostgreSQL Company - Command Prompt, Inc.
PostgreSQL Replication, Consulting, Custom Development, 24x7 support

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Emmanuel 2011-05-21 18:34:37 BUG #6032: server fail to connect
Previous Message Preston M. Price 2011-05-20 16:12:09 BUG #6031: Bug with plpgsql function and RETURNS TABLE