From: Thomas Kellerer <shammat@gmx.net>
To: pgsql-general@lists.postgresql.org
Subject: Re: Multiple result set to be returned in procedure/function
Date: Thu, 19 Nov 2020 09:57:03 +0100
Message-ID: <e1ec41df-2689-7cc9-da85-6bb79f5d21b4@gmx.net> (raw)
In-Reply-To: <CA+rAWgnNUfe0_Mw=8BFUam_5swrQ331wuphJBfjUdi_PNW08vw@mail.gmail.com>
References: <CA+rAWgnNUfe0_Mw=8BFUam_5swrQ331wuphJBfjUdi_PNW08vw@mail.gmail.com>
Muthukumar.GK schrieb am 19.11.2020 um 09:27:
> is it possible to return Multiple results set from procedure/function
> on single execution. Please advise me on this. we are planning to
> migrate things from sqlserver to postgresql where my existing sql
> stored procs will return multiple result set. so we need achieve same
> thing in postgresql.
You can achieve something like that, but it's a bit cumbersome to consume/use the results:
create function get_results()
returns setof refcursor
as
$$
declare
c1 refcursor;
c2 refcursor;
begin
open c1 for select * from (values (1,2,3), (4,5,6)) as t(a,b,c);
return next c1;
open c2 for select * from (values ('one'),('two'),('three'),('four')) as p(name);
return next c2;
end;
$$
language plpgsql;
You have to turn off autocommit in order to be able to consume the results.
In psql you would get a result with two "unnamed portals" that you need to fetch
manually
arthur=> \set AUTOCOMMIT off
arthur=> select * from get_results();
get_results
--------------------
<unnamed portal 1>
<unnamed portal 2>
arthur=> fetch all in fetch all in "<unnamed portal 1>";
a | b | c
---+---+---
1 | 2 | 3
4 | 5 | 6
(2 rows)
arthur=> fetch all in fetch all in "<unnamed portal 2>";
name
-------
one
two
three
four
(4 rows)
Other SQL clients might make this a bit easier.
How exactly you deal with that in your application depends on the
programming language you use.
I would recommend to take the opportunity of the migration project
and refactor your code so that you don't need this.
Thomas
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: shammat@gmx.net
Subject: Re: Multiple result set to be returned in procedure/function
In-Reply-To: <e1ec41df-2689-7cc9-da85-6bb79f5d21b4@gmx.net>
References: <CA+rAWgnNUfe0_Mw=8BFUam_5swrQ331wuphJBfjUdi_PNW08vw@mail.gmail.com> <e1ec41df-2689-7cc9-da85-6bb79f5d21b4@gmx.net>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox