Re: How return a refcusor using functions????

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: How return a refcusor using functions????
Дата
Msg-id 29244.1053114965@sss.pgh.pa.us
обсуждение исходный текст
Ответ на Re: How return a refcusor using functions????  (frank@chagford.com (Frank Millman))
Список pgsql-general
frank@chagford.com (Frank Millman) writes:
> create or replace function v_ArMaster() returns refcursor as '
>   declare
>     curs1 refcursor;
>   begin
>     open curs1 for select AccNo, Name, Contact, Phone from ArMaster
> order by AccNo;
>     return curs1;
>   end;'
> language 'plpgsql';

> begin;
> select v_ArMaster();
> fetch all from curs1;
> end;

> ... fails with the following error -

> NOTICE: PerformPortalFetch: portal "curs1" not found.

The actual name of the cursor is not "curs1" in this case, but some
internally assigned name.  You would have to pay attention to the string
returned by v_ArMaster() to know what to FETCH from.

I believe you can work around this by assigning a value to the refcursor
before you OPEN:

    declare curs1 refcursor := ''curs1'';
    begin;
    open curs1 for ...

This forces "curs1" to be the internal cursor name --- meaning you get an
error if that name is already in use for a cursor.  You pays your money
and takes your choice whether you'd rather have an autogenerated unique
name or a predictable name.

There is an example discussing this at the bottom of the plpgsql manual
section that covers cursors, but the implications are perhaps not very
obvious.

            regards, tom lane

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

Предыдущее
От: "Chris Palmer"
Дата:
Сообщение: Re: priority on a process
Следующее
От: DeJuan Jackson
Дата:
Сообщение: Re: Rule on tabel