X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 6C97ED1E931 for ; Sun, 29 Feb 2004 23:33:46 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 11670-07 for ; Sun, 29 Feb 2004 19:33:47 -0400 (AST) Received: from joeconway.com (66-146-172-86.skyriver.net [66.146.172.86]) by svr1.postgresql.org (Postfix) with ESMTP id 134B4D1E92F for ; Sun, 29 Feb 2004 19:33:44 -0400 (AST) Received: from [192.168.5.3] (account jconway HELO joeconway.com) by joeconway.com (CommuniGate Pro SMTP 4.1.8) with ESMTP-TLS id 1026862; Sun, 29 Feb 2004 15:29:41 -0800 Message-ID: <40427698.2010402@joeconway.com> Date: Sun, 29 Feb 2004 15:32:40 -0800 From: Joe Conway User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.6) Gecko/20040113 X-Accept-Language: en-us, en MIME-Version: 1.0 To: jbduffy@ntlworld.com Cc: pgsql-sql@postgresql.org Subject: Re: plpgsql function returning SETOF RECORD Question References: <20040223132208.MQDR10961.mta07-svc.ntlworld.com@mta7-svc> In-Reply-To: <20040223132208.MQDR10961.mta07-svc.ntlworld.com@mta7-svc> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200402/403 X-Sequence-Number: 17093 jbduffy@ntlworld.com wrote: > When I run the function below I recieve an error message saying that > column definitions need to be specified for a function returing a > type RECORD. > > I was under the impression that the FOR row IN SELECT... loop would > assign a column structure to the RECORD type. Am I wrong about this? > > CREATE FUNCTION test() RETURNS SETOF RECORD AS ' DECLARE row RECORD; > BEGIN FOR row IN SELECT * FROM dates LOOP RETURN NEXT row; END LOOP; > RETURN; END; ' LANGUAGE 'plpgsql'; Please look at the docs: http://www.postgresql.org/docs/current/static/sql-select.html Specifically, the latter half of this paragraph: "Function calls can appear in the FROM clause. (This is especially useful for functions that return result sets, but any function can be used.) This acts as though its output were created as a temporary table for the duration of this single SELECT command. An alias may also be used. If an alias is written, a column alias list can also be written to provide substitute names for one or more attributes of the function's composite return type. If the function has been defined as returning the record data type, then an alias or the key word AS must be present, followed by a column definition list in the form ( column_name data_type [, ... ] ). The column definition list must match the actual number and types of columns returned by the function." and the example further down the page: CREATE FUNCTION distributors_2(int) RETURNS SETOF record AS ' SELECT * FROM distributors WHERE did = $1; ' LANGUAGE SQL; SELECT * FROM distributors_2(111) AS (f1 int, f2 text); f1 | f2 -----+------------- 111 | Walt Disney HTH, Joe