Re: plpgsql: returning multiple named columns from function - Mailing list pgsql-general

From Tony Caduto
Subject Re: plpgsql: returning multiple named columns from function
Date
Msg-id 430B81D3.4020402@amsoftwaredesign.com
Whole thread Raw
In response to plpgsql: returning multiple named columns from function *simply*  (John Lawler <postgresql.org@tgice.com>)
List 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
>
>
>


pgsql-general by date:

Previous
From: Alvaro Herrera
Date:
Subject: Re: ctid access is slow
Next
From: "Roger Hand"
Date:
Subject: Re: plpgsql: returning multiple named columns from function *simply*