Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 5B8E4632872 for ; Thu, 8 Apr 2010 05:54:59 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 56787-03 for ; Thu, 8 Apr 2010 08:54:48 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from lo.gmane.org (lo.gmane.org [80.91.229.12]) by mail.postgresql.org (Postfix) with ESMTP id CAF3C6327F3 for ; Thu, 8 Apr 2010 05:54:48 -0300 (ADT) Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1NznVf-00011p-HI for pgsql-sql@postgresql.org; Thu, 08 Apr 2010 10:54:47 +0200 Received: from 217.110.94.67 ([217.110.94.67]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 08 Apr 2010 10:54:47 +0200 Received: from spam_eater by 217.110.94.67 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 08 Apr 2010 10:54:47 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Thomas Kellerer Subject: Problem with function returning a result set Date: Thu, 08 Apr 2010 10:54:36 +0200 Lines: 48 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: 217.110.94.67 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=2.068 tagged_above=-10 required=5 tests=BAYES_50=0.001, RCVD_NUMERIC_HELO=2.067 X-Spam-Level: ** X-Archive-Number: 201004/27 X-Sequence-Number: 33954 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