Re: Return unknown resultset from a function - Mailing list pgsql-general

From Rory Campbell-Lange
Subject Re: Return unknown resultset from a function
Date
Msg-id 20120304205809.GA11451@campbell-lange.net
Whole thread Raw
In response to Re: Return unknown resultset from a function  (Rory Campbell-Lange <rory@campbell-lange.net>)
List pgsql-general
On 04/03/12, Rory Campbell-Lange (rory@campbell-lange.net) wrote:
> On 04/03/12, Jan Meyland Andersen (jma@agile.dk) wrote:

> > My main problem is that I do not know how many columns or the data
> > type of the columns before runtime.
> > It this possible at all?

> There is a section on this in the docs at (for instance)
> http://www.postgresql.org/docs/8.4/static/xfunc-sql.html#XFUNC-OUTPUT-PARAMETERS
>
> A specific example which may help is
>
> CREATE FUNCTION new_emp() RETURNS emp AS $$
>     SELECT ROW('None', 1000.0, 25, '(2,2)')::emp;
> $$ LANGUAGE SQL;
>
> Note that there are two ways of calling such a function. You probably
> want the "SELECT *" form.

My apologies -- if you aren't sure about the return type you will need
to use a RECORD.

eg http://www.postgresql.org/docs/7.4/static/plpgsql-declarations.html#PLPGSQL-DECLARATION-RECORDS

There is a good answer on stack overflow about returning polymorphic
results here:
http://stackoverflow.com/questions/4547672/how-to-return-multiple-fields-as-a-record-in-postgresql-pl-pgsql
--
Rory Campbell-Lange
rory@campbell-lange.net

Campbell-Lange Workshop
www.campbell-lange.net
0207 6311 555
3 Tottenham Street London W1T 2AF
Registered in England No. 04551928

pgsql-general by date:

Previous
From: Rory Campbell-Lange
Date:
Subject: Re: Return unknown resultset from a function
Next
From: Chris Angelico
Date:
Subject: atoi-like function: is there a better way to do this?