Re: pl/pgsql how to return multiple values from a function

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: jack <datactrl(at)tpg(dot)com(dot)au>
Cc: <pgsql-sql(at)postgresql(dot)org>
Subject: Re: pl/pgsql how to return multiple values from a function
Date: 2003-03-09 08:51:00
Message-ID: 20030309004652.X14723-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql


On Sun, 9 Mar 2003, jack wrote:

> Hi, all
>
> I use "RETURNS RECORD" to return a multiple values from a function as
> following but it doesn't work at all. Or is there any other way to do it?
> Thank you jin advance!

It's easier if you define a composite type to hold the return type,
something like:
CREATE TYPE tesp_return1 AS (jj int, ss varchar(10));

Then have tesp_returnRec1() return a tesp_return1 and tesp_returnrec2
changes a little bit as well, rec becomes of type tesp_return1%ROWTYPE
and you want to select into using SELECT INTO rec * from tesP_returnRec1()
I believe.

If you do it all with RECORD, you don't need to define the type, change
the return type or type of rec, but you need to put the type information
on the select into, something like:
SELECT INTO rec * from tesP_returnRec1() as foo(jj int, ss varchar(10));

> CREATE OR REPLACE FUNCTION tesP_returnRec2( ) RETURNS INTEGER AS'
> DECLARE
> rec RECORD;
> BEGIN
> SELECT INTO rec tesP_returnRec1();
> RAISE NOTICE ''jj=%, ss=%'', rec.jj,rec.ss;
> RETURN 0;
> END;'
> LANGUAGE 'PLPGSQL';
> ;-------
> CREATE OR REPLACE FUNCTION tesP_returnRec1( ) RETURNS RECORD AS'
> DECLARE
> rec RECORD;
> BEGIN
> SELECT INTO rec
> CAST(100 AS INTEGER) AS jj,
> CAST(''ABC'' AS VARCHAR(10)) AS ss;
> RETURN rec;
> END;'
> LANGUAGE 'PLPGSQL';
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
> (send "unregister YourEmailAddressHere" to majordomo(at)postgresql(dot)org)
>

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Jonathan Weiss 2003-03-09 15:14:48 check if table/object is locked
Previous Message Rajesh Kumar Mallah 2003-03-09 05:59:28 Re: pl/pgsql how to return multiple values from a function