Thread: returning a record from PL/pgSQL

returning a record from PL/pgSQL

From
KÖPFERL Robert
Date:
I just tried hard to return
a single record fromout a plpgsql-function. While the (otherwise excelent)
documentation didn't give me an answer, I found out that this works:
       select into ret false, balance, balance;       return ret;

while ret is a composite type.

This construction however tastes not good to me. Is there a nicer way?

An example for PL/pgSQL which returns not just a scalar but also a composite
type should be added to the documentation. i.e. as second part of the RETURN
statement.


Re: returning a record from PL/pgSQL

From
Michael Fuhr
Date:
On Fri, Jan 21, 2005 at 12:41:09PM +0100, KÖPFERL Robert wrote:
>
> I just tried hard to return
> a single record fromout a plpgsql-function. While the (otherwise excelent)
> documentation didn't give me an answer, I found out that this works:
> 
>         select into ret false, balance, balance;
>         return ret;
> 
> while ret is a composite type.
>
> This construction however tastes not good to me. Is there a nicer way?

In the "Declarations" section of the PL/pgSQL documentation, under
"Row Types," is the following:
   The individual fields of the row value are accessed using the   usual dot notation, for example rowvar.field.

and under "RETURN" in the "Control Structures" section is this:
   To return a composite (row) value, you must write a record or   row variable as the expression.

So you could to the following:
   ret.field1 := value1;   ret.field2 := value2;   ret.field3 := value3;   RETURN ret;

Internally, however, each expression in the above assignments would
be evaluated using a SELECT statement, so whether this code is
"nicer" than what you wrote depends on what you mean by "nice."

-- 
Michael Fuhr
http://www.fuhr.org/~mfuhr/