pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
returning a record from PL/pgSQL
2+ messages / 2 participants
[nested] [flat]

* returning a record from PL/pgSQL
@ 2005-01-21 11:41 KÖPFERL Robert <robert.koepferl@sonorys.at>
  2005-01-23 19:00 ` Re: returning a record from PL/pgSQL Michael Fuhr <mike@fuhr.org>
  0 siblings, 1 reply; 2+ messages in thread

From: KÖPFERL Robert @ 2005-01-21 11:41 UTC (permalink / raw)
  To: ; +Cc: pgsql-sql

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.



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: returning a record from PL/pgSQL
  2005-01-21 11:41 returning a record from PL/pgSQL KÖPFERL Robert <robert.koepferl@sonorys.at>
@ 2005-01-23 19:00 ` Michael Fuhr <mike@fuhr.org>
  0 siblings, 0 replies; 2+ messages in thread

From: Michael Fuhr @ 2005-01-23 19:00 UTC (permalink / raw)
  To: KÖPFERL Robert <robert.koepferl@sonorys.at>; +Cc: 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/



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2005-01-23 19:00 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-01-21 11:41 returning a record from PL/pgSQL KÖPFERL Robert <robert.koepferl@sonorys.at>
2005-01-23 19:00 ` Michael Fuhr <mike@fuhr.org>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox