pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: ddurst@larubber.com
Cc: pgsql-sql@postgresql.org
Subject: Re: returning setof in plpgsql
Date: Tue, 21 Jan 2003 10:34:41 -0500
Message-ID: <2305.1043163281@sss.pgh.pa.us> (raw)
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>

"David Durst" <ddurst@larubber.com> 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



view thread (4+ messages)  latest in thread

Message-ID: <2305.1043163281@sss.pgh.pa.us>
Permalink:  ../2305.1043163281@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/2305.1043163281@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, ddurst@larubber.com
  Subject: Re: returning setof in plpgsql
  In-Reply-To: <2305.1043163281@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox