Re: return two elements

From: "Franco Bruno Borghesi" <fborghesi(at)gmail(dot)com>
To: <"Rodrguez Rodrguez"(at)svr1(dot)postgresql(dot)org>, "Pere" <prr(at)hosppal(dot)es>, "IMB Recipient 1" <mspop3connector(dot)gfnobrega(at)Planae2004(dot)local>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: return two elements
Date: 2005-06-06 15:15:19
Message-ID: 000401c56aaa$8d5d8c70$7e00a8c0@Planae2004.local
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

You could use your own type, since it seems you know what values you'll be storing there. You can create it like this:

CREATE TYPE MyResult AS (
dt_inici DATE,
dt_fi DATE
);

Then you must change your functions as follows:

CREATE OR REPLACE FUNCTION test1() RETURNS MyResult AS '
declare
r MyResult;
begin
r.dt_inici:=\'01/01/2005\'::Date;
r.dt_fi:=\'02/02/2005\'::Date;
RETURN r;
end; '
LANGUAGE 'plpgsql' VOLATILE;

CREATE OR REPLACE FUNCTION test2() RETURNS date AS '
declare
r MyResult;
begin
r:=test1();
RETURN r.dt_inici;
end; '
LANGUAGE 'plpgsql' VOLATILE;

To me, using a TYPE seems clearer than using an array in this case.

2005/6/6, "Rodríguez Rodríguez, Pere" <prr(at)hosppal(dot)es>:

Hello,

how can I write a function that return two or more elements?
I try to return a record but after I don't know how to assign returned value to a variable. For example,

CREATE OR REPLACE FUNCTION test1() RETURNS record AS '
declare
rec record;
begin
select into rec \'01/01/2005\'::Date as dt_inici, \'02/02/2005\'::Date as dt_fi;
return rec;
end; '
LANGUAGE 'plpgsql' VOLATILE;

CREATE OR REPLACE FUNCTION test2() RETURNS date AS '
declare
rec record;
begin
-- rec := test1(); << ERROR: syntax error at or near "rec"
select into rec test1();
return rec.dt_inici; << ERROR: record "rec" has no field "dt_inici"
end; '
LANGUAGE 'plpgsql' VOLATILE;

Can I return an array?, and how I take returned array?
I search postgres documentation but I don't find it

Thanks in advance,

pere

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Phil Endecott 2005-06-06 15:16:22 Re: CPU-intensive autovacuuming
Previous Message Tom Lane 2005-06-06 15:09:38 Re: return two elements