Skip site navigation (1) Skip section navigation (2)

Re: [GENERAL] PL/pgsql return resultset/cursor?

From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Richard Emberson <emberson(at)phc(dot)net>
Cc: PostgreSQL-documentation <pgsql-docs(at)postgresql(dot)org>
Subject: Re: [GENERAL] PL/pgsql return resultset/cursor?
Date: 2002-04-09 03:34:15
Message-ID: 200204090334.g393YFE14191@candle.pha.pa.us (view raw or flat)
Thread:
Lists: pgsql-docs
I have added an FAQ item and improved the documentation.  Seems the
official current docs aren't being built regularly, (last April 3), so I
pointed to my web copy in the FAQ:
	
	4.25) How do I return multiple rows or columns from a function?
	
	You can return result sets from PL/pgSQL functions using refcursors. See
	http://candle.pha.pa.us/main/writings/pgsql/sgml/plpgsql-cursors.html,
	section 23.7.3.3.


---------------------------------------------------------------------------

Richard Emberson wrote:
> Thanks for the example. Whoever maintains the documentation ought to put such
> an
> example in the refcursor section.
> 
> Richard
> 
> 
> Bruce Momjian wrote:
> 
> > 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(at)candle(dot)pha(dot)pa(dot)us               |  (610) 853-3000
> >   +  If your life is a hard drive,     |  830 Blythe Avenue
> >   +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026
> >
> > ---------------------------(end of broadcast)---------------------------
> > TIP 1: subscribe and unsubscribe commands go to majordomo(at)postgresql(dot)org
> 
> 

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

Responses

pgsql-docs by date

Next:From: Bruce MomjianDate: 2002-04-09 04:18:13
Subject: Re: [GENERAL] PL/pgsql return resultset/cursor?
Previous:From: Viktor VislobokovDate: 2002-04-04 04:21:02
Subject: Re: Upgrading - How?

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group