Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 6F4E3475FB5 for ; Wed, 30 Oct 2002 04:42:43 -0500 (EST) Received: from mailhub2.sghms.ac.uk (firewall.sghms.ac.uk [194.82.50.2]) by postgresql.org (Postfix) with ESMTP id 84143475DFF for ; Wed, 30 Oct 2002 04:42:42 -0500 (EST) Received: from [194.82.51.24] (helo=imail) by mailhub2.sghms.ac.uk with esmtp (Exim 4.05) id 186pIQ-0004D1-00; Wed, 30 Oct 2002 09:37:54 +0000 Received: from [172.16.20.3] (mrc1-003.sghms.ac.uk [172.16.20.3]) by imail.sghms.ac.uk (iPlanet Messaging Server 5.1 HotFix 0.7 (built May 7 2002)) with ESMTPA id <0H4S0067EGA8RI@imail.sghms.ac.uk>; Wed, 30 Oct 2002 09:42:08 +0000 (GMT) Date: Wed, 30 Oct 2002 09:42:20 +0000 From: Adam Witney In-reply-to: To: Jonas Wouters , pgsql-sql Message-id: MIME-version: 1.0 User-Agent: Microsoft-Entourage/10.1.0.2006 Subject: Re: Creating Stored Procedures Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7BIT X-Spam-Status: No, hits=-1.6 required=5.7 tests=IN_REP_TO,QUOTED_EMAIL_TEXT,SPAM_PHRASE_00_01,USER_AGENT, USER_AGENT_ENTOURAGE version=2.43 X-Spam-Level: X-MailScanner: Found to be clean X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200210/404 X-Sequence-Number: 10121 > I installed 7.3beta3 but how do I do it now? > How do I make a function that returns a dataset > > Greets > Zertox He is an email I received from Joe Conway on the subject a month or so ago > Adam Witney wrote: >> There have been a few emails recently concerning using functions. However I >> am a little confused as to their use with composite types. I can see how to >> return a whole row from a table, but is it possible to return multiple >> fields that do not originate from the same table? > > Sure. But you either need a named composite type that matches the row you want > to return, or you can use a record datatype and specify the column definitions > in the sql statement at run time. > > A composite type exists for each table and view in your database, as well as > any stand-alone composite types you define. So, for example: > > test=# create table foo (f1 int,f2 text); > CREATE TABLE > test=# create table bar (f3 int,f4 text); > CREATE TABLE > test=# create view foobar as select f1,f2,f4 from foo, bar where f1=f3; > CREATE VIEW > test=# insert into foo values(1,'a'); > INSERT 1105496 1 > test=# insert into foo values(2,'b'); > INSERT 1105497 1 > test=# insert into bar values(1,'c'); > INSERT 1105498 1 > test=# insert into bar values(2,'d'); > INSERT 1105499 1 > > -- This uses a named composite type based on the view > test=# create function getfoobar1() returns setof foobar as 'select f1,f2,f4 > from foo, bar where f1=f3' language sql; > CREATE FUNCTION > test=# select * from getfoobar1(); > f1 | f2 | f4 > ----+----+---- > 1 | a | c > 2 | b | d > (2 rows) > > -- This uses an anonymous composite type specified at runtime > test=# create function getfoobar2() returns setof record as 'select f1,f2,f4 > from foo, bar where f1=f3' language sql; > CREATE FUNCTION > test=# select * from getfoobar2() as (f1 int,f2 text,f4 text); > f1 | f2 | f4 > ----+----+---- > 1 | a | c > 2 | b | d > (2 rows) > > HTH, > > Joe > > > ---------------------------(end of broadcast)--------------------------- > TIP 2: you can get off all lists at once with the unregister command > (send "unregister YourEmailAddressHere" to majordomo@postgresql.org) > -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean.