Re: Letting a function return multiple columns instead of a single complex one

From: Jaime Casanova <systemguards(at)gmail(dot)com>
To: "A(dot)j(dot) Langereis" <a(dot)j(dot)langereis(at)inter(dot)nl(dot)net>
Cc: Postgres general mailing list <pgsql-general(at)postgresql(dot)org>
Subject: Re: Letting a function return multiple columns instead of a single complex one
Date: 2005-12-07 18:31:23
Message-ID: c2d9e70e0512071031g43ebd769jae35c24192221079@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

>
> The reason that I need this is because of my other question (is there in pg
> a function like oracle's rownum?). The function get_a_foo looks in reality a
> bit more like the next:
>
> create type foo_extended as (a int, b int, rowno int);
>
> create or replace function get_a_foo_func(int)
> returns setof foo_extended as
> '
> declare
> tmp_row foo_extended%rowtype;
> i int;
> begin
> i := 1;
>
> for tmp_row in (select * from foo where a = $1) loop
> tmp_row.rowno := i;
> return next tmp_row;
> i := i + 1;
> end loop;
>
> end;
> '
> language plpgsql volatile;
>
> create or replace function get_a_foo(int)
> returns setof foo_extended as
> '
> select * from get_a_foo_func($1);
> '
> language sql volatile;
>
> The function get_a_foo_func runs a query and adds to each row of the result
> a rownum like number. The other, wrapper, function is to make it possible to
> give a set as an imput parameter: unfortunately this is something that
> doesn't seem to be supported by pl/pgsql.
>
> Yours,
>
> Aarjan Langereis
>
>

Maybe you can do something like:

create type foo_extended as (a int, b int, rowno int);

create or replace function get_a_foo(int) returns setof foo_extended as '
create temp sequence seq1;
select *, nextval('seq1') from foo where a = $1;
' language sql volatile;

--
regards,
Jaime Casanova
(DBA: DataBase Aniquilator ;)

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Oleg Bartunov 2005-12-07 19:50:52 Re: fts, compond words?
Previous Message Mike Rylander 2005-12-07 18:20:32 Re: fts, compond words?