Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 1276F4765C2 for ; Sat, 25 Jan 2003 22:54:42 -0500 (EST) Received: from candle.pha.pa.us (momjian.navpoint.com [207.106.42.251]) by postgresql.org (Postfix) with ESMTP id 73EC5476694 for ; Sat, 25 Jan 2003 22:53:10 -0500 (EST) Received: (from pgman@localhost) by candle.pha.pa.us (8.11.6/8.10.1) id h0Q3pUL14915; Sat, 25 Jan 2003 22:51:30 -0500 (EST) From: Bruce Momjian Message-Id: <200301260351.h0Q3pUL14915@candle.pha.pa.us> Subject: Re: returning setof in plpgsql In-Reply-To: <2305.1043163281@sss.pgh.pa.us> To: Tom Lane Date: Sat, 25 Jan 2003 22:51:30 -0500 (EST) Cc: ddurst@larubber.com, pgsql-sql@postgresql.org X-Mailer: ELM [version 2.4ME+ PL99 (25)] MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200301/411 X-Sequence-Number: 11276 I thought we had an example of this type of function in the docs, but we don't. Here is one: http://www.brasileiro.net:8080/postgres/cookbook/view-recipes.adp?section_id=535&format=long --------------------------------------------------------------------------- Tom Lane wrote: > "David Durst" writes: > > CREATE FUNCTION lookup_account(varchar(32)) RETURNS SETOF accounts AS ' > > DECLARE > > aname ALIAS FOR $1; > > rec RECORD; > > BEGIN > > select into rec * from accounts where accountname = aname; > > return rec; > > END;' > > LANGUAGE 'plpgsql'; > > As written, this function can only return a single row (so you hardly > need SETOF). If you intend that it be able to return multiple rows > when accountname is not unique, then you'll need a loop and RETURN NEXT > commands. It'd probably be less tedious to use a SQL-language function: > > CREATE FUNCTION lookup_account(varchar(32)) RETURNS SETOF accounts AS ' > select * from accounts where accountname = $1' > language sql; > > > This seems to hang when I attempt to select it using: > > > select accountid( > > lookup_account('some account')), > > accountname(lookup_account('some account')), > > type(lookup_account('some account')), > > balance(lookup_account('some account')); > > It works for me (in 7.3), but in any case that's a bad approach: you're > invoking the function four times, independently. Better is > > select accountid,accountname,type,balance > from lookup_account('some account'); > > (again, this syntax requires 7.3) > > regards, tom lane > > ---------------------------(end of broadcast)--------------------------- > TIP 3: if posting/reading through Usenet, please send an appropriate > subscribe-nomail command to majordomo@postgresql.org so that your > message can get through to the mailing list cleanly > -- Bruce Momjian | http://candle.pha.pa.us pgman@candle.pha.pa.us | (610) 359-1001 + If your life is a hard drive, | 13 Roberts Road + Christ can be your backup. | Newtown Square, Pennsylvania 19073