Skip site navigation (1) Skip section navigation (2)

Re: function returning result set of varying column

From: "Pavel Stehule" <pavel(dot)stehule(at)gmail(dot)com>
To: "maria s" <psmg01(at)gmail(dot)com>
Cc: "Ivan Sergio Borgonovo" <mail(at)webthatworks(dot)it>, "Bart Degryse" <Bart(dot)Degryse(at)indicator(dot)be>, pgsql-sql(at)postgresql(dot)org
Subject: Re: function returning result set of varying column
Date: 2008-06-03 13:54:41
Message-ID: 162867790806030654y45e36080lf016285049044d7a@mail.gmail.com (view raw or flat)
Thread:
Lists: pgsql-sql
2008/6/3 maria s <psmg01(at)gmail(dot)com>:
> Thanks for all your replies.
>
> Actually I don't know the number of columns that I am going to return.
>
> I have 2 tables. For a single entry E1  in one table(t1), I have to fetch
> all the matching entries for E1 from the other table(t2),  K1,..Kn.
> and finally the function should return E1, K1..Kn. So I don't know the
> number of columns that I am going to get.
>
> Is it possible to write a function that returns this kind of result?

no, it's not possible. You have to know number and types of result
columns before function's execution.

try to use arrays.

Regards
Pavel Stehule
>
> Please help.
>
> Thanks,
> maria
>
> On Tue, Jun 3, 2008 at 9:28 AM, Ivan Sergio Borgonovo <mail(at)webthatworks(dot)it>
> wrote:
>>
>> On Tue, 3 Jun 2008 09:01:02 -0400
>> "maria s" <psmg01(at)gmail(dot)com> wrote:
>>
>> > Hi Friends,
>> > Thanks for all your for the reply.
>> >
>> > I tried the function and when I execute it using
>> > select * from myfunction()
>> > it says
>> > ERROR:  a column definition list is required for functions
>> > returning "record"
>> >
>> > Could you please help me to fix this error?
>> >
>> > Thanks so much for your help.
>>
>> you can specify the returned types in each statement that call your
>> function or you can specify the returned type in the function itself.
>>
>> CREATE OR REPLACE FUNCTION myfunction(out col1 int, out col2
>> varchar(32), out ...)
>> RETURNS
>> SETOF
>> RECORD
>> AS
>> $body$
>> DECLARE
>>  rec record;
>> BEGIN
>>  FOR rec IN (
>>    SELECT * FROM sometable)
>>  LOOP
>>    col1:=rec.col1;
>>    col2:=rec.col2;
>> --    col3:=...;
>>    RETURN NEXT;
>>  END LOOP;
>>  RETURN;
>> END;
>> $body$
>>
>> > > CREATE OR REPLACE FUNCTION myfunction() RETURNS SETOF RECORD AS
>> > > $body$
>> > > DECLARE
>> > >   rec record;
>> > > BEGIN
>> > >   FOR rec IN (
>> > >     SELECT * FROM sometable)
>> > >   LOOP
>> > >     RETURN NEXT rec;
>> > >   END LOOP;
>> > >   RETURN;
>> > > END;
>> > > $body$
>>
>> --
>> Ivan Sergio Borgonovo
>> http://www.webthatworks.it
>>
>>
>> --
>> Sent via pgsql-sql mailing list (pgsql-sql(at)postgresql(dot)org)
>> To make changes to your subscription:
>> http://www.postgresql.org/mailpref/pgsql-sql
>
>

In response to

pgsql-sql by date

Next:From: Adrian KlaverDate: 2008-06-03 13:57:17
Subject: Re: cross-database references are not implemented
Previous:From: Pavel StehuleDate: 2008-06-03 13:51:03
Subject: Re: cross-database references are not implemented

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group