function with multiple return values

From: Scott Serr <serrs(at)theserrs(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: function with multiple return values
Date: 2010-11-07 07:43:37
Message-ID: 4CD658A9.10706@theserrs.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I've created the following function:

CREATE OR REPLACE FUNCTION latest ( lot_id int4,
condition text, OUT perc smallint, OUT entry_date date )
RETURNS SETOF record AS
'
BEGIN
RETURN QUERY SELECT t1.perc, t1.entry_date
FROM t1, t2
WHERE t1.condition_id=t2.id and t1.lot_id = $1 and t2.code = $2
ORDER BY entry_date DESC LIMIT 1;
END;
' language 'plpgsql' VOLATILE;

It works for this:
select (latest(38787,'IP')).*
returning perc and entry_date each in it's own column.

Problem is:
select (latest(38787,'IP')).*, (latest(38787,'FI')).*;
returns 4 columns: perc, entry_date, perc, entry_date

Tried:
select perc as p1, perc as perc2 from (
select (latest(38787,'IP')).*, (latest(38787,'FI')).*
) as foo;
just to see -- it says perc is ambiguous... well yes it is! :)

Ideas on how to uniquely name the first and second set of "perc,
entry_date"?
Or maybe there is a different way to return 2 values from a function?

Thanks,
Scott

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Anders Söderman 2010-11-07 07:52:48 Run "postgresql\9.0\data" on a RAID-1 disk on my Local Area Network (LAN)
Previous Message John R Pierce 2010-11-07 05:54:32 Re: Need magic to clean strings from unconvertible UTF8