Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id DB7BD475AF8 for ; Tue, 21 Jan 2003 10:34:47 -0500 (EST) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by postgresql.org (Postfix) with ESMTP id 2D22A4758C9 for ; Tue, 21 Jan 2003 10:34:47 -0500 (EST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.6/8.12.6) with ESMTP id h0LFYf5u002306; Tue, 21 Jan 2003 10:34:41 -0500 (EST) To: ddurst@larubber.com Cc: pgsql-sql@postgresql.org Subject: Re: returning setof in plpgsql In-reply-to: <52900.216.86.192.34.1043145602.squirrel@www.la-rubber.com> References: <52900.216.86.192.34.1043145602.squirrel@www.la-rubber.com> Comments: In-reply-to "David Durst" message dated "Tue, 21 Jan 2003 02:40:02 -0800" Date: Tue, 21 Jan 2003 10:34:41 -0500 Message-ID: <2305.1043163281@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200301/321 X-Sequence-Number: 11186 "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