Re: Stored Procedure / function and their result

From: "Albe Laurenz" <all(at)adv(dot)magwien(dot)gv(dot)at>
To: "Alain Roger *EXTERN*" <raf(dot)news(at)gmail(dot)com>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: Stored Procedure / function and their result
Date: 2007-03-20 07:47:19
Message-ID: AFCCBB403D7E7A4581E48F20AF3E5DB201C43AE4@EXADV1.host.magwien.gv.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Alain Roger wrote:
> I would like to know if there is a better way how to retrieve
> result from a stored procedure (function) than to use 'AS
> res(col1 varchar, col2 timestamp,..)'
>
> for example, here is a stored procedure :
> CREATE OR REPLACE FUNCTION SP_A_003(username VARCHAR)
> RETURNS SETOF RECORD AS
[...]

Yes, there are two ways to avoid this.

1.) define a composite type:

CREATE TYPE sp_a_result (
name varchar,
firstname varchar,
userlogin varchar,
statustype varchar
);

CREATE OR REPLACE FUNCTION SP_A_003 (username VARCHAR)
RETURNS SETOF sp_a_result AS ...

2.) Use output parameters (for Versions >= 8.1):

CREATE OR REPLACE FUNCTION SP_A_003 (
username IN VARCHAR,
name OUT varchar,
firstname OUT varchar,
userlogin OUT varchar,
statustype OUT varchar
) RETURNS SETOF record AS ...

You can find a more verbose description in
http://www.postgresql.org/docs/current/static/xfunc-sql.html

Yours,
Laurenz Albe

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Poornima 2007-03-20 09:28:41 Job Opening for PostgreSQL DBA in Pune.
Previous Message Richard Huxton 2007-03-20 07:35:36 Re: Optimizing warm standby. Will this work? [PITR WAL]