Re: plpgsql: returning multiple named columns from function

From: Tony Caduto <tony_caduto(at)amsoftwaredesign(dot)com>
To: John Lawler <postgresql(dot)org(at)tgice(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: plpgsql: returning multiple named columns from function
Date: 2005-08-23 20:06:43
Message-ID: 430B81D3.4020402@amsoftwaredesign.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

you can do this with a function that returns a refcursor.
(lookup refcursor in the docs)

you would call it something like this

select mycursorfunct();
fetch all from return_cursor;

In this example I hardcode the name return cursor and then call both
lines from a transaction.

you could also retrieve the name of the cursor into a variable, then do
something like(this is delphi code)

connection.starttransaction;
try
query1.sql.add('select mycursorfunct();');
query1.open;
refcursorname:= query1.fieldbyname('mycursofunct').asstring;
query1.close;
query1.sql.add('fetch all from '+refcursorname);
query1.open;

finally
connection.commit;
end;

You won't be able to do it exactly like M$ SQL server, but you can do
something equivelent with a couple extra lines of code.

A refcursor takes a couple of more lines of code on the client, but you
don't have to use a type or a record.

If you need a actual test function, let me know.

hope this helps,

Tony Caduto
http://www.amsoftwaredesign.com
Home of PG Lightning Admin for Postgresql 8.x

>
>
> CREATE PROCEDURE test(
> @lookup char(50))
> WITH ENCRYPTION AS BEGIN
>
> -- ... a bunch of code to do some lookup, and then ...
>
> SELECT
> @Result1 AS Result1,
> @Result2 AS Result2,
> @Result3 AS Result3,
> @Result4 AS Result4
>
> END
> GO
>
>
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Roger Hand 2005-08-23 20:46:51 Re: plpgsql: returning multiple named columns from function *simply*
Previous Message Alvaro Herrera 2005-08-23 19:55:18 Re: ctid access is slow