Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 8A3CD632315 for ; Thu, 8 Apr 2010 06:01:32 -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 58940-09 for ; Thu, 8 Apr 2010 09:01:21 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-ww0-f46.google.com (mail-ww0-f46.google.com [74.125.82.46]) by mail.postgresql.org (Postfix) with ESMTP id D4139632224 for ; Thu, 8 Apr 2010 06:01:21 -0300 (ADT) Received: by wwi17 with SMTP id 17so7009wwi.19 for ; Thu, 08 Apr 2010 02:01:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:received:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=ZHjHwukD+4Nl/QxCHe7JEtl0kFTJVpNR6JqG8jowIC0=; b=KrNe8/fmhhC3RGBxlhPIrJ4XvGUXC9kTKsLVg/4XE27XhLaJMuczwsXgNW4X21CeYB vVcvTk+gWVHhNbHAHNpZKLBK/pLDz1J54dl8ENeW95c/o9AdQA5Qf8Dtm1ibNyfYSBcg CRLMxmET8YjI3UEgPyC5HkTHRtdhdNC5kwE6s= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=htXbx185ahI8kiA1AtFoenqY6yyUTbl1NpH30x9lfgtolFFZhuQk3ozsCfwDFyEqMT t4FJLT/+YXh8J+ljPOyR7r7/hLM+52xG+/rib9B9uxJ62ylnhKHQfsUwF/lGyMzBrBqZ mkjKunu+Aj7JIlzUtGnva31OcUGG+Vmd3nh/8= MIME-Version: 1.0 Received: by 10.216.233.151 with HTTP; Thu, 8 Apr 2010 02:01:20 -0700 (PDT) In-Reply-To: References: Date: Thu, 8 Apr 2010 11:01:20 +0200 Received: by 10.216.157.212 with SMTP id o62mr216860wek.41.1270717280131; Thu, 08 Apr 2010 02:01:20 -0700 (PDT) Message-ID: Subject: Re: Problem with function returning a result set From: Pavel Stehule To: Thomas Kellerer Cc: pgsql-sql@postgresql.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201004/29 X-Sequence-Number: 33956 2010/4/8 Thomas Kellerer : > 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 > > =C2=A0RETURN QUERY > =C2=A0 =C2=A0SELECT id, first_name||' '||last_name > =C2=A0 =C2=A0FROM employee > =C2=A0 =C2=A0WHERE 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=3D> select version(); > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 version > ------------------------------------------------------------- > =C2=A0PostgreSQL 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 >