Re: PL/PGSQL Record type question - Mailing list pgsql-sql

From imad
Subject Re: PL/PGSQL Record type question
Date
Msg-id 1f30b80c0705111119v7b0520ebgbda9ae7e07259bc5@mail.gmail.com
Whole thread Raw
In response to Re: PL/PGSQL Record type question  (Robins <tharakan@gmail.com>)
List pgsql-sql
On 5/11/07, Robins <tharakan@gmail.com> wrote:
> Hi Gabriel,
>
> There are two ways to do this:
> 1. Imad's way (Define the function with the return type as RECORD). Its only
> problem is that while querying from this function, you need to give a proper
> SELECT query or else PG returns an error.

Yeah ... valid point.

>
> e.g.  As Imad gives in his example ...
>
> CREATE FUNCTION xyz() RETURNS record AS
> $$
>  declare
>   abc RECORD;
>  begin
>   abc := (1, 2);
>   return abc;
>  end;
>  $$
> language plpgsql;
>
> And execute the function in this fashion:
>
> select a, b from xyz() as (a int, b int);
>
> The only problem with this is that if you have 6 elements your select
> statement becomes quite long. Also, in case your function return parameter
> count changes, or its types change, you would need to change the SELECT SQL
> at all the places.
>
> 2. Define a TYPE as John mentioned, and set the function's return type as
> this TYPE. The advantage is that you can always redefine the function and
> the type in case the return parameters are changing and that your select
> statement is a simple SELECT * from fn().

The drawback is that you need to know the complete definition in
advance. In contrast, you can assign any type of row to a RECORD
variable ... much more flexible.


--Imad
www.EnterpriseDB.com


pgsql-sql by date:

Previous
From: Robins
Date:
Subject: Re: PL/PGSQL Record type question
Next
From: "Collin Peters"
Date:
Subject: Re: Convert serial column to regular integer