From: Thomas Kellerer <spam_eater@gmx.net>
To: pgsql-sql@postgresql.org
Subject: Problem with function returning a result set
Date: Thu, 08 Apr 2010 10:54:36 +0200
Message-ID: <hpk5kd$98o$1@dough.gmane.org> (raw)
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: spam_eater@gmx.net
Subject: Re: Problem with function returning a result set
In-Reply-To: <hpk5kd$98o$1@dough.gmane.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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