Re: PLPGSQL func. defn. for returning resultset?

From: Roberto Mello <rmello(at)cc(dot)usu(dot)edu>
To: Otis Gospodnetic <otisg(at)ivillage(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: PLPGSQL func. defn. for returning resultset?
Date: 2002-02-25 14:55:12
Message-ID: 20020225145512.GB24707@cc.usu.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Sun, Feb 24, 2002 at 03:18:54PM -0500, Otis Gospodnetic wrote:
> Hello,
>
> I need to write some functions/stored procedures in PLPGSQL.
> All examples have a return clause that returns a single item (e.g.
> RETURN INTEGER or RETURN VARCHAR, etc.).
>
> However, I need to return a result set (e.g. SELECT user_id, name FROM
> users). What do I specify in the return part of the FUNCTION definition
> for that?
> CREATE FUNCTION foo() RETURNS ????? AS ' .....
> What do I put in place of ?????

If you're creating a SQL function it should be "setof" as described in the
reference for the "CREATE FUNCTION" command. All other types of function
use "refcursor", as described in the 7.2 and beyond documentation.

"setof" works only for SQL functions and works in 7.1 (and I think 7.0 as well).
"refcursor" only works for 7.2 and above.

Let me paste what I replied to a question like this a couple days ago (BTW,
it's a good idea to search the list archives at http://fts.postgresql.org/)

See this:
http://developer.postgresql.org/docs/postgres/plpgsql-cursors.html

Especially the section "Using Cursors". This is the 7.3devel
documentation, but this section should work just as well for 7.2 (this
WON'T work for 7.1 and below).

-Roberto

--
+----| http://fslc.usu.edu/ USU Free Software & GNU/Linux Club |------+
Roberto Mello - Computer Science, USU - http://www.brasileiro.net/
http://www.sdl.usu.edu/ - Space Dynamics Lab, Developer
My last original thought died of loneliness.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Roberto Mello 2002-02-25 14:59:57 Re: returning a record from a function?
Previous Message Dan Langille 2002-02-25 05:30:59 processing faster with plpgsql by passing in more data