Re: returning a record from PL/pgSQL

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: KÖPFERL Robert <robert(dot)koepferl(at)sonorys(dot)at>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: returning a record from PL/pgSQL
Date: 2005-01-23 19:00:25
Message-ID: 20050123190024.GA18329@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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/

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Ryan Miranda 2005-01-23 22:59:36 Invalid Input syntax for type bigint
Previous Message Klaus W. 2005-01-23 17:23:15 How to update dependent tables AND design considerations