Re: returning setof in plpgsql

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: returning setof in plpgsql
Дата
Msg-id 2305.1043163281@sss.pgh.pa.us
обсуждение исходный текст
Ответ на returning setof in plpgsql  ("David Durst" <ddurst@larubber.com>)
Ответы Re: returning setof in plpgsql  (Bruce Momjian <pgman@candle.pha.pa.us>)
Список pgsql-sql
"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


В списке pgsql-sql по дате отправления:

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: Speed depending of Join Order.
Следующее
От: Rudi Starcevic
Дата:
Сообщение: help