Re: FUNCTION problem

From: Adrian Klaver <aklaver(at)comcast(dot)net>
To: pgsql-sql(at)postgresql(dot)org
Cc: peterw(at)borstad(dot)com
Subject: Re: FUNCTION problem
Date: 2009-04-03 14:12:38
Message-ID: 200904030712.38321.aklaver@comcast.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Friday 03 April 2009 6:51:05 am Adrian Klaver wrote:
> On Thursday 02 April 2009 6:16:44 pm Adrian Klaver wrote:
> > Now I remember. Its something that trips me up, the RECORD in RETURN
> > setof RECORD is not the same thing as the RECORD in DECLARE RECORD. See
> > below for a better explanation-
> > http://www.postgresql.org/docs/8.3/interactive/plpgsql-declarations.html#
> >PL PGSQL-DECLARATION-RECORDS Note that RECORD is not a true data type,
> > only a placeholder. One should also realize that when a PL/pgSQL function
> > is declared to return type record, this is not quite the same concept as
> > a record variable, even though such a function might use a record
> > variable to hold its result. In both cases the actual row structure is
> > unknown when the function is written, but for a function returning record
> > the actual structure is determined when the calling query is parsed,
> > whereas a record variable can change its row structure on-the-fly.
> >
> >
> >
> > --
> > Adrian Klaver
> > aklaver(at)comcast(dot)net
>
> For this particular case the following works.
>
> CREATE OR REPLACE FUNCTION test_function(integer) RETURNS record
> AS $Body$
> DECLARE croid integer;
> DECLARE R RECORD;
> BEGIN
> SELECT INTO croid 2;
> SELECT INTO R croid,$1;
> RETURN R;
> END;
>
> $Body$
> LANGUAGE plpgsql;
>
> --
> Adrian Klaver
> aklaver(at)comcast(dot)net

Forgot to show how to call it.

test=# SELECT * from test_function(1) as test(c1 int,c2 int);
c1 | c2
----+----
2 | 1
(1 row)

--
Adrian Klaver
aklaver(at)comcast(dot)net

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Peter Willis 2009-04-03 15:29:03 Re: FUNCTION problem
Previous Message Adrian Klaver 2009-04-03 13:51:05 Re: FUNCTION problem