473,287 Members | 1,927 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

plpgsql: return multiple result sets

Hi all,

I'm running Postgres 7.2.1 and I need to return multiple row sets from plpgsql
function. I'm new in plpgsql but according documentation and everything I
could find in the mailing list I need to switch to 7.3 to get at least SETOF
rows as a result.

I can't really upgrade Postgres now. Is there is any a workaround idea to
retrieve multiple rowsets?

I have up to 50 tables in database to join and pass this data to the another
application
I had an idea to build a view and retrieve cursor on this view (if I stay with
7.2) or generate custom type based on the columns of all 50 tables and
retrieve a SETOF custom type (if I use 7.3)

Can anybody give me any suggestion?

Thank you in advance,

Oksana
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #1
5 9339
--- Oksana Yasynska <ok****@athabascau.ca> wrote:
Hi all,

I'm running Postgres 7.2.1 and I need to return
multiple row sets from plpgsql
function. I'm new in plpgsql but according
documentation and everything I
could find in the mailing list I need to switch to
7.3 to get at least SETOF
rows as a result.

I can't really upgrade Postgres now. Is there is any
a workaround idea to
retrieve multiple rowsets?

I have up to 50 tables in database to join and pass
this data to the another
application
I had an idea to build a view and retrieve cursor on
this view (if I stay with
7.2) or generate custom type based on the columns of
all 50 tables and
retrieve a SETOF custom type (if I use 7.3)

Can anybody give me any suggestion?


You can return a cursor from your function, which you
can then use in your application. Sort of like:

create function my_cursor_test(refcursor, integer)
returns refcursor as 'begin open $1 as cursor for
select * from mytable where id = $2; return $1; end;'
language 'plpgsql';

Then call it like:
begin;
select my_cursor_test(mycursor, 1);
select * from mycursor;
(output comes here)
end;

Note the need to wrap the statements in an explicit
transaction. With statements being autocommitted, the
cursor would be closed immediately following the
function call.

Better check the syntax too, I just dashed that off
(hey, it's Saturday). It's all there in the 7.2 docs
under "procedural languages".

__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 12 '05 #2
Jeff,

Sorry for the delay response. We have problems with our mail server and my
first mail didn't get to the list.

Thank you for the idea about cursor unfortunately it's not a right solution in
my situation.

I think I didn't explain properly what my problem is.
It's obvious that Eng is not my native language:)

I need to write a plpgsql function which returns information selected from the
50 tables (in the following example: title, descriptions and locations) to
the other application. By the way, data has a tree structure.

I have an idea to use function to build a temp table.
Is it feasible to return temporary table as a plpgsql function result ?
For example, 3 tables:
CREATE TABLE "lom" (
"id" int4 DEFAULT nextval('"lom_id_seq"'::text) NOT NULL,
"title" varchar(1000));

CREATE TABLE "description" (
"id" int4 DEFAULT nextval('"description_id_seq"'::text) NOT NULL,
"lom_id" int4,
"description" varchar(2000));

CREATE TABLE "location" (
"id" int4 DEFAULT nextval('"location_id_seq"'::text) NOT NULL,
"lom_id" int4,
"uri" varchar(1000));

With the following information:

INSERT INTO "lom" ("id", "title") VALUES(948, 'title');

INSERT INTO "description" ("id", "lom_id", "description") VALUES(564, 948,
'description1');
INSERT INTO "description" ("id", "lom_id", "description") VALUES(565, 948,
'description2');

INSERT INTO "location" ("id", "lom_id", "uri") VALUES(1258, 948,
'http://yahoo.ca - location1');
INSERT INTO "location" ("id", "lom_id", "uri") VALUES(1259, 948,
'http://google.ca - location2');

Oksana
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 12 '05 #3
Jeff,

Thank you for the idea about cursor unfortunately it's not a right solution in
my situation.

I think I didn't explain properly what my problem is.
It's obvious that Eng is not my native language:)

I need to write a plpgsql function which returns information selected from the
50 tables (in the following example: title, descriptions and locations) to
the other application. By the way, data has a tree structure.

I have an idea to use function to build a temp table.
Is it feasible to return temporary table as a plpgsql function result ?
For example, 3 tables:
CREATE TABLE "lom" (
"id" int4 DEFAULT nextval('"lom_id_seq"'::text) NOT NULL,
"title" varchar(1000));

CREATE TABLE "description" (
"id" int4 DEFAULT nextval('"description_id_seq"'::text) NOT NULL,
"lom_id" int4,
"description" varchar(2000));

CREATE TABLE "location" (
"id" int4 DEFAULT nextval('"location_id_seq"'::text) NOT NULL,
"lom_id" int4,
"uri" varchar(1000));

With the following information:

INSERT INTO "lom" ("id", "title") VALUES(948, 'title');

INSERT INTO "description" ("id", "lom_id", "description") VALUES(564, 948,
'description1');
INSERT INTO "description" ("id", "lom_id", "description") VALUES(565, 948,
'description2');

INSERT INTO "location" ("id", "lom_id", "uri") VALUES(1258, 948,
'http://yahoo.ca - location1');
INSERT INTO "location" ("id", "lom_id", "uri") VALUES(1259, 948,
'http://google.ca - location2');

Oksana
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 12 '05 #4
--- Oksana Yasynska <ok****@athabascau.ca> wrote:
I need to write a plpgsql function which returns
information selected from the
50 tables (in the following example: title,
descriptions and locations) to
the other application. By the way, data has a tree
structure.

I have an idea to use function to build a temp
table.
Is it feasible to return temporary table as a
plpgsql function result ?
Yes. This was a commonly used workaround before
version 7.2.x. A couple of points to be aware of:
* A temporary table persists only for (and is only
available to) the current user session.
* Better to use "EXECUTE" to create the table, as
there are some potential gotchas with tables created
by a function. Check the archives for plenty of
examples.


For example, 3 tables:
CREATE TABLE "lom" (
"id" int4 DEFAULT nextval('"lom_id_seq"'::text)
NOT NULL,
"title" varchar(1000));

CREATE TABLE "description" (
"id" int4 DEFAULT
nextval('"description_id_seq"'::text) NOT NULL,
"lom_id" int4,
"description" varchar(2000));

CREATE TABLE "location" (
"id" int4 DEFAULT
nextval('"location_id_seq"'::text) NOT NULL,
"lom_id" int4,
"uri" varchar(1000));

With the following information:

INSERT INTO "lom" ("id", "title") VALUES(948,
'title');

INSERT INTO "description" ("id", "lom_id",
"description") VALUES(564, 948,
'description1');
INSERT INTO "description" ("id", "lom_id",
"description") VALUES(565, 948,
'description2');

INSERT INTO "location" ("id", "lom_id", "uri")
VALUES(1258, 948,
'http://yahoo.ca - location1');
INSERT INTO "location" ("id", "lom_id", "uri")
VALUES(1259, 948,
'http://google.ca - location2');

Oksana

__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 12 '05 #5
Jeff,

thank you for the time and suggestion.
I'm also trying to use SETOF custom_type as a solution

Oksana

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stanley Sinclair | last post by:
I have a need to return multiple result sets from a stored procedure. Want that SP to call others to get the data. Win2003, db2 8.1.5. Can't figure out how to handle open cursors, and return...
0
by: Randy Foster | last post by:
We have a stored procedure (on DB2 8.1 FP5) that returns multiple result sets which we are calling from Java (WebSphere 5.0.2.8). We can call the stored procedure from the DB2 command line...
15
by: AussieRules | last post by:
Hi, I have a need to do two selects against to stored proc's in my SQL db. At the moment, each SP is called and two different dataset are populate. Thats two round trips to the SQL server. ...
2
by: situ | last post by:
look at the procedure below CREATE PROCEDURE PR_2 ( ) dynamic result sets 1 BEGIN declare cursor_1 cursor with return to caller for select *from elements; open cursor_1; END
4
by: Liz | last post by:
Anyone have any info on how you can fill a DataSet with mutiple result sets from a SQL batch ? I know with a dataReader you can use the nextResult method but I'm lost on how you do this with a...
0
by: prad | last post by:
Hi, Following java code returns 24 result sets.It counts number of rows in the first result set correctly.But doesnt count rows from next result set. When I debugged the code I found out that...
1
by: Mike | last post by:
Is it possible to use a SqlDataSource to return multiple result sets and if so, how do I access them? Thanks
3
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
I am wondering if someone could provide a code sample (or just a suggested approach) on how to fill a single DataGridView with multiple result sets. For example, this statement returns a one-line...
0
by: anilkodali | last post by:
How to compare multiple result sets with a set of values? Here is the scenario.. My query returns me multiple results(one column of data) and I want compare all the data at once with a set of...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.