pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedProblem with function returning a result set
4+ messages / 3 participants
[nested] [flat]
* Problem with function returning a result set
@ 2010-04-08 08:54 Thomas Kellerer <spam_eater@gmx.net>
2010-04-08 08:59 ` Re: Problem with function returning a result set Tom Lane <tgl@sss.pgh.pa.us>
2010-04-08 09:01 ` Re: Problem with function returning a result set Pavel Stehule <pavel.stehule@gmail.com>
0 siblings, 2 replies; 4+ messages in thread
From: Thomas Kellerer @ 2010-04-08 08:54 UTC (permalink / raw)
To: pgsql-sql
Hi,
I'm playing around with functions returning result sets, and I have a problem with the following function:
-- Create sample data
CREATE TABLE employee (id integer, first_name varchar(50), last_name varchar(50));
INSERT INTO employee values (1, 'Arthur', 'Dent');
INSERT INTO employee values (2, 'Zaphod', 'Beeblebrox');
INSERT INTO employee values (3, 'Ford', 'Prefect');
COMMIT;
-- Create the function
CREATE OR REPLACE FUNCTION get_employees(name_pattern varchar)
RETURNS TABLE(id integer, full_name text)
AS
$$
BEGIN
RETURN QUERY
SELECT id, first_name||' '||last_name
FROM employee
WHERE last_name LIKE name_pattern ||'%';
END
$$
LANGUAGE plpgsql;
COMMIT;
Now when I run:
SELECT *
FROM get_employees('D');
I get one row returned which is correct, but the ID column is null (but should be 1). It does not depend which row(s) I select through the procedure. I also tried to change the datatype of the returned id to int8 and an explicit cast in the SELECT statement, but to no avail.
When I define the function using SQL as a language (with the approriate changes), the ID column is returned correctly.
I'm using Postgres 8.4.3 on Windows XP
postgres=> select version();
version
-------------------------------------------------------------
PostgreSQL 8.4.3, compiled by Visual C++ build 1400, 32-bit
(1 row)
What am I missing?
Regards
Thomas
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Problem with function returning a result set
2010-04-08 08:54 Problem with function returning a result set Thomas Kellerer <spam_eater@gmx.net>
@ 2010-04-08 08:59 ` Tom Lane <tgl@sss.pgh.pa.us>
2010-04-08 09:29 ` Re: Problem with function returning a result set Thomas Kellerer <spam_eater@gmx.net>
1 sibling, 1 reply; 4+ messages in thread
From: Tom Lane @ 2010-04-08 08:59 UTC (permalink / raw)
To: Thomas Kellerer <spam_eater@gmx.net>; +Cc: pgsql-sql
Thomas Kellerer <spam_eater@gmx.net> writes:
> CREATE OR REPLACE FUNCTION get_employees(name_pattern varchar)
> RETURNS TABLE(id integer, full_name text)
> AS
> $$
> BEGIN
> RETURN QUERY
> SELECT id, first_name||' '||last_name
> FROM employee
> WHERE last_name LIKE name_pattern ||'%';
> END
> $$
> LANGUAGE plpgsql;
> I get one row returned which is correct, but the ID column is null
> (but should be 1).
Don't name the parameter the same as the table column ...
regards, tom lane
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Problem with function returning a result set
2010-04-08 08:54 Problem with function returning a result set Thomas Kellerer <spam_eater@gmx.net>
2010-04-08 08:59 ` Re: Problem with function returning a result set Tom Lane <tgl@sss.pgh.pa.us>
@ 2010-04-08 09:29 ` Thomas Kellerer <spam_eater@gmx.net>
0 siblings, 0 replies; 4+ messages in thread
From: Thomas Kellerer @ 2010-04-08 09:29 UTC (permalink / raw)
To: pgsql-sql
Tom Lane, 08.04.2010 10:59:
> Thomas Kellerer<spam_eater@gmx.net> writes:
>> CREATE OR REPLACE FUNCTION get_employees(name_pattern varchar)
>> RETURNS TABLE(id integer, full_name text)
>> AS
>> $$
>> BEGIN
>
>> RETURN QUERY
>> SELECT id, first_name||' '||last_name
>> FROM employee
>> WHERE last_name LIKE name_pattern ||'%';
>> END
>> $$
>> LANGUAGE plpgsql;
>
>> I get one row returned which is correct, but the ID column is null
>> (but should be 1).
>
> Don't name the parameter the same as the table column ...
>
> regards, tom lane
>
I knew it was something simple I overlooked ;)
Thanks for the quick response.
Regards
Thomas
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: Problem with function returning a result set
2010-04-08 08:54 Problem with function returning a result set Thomas Kellerer <spam_eater@gmx.net>
@ 2010-04-08 09:01 ` Pavel Stehule <pavel.stehule@gmail.com>
1 sibling, 0 replies; 4+ messages in thread
From: Pavel Stehule @ 2010-04-08 09:01 UTC (permalink / raw)
To: Thomas Kellerer <spam_eater@gmx.net>; +Cc: pgsql-sql
2010/4/8 Thomas Kellerer <spam_eater@gmx.net>:
> Hi,
>
> I'm playing around with functions returning result sets, and I have a
> problem with the following function:
>
> -- Create sample data
> CREATE TABLE employee (id integer, first_name varchar(50), last_name
> varchar(50));
> INSERT INTO employee values (1, 'Arthur', 'Dent');
> INSERT INTO employee values (2, 'Zaphod', 'Beeblebrox');
> INSERT INTO employee values (3, 'Ford', 'Prefect');
> COMMIT;
>
> -- Create the function
> CREATE OR REPLACE FUNCTION get_employees(name_pattern varchar)
> RETURNS TABLE(id integer, full_name text)
> AS
> $$
> BEGIN
>
> RETURN QUERY
> SELECT id, first_name||' '||last_name
> FROM employee
> WHERE last_name LIKE name_pattern ||'%';
> END
> $$
> LANGUAGE plpgsql;
>
> COMMIT;
>
> Now when I run:
>
> SELECT *
> FROM get_employees('D');
>
> I get one row returned which is correct, but the ID column is null (but
> should be 1). It does not depend which row(s) I select through the
> procedure. I also tried to change the datatype of the returned id to int8
> and an explicit cast in the SELECT statement, but to no avail.
>
> When I define the function using SQL as a language (with the approriate
> changes), the ID column is returned correctly.
>
> I'm using Postgres 8.4.3 on Windows XP
> postgres=> select version();
> version
> -------------------------------------------------------------
> PostgreSQL 8.4.3, compiled by Visual C++ build 1400, 32-bit
> (1 row)
>
> What am I missing?
there are collision between SQL and PLpgSQL identifiers.
RETURNS TABLE(id integer, full_name text) AS
$$
BEGIN
RETURN QUERY
SELECT e.id, e.first_name||' '||e.last_name
FROM employee e
WHERE e.last_name LIKE e.name_pattern ||'%';
END
$$
LANGUAGE plpgsql;
use aliases.
Regards
Pavel Stehule
>
> Regards
> Thomas
>
>
> --
> Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql
>
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2010-04-08 09:29 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2010-04-08 08:54 Problem with function returning a result set Thomas Kellerer <spam_eater@gmx.net>
2010-04-08 08:59 ` Tom Lane <tgl@sss.pgh.pa.us>
2010-04-08 09:29 ` Thomas Kellerer <spam_eater@gmx.net>
2010-04-08 09:01 ` Pavel Stehule <pavel.stehule@gmail.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox