Re: [Fwd: Returning multiple rows in 8.4]

From: Alban Hertroys <dalroi(at)solfertje(dot)student(dot)utwente(dot)nl>
To: Baris Gerze <barisgerze(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: [Fwd: Returning multiple rows in 8.4]
Date: 2009-11-10 10:53:51
Message-ID: C88D512B-9885-49AB-A539-590DF02BFEE1@solfertje.student.utwente.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 9 Nov 2009, at 23:30, Baris Gerze wrote:
> I have users table such as
> create table users(
> user_id bigserial,
> user_name varchar(50),
> user_surname varchar(100)
> );
>
>
> how can I write a function to return multiple rows? ( in plpgsql )

You can create set-returning functions (http://www.postgresql.org/docs/8.4/interactive/xfunc-sql.html#XFUNC-SQL-FUNCTIONS-RETURNING-SET
).

> what is the return value of the function? records?

That's up to you, but most likely you want to return a type that you
defined somewhere, that being a table you created or a custom type.

You probably want "returns setof users". You can also use "returns
setof record", but then you'll have to specify what that record
consists of in every query that uses your function.

> and how can i run it without running as select my_func() as (int8,
> varchar) ???
> is there simple way to do this?

Set returning functions are queried as select * from my_func();
If you return a set of records then you'll have to specify it's type
the way you mention above.

> thanks

You're welcome.

Alban Hertroys

--
Screwing up is the best way to attach something to the ceiling.

!DSPAM:737,4af9464211073046225299!

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Alban Hertroys 2009-11-10 11:03:58 Re: Incremental Backups in postgres
Previous Message Alexandra Roy 2009-11-10 10:37:25 Re: PostgreSQL 8.3.8 on AIX5.3 : compilation failed