473,756 Members | 4,863 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9400
--- Oksana Yasynska <ok****@athabas cau.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_i d_seq"'::text) NOT NULL,
"title" varchar(1000));

CREATE TABLE "descriptio n" (
"id" int4 DEFAULT nextval('"descr iption_id_seq"' ::text) NOT NULL,
"lom_id" int4,
"descriptio n" varchar(2000));

CREATE TABLE "location" (
"id" int4 DEFAULT nextval('"locat ion_id_seq"'::t ext) NOT NULL,
"lom_id" int4,
"uri" varchar(1000));

With the following information:

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

INSERT INTO "descriptio n" ("id", "lom_id", "descriptio n") VALUES(564, 948,
'description1') ;
INSERT INTO "descriptio n" ("id", "lom_id", "descriptio n") 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_i d_seq"'::text) NOT NULL,
"title" varchar(1000));

CREATE TABLE "descriptio n" (
"id" int4 DEFAULT nextval('"descr iption_id_seq"' ::text) NOT NULL,
"lom_id" int4,
"descriptio n" varchar(2000));

CREATE TABLE "location" (
"id" int4 DEFAULT nextval('"locat ion_id_seq"'::t ext) NOT NULL,
"lom_id" int4,
"uri" varchar(1000));

With the following information:

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

INSERT INTO "descriptio n" ("id", "lom_id", "descriptio n") VALUES(564, 948,
'description1') ;
INSERT INTO "descriptio n" ("id", "lom_id", "descriptio n") 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****@athabas cau.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_i d_seq"'::text)
NOT NULL,
"title" varchar(1000));

CREATE TABLE "descriptio n" (
"id" int4 DEFAULT
nextval('"descr iption_id_seq"' ::text) NOT NULL,
"lom_id" int4,
"descriptio n" varchar(2000));

CREATE TABLE "location" (
"id" int4 DEFAULT
nextval('"locat ion_id_seq"'::t ext) NOT NULL,
"lom_id" int4,
"uri" varchar(1000));

With the following information:

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

INSERT INTO "descriptio n" ("id", "lom_id",
"descriptio n") VALUES(564, 948,
'description1') ;
INSERT INTO "descriptio n" ("id", "lom_id",
"descriptio n") 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*******@postg resql.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
8681
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 >1 result sets. Thought about global temp tables.
0
2540
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 successfully. When we call it from WebSphere, we get the following error: Execution failed due to a distribution protocol error that caused deallocation of the conversation. A DRDA Data Stream Syntax Error was
15
6511
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. I can combine the two SP into one, and have one SP that executes two Select Statements, and effectivly returns two result sets, and I could call this with one trip to the SQL server from my VB app.
2
3360
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
10932
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 dataAdapter .... TIA .. L
0
2959
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 first time it enters in the loop while(rs.next()).But from the next result set it doesn't enter that loop as rs.next() returns false.DB2 is used as backend. thanx 4 help. import java.sql.*; import java.io.*; public class exportdds33 { static...
1
2383
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
6234
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 result set for each table in the database: EXEC sp_MSforeachtable @command1="EXEC sp_spaceused '?'" It would be handy to be able to put them all into one DataGridView. I would like to be able to handle an arbitrary SQL statement; above is just...
0
2326
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 data. For example my query returns 1,2,3 and I want compare the result set with (1,3), can I do that using a query with out using stored procedures? Obviously using the in clause isn't working(i.e., (1,2,3) in (1,3)), any other alternative. Thanks,
0
9325
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9716
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9716
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9571
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8569
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7116
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6410
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3185
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2542
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.