Re: PL/pgsql return resultset/cursor? - Mailing list pgsql-general

From Bruce Momjian
Subject Re: PL/pgsql return resultset/cursor?
Date
Msg-id 200203231438.g2NEcIK16492@candle.pha.pa.us
Whole thread Raw
In response to Re: PL/pgsql return resultset/cursor?  (Jan Wieck <janwieck@yahoo.com>)
List pgsql-general
Jan Wieck wrote:
> Richard Emberson wrote:
> >
> > Is it possible to return a result-set or cursor from a PL/pgsql
> > procedure, like
> >
> > CREATE OR REPLACE FUNCTION foo()
> > RETURNS <WHAT_TYPE>  AS '
> > BEGIN
> >         RETURN SELECT * from FOO;
> > END;
> > ' LANGUAGE 'plpgsql';
> >
> > If you open a cursor in a procedure, it gets closed when the procedure
> > exits, right?
>
>     Cursors  get closed in PostgreSQL when you close them or when
>     the transaction ends.
>
>     Look at the  refcursor  data  type  (new  in  v7.2)  and  use
>     transactions.

Here is a sample:

    create table aa(a int, b int, c int);
    create function f() returns refcursor as '
    declare
     r refcursor;
    begin
     open r for select * from aa;
     return r;
    end;' language 'plpgsql';

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026

pgsql-general by date:

Previous
From: Martín Marqués
Date:
Subject: Re: SQL Server performing much better?!?!
Next
From: "Clark C . Evans"
Date:
Subject: Anyone working on non-cygwin Win32 Port?