knowledge-database (beta)

Current group: pgsql.sql

returning a record from PL/pgSQL

returning a record from PL/pgSQL  
KÖPFERL_Robert
 Re: returning a record from PL/pgSQL  
Michael Fuhr
From:KÖPFERL_Robert
Subject:returning a record from PL/pgSQL
Date:Fri, 21 Jan 2005 12:41:09 +0100
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.

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly
From:Michael Fuhr
Subject:Re: returning a record from PL/pgSQL
Date:Sun, 23 Jan 2005 12:00:25 -0700
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/

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
   

Copyright © 2006 knowledge-database   -   All rights reserved