X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 18FB79DCBD9 for ; Thu, 9 Mar 2006 10:15:26 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 74271-01 for ; Thu, 9 Mar 2006 10:15:29 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from pih-relay06.plus.net (pih-relay06.plus.net [212.159.14.133]) by postgresql.org (Postfix) with ESMTP id 88C5D9DCABD for ; Thu, 9 Mar 2006 10:15:23 -0400 (AST) Received: from [213.162.97.75] (helo=mail.metronet.co.uk) by pih-relay06.plus.net with esmtp (Exim) id 1FHLvL-0001Ro-4b; Thu, 09 Mar 2006 14:15:27 +0000 Received: from mainbox.archonet.com (unknown [84.51.143.99]) by smtp.metronet.co.uk (MetroNet Mail) with ESMTP id 4ED2840D04D; Thu, 9 Mar 2006 13:59:17 +0000 (GMT) Received: from [192.168.1.17] (client17.office.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id 0660EFF04; Thu, 9 Mar 2006 13:59:16 +0000 (GMT) Message-ID: <441034B4.1060400@archonet.com> Date: Thu, 09 Mar 2006 13:59:16 +0000 From: Richard Huxton User-Agent: Thunderbird 1.5 (X11/20051201) MIME-Version: 1.0 To: Klay Martens Cc: pgsql-sql@postgresql.org Subject: Re: References: In-Reply-To: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.121 required=5 tests=[AWL=0.121] X-Spam-Score: 0.121 X-Spam-Level: X-Archive-Number: 200603/138 X-Sequence-Number: 24484 Klay Martens wrote: > Hi all. >=20 > I am new to postgres, so I am still learning the basics. >=20 > In Sequel Server, one can set up a function to return a table eg: >=20 > =20 >=20 > CREATE FUNCTION [dbo].[AuthCodes] (@CLIENTID INT)=20 > RETURNS @AuthCodes TABLE [snip] > and then use select into or insert to populate the table. >=20 > I am really battling to figure out how to do the same in a postgres fun= ction. >=20 > It seems like one would have to create a user defined type (myType for = eg), and=20 > then set the return type of the function to be a set of myType, but I c= an=92t seem=20 > to make it work. I am guessing that I am on the wrong track=85does anyo= ne have any=20 > suggestions, or examples I could follow? You've got the right idea. If there's not an existing table, define a=20 type, return SETOF my_type and use RETURN NEXT inside the function to=20 return each row. You call the function as: "SELECT * FROM myfunc()",=20 that is - treat it as a table. There's an item on set-returning functions here: http://techdocs.postgresql.org/ and here http://www.varlena.com/varlena/GeneralBits/Tidbits/index.php --=20 Richard Huxton Archonet Ltd