473,396 Members | 2,030 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,396 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 9352
--- 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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.