X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 9B2D4DAABB for ; Wed, 26 Oct 2005 07:57:05 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 43395-10 for ; Wed, 26 Oct 2005 10:57:00 +0000 (GMT) Received: from wimpy.net.nih.gov (mailfwd.nih.gov [128.231.88.106]) by svr1.postgresql.org (Postfix) with ESMTP id 4F32ADA924 for ; Wed, 26 Oct 2005 07:57:01 -0300 (ADT) Received: from wimpy.net.nih.gov (localhost [127.0.0.1]) by wimpy.net.nih.gov (8.12.11/8.11.7) with ESMTP id j9QAv3x2026105 for ; Wed, 26 Oct 2005 06:57:04 -0400 (EDT) Received: from [128.231.145.14] (holmes.nhgri.nih.gov [128.231.145.14]) by wimpy.net.nih.gov (8.12.11/8.11.7) with ESMTP id j9QAv2wQ026099; Wed, 26 Oct 2005 06:57:02 -0400 (EDT) User-Agent: Microsoft-Entourage/10.1.4.030702.0 Date: Wed, 26 Oct 2005 06:58:16 -0400 Subject: Re: SETOF RECORD RETURN VALUE From: Sean Davis To: "Christian Paul B. Cosinas" , Message-ID: In-Reply-To: <000001c5da18$e1d9ba20$1e21100a@ghwk02002147> Mime-version: 1.0 Content-type: text/plain; charset="US-ASCII" Content-transfer-encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.116 required=5 tests=[AWL=-0.091, UPPERCASE_25_50=0.207] X-Spam-Level: X-Archive-Number: 200510/320 X-Sequence-Number: 23173 On 10/26/05 6:34 AM, "Christian Paul B. Cosinas" wrote: > Hi I am having some problem with function that returns SETOF RECORD > > Here is my function: > > CREATE OR REPLACE FUNCTION test_record(text) > RETURNS SETOF RECORD AS > $BODY$ > > > DECLARE > p_table_name ALIAS FOR $1; > temp_rec RECORD; > v_query text; > > BEGIN > > v_query = 'SELECT * FROM ' || p_table_name; FOR temp_rec IN EXECUTE v_query > LOOP > RETURN NEXT temp_rec; > END LOOP; > > RETURN ; > > END; > > $BODY$ > LANGUAGE 'plpgsql' VOLATILE; > > > And here is how I execute the function: > select * from test_record('field_list') > > I have this error: > > ERROR: a column definition list is required for functions returning > "record" Since Postgres doesn't know what to expect from your function, you have to tell it by giving the list of columns that are actually returned: select * from test_record('field_list') as s(a,b,c,d) where a,b,c,d are the columns in your returned set. (ie., in your example, if p_table_name has 5 columns, you would use "as s(a,b,c,d,e)", etc.). See here for more detail: http://techdocs.postgresql.org/guides/SetReturningFunctions Sean