If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Returning Records from Stored Procedures

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-23-04, 05:07
KimballJohnson KimballJohnson is offline
Registered User
 
Join Date: Aug 2004
Posts: 51
Returning Records from Stored Procedures

I have to write some complex logic in stored procedures and I see that the 'common table' syntax in db2 sql would be helpful.

But I don't get how the DECLARE CURSOR RETURN FOR CLIENT and
OPEN CURSOR syntax requirements are going to work out for this.

How do I open temporary/common tables in a stored procedure and then get them into a cursor and return the records?

For me the goal would be to flexibly create a recordset/table on the fly from a range of sources and techniques and then return the version of it that I want to define as the 'final result'.

So again, how does the OPEN CURSOR syntax work?

If I just start by trying to create a table like:

WITH TEMP1 AS
(SELECT SOME STUFF);

I immediately wonder what I'm doing.

Thanks,

Kimball
Reply With Quote
  #2 (permalink)  
Old 08-23-04, 08:34
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
In order to create, fill, the select and return data from a temporary table, you need to perform a little trick to get around the syntax.

Create procedure myproc(...)
...
DECLARE GLOBAL TEMPORARY TABLE fred ...

INSERT INTO SESSION.FRED ...

...

BEGIN
DECLARE CURSOR1 CURSOR WITH RETURN TO CLIENT FOR SELECT * from SESSION.FRED;

OPEN CURSOR1;

END;

END

HTH

Andy
Reply With Quote
  #3 (permalink)  
Old 08-23-04, 09:44
KimballJohnson KimballJohnson is offline
Registered User
 
Join Date: Aug 2004
Posts: 51
That's It!

So I can see how that would work, and I recognize that I can look up a lot of the details in the docs.

But I read somewhere you can create joins and such with the global tables.

So how about this?

DECLARE GLOBAL TABLE FRED;
INSERT INTO SESSION.FRED SELECT * FROM tblFriends;

DECLARE GLOBAL TABLE FRED2 ;
INSERT INTO SESSION.FRED2 SELECT * FROM tblFoes;

DECLARE GLOBAL TABLE WARFRED;
INSERT INTO SESSION.WARFRED SELECT FRIENDS.*, FOES.* FROM SESSION.FRED FRIENDS INNER JOIN SESSION.FRED2 FOES;

BEGIN
DECLARE CURSOR WARANDPEACE FOR RETURN TO CLIENT
OPEN CUSOR WARANDPEACE
END
Reply With Quote
  #4 (permalink)  
Old 08-23-04, 09:55
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Looks OK to me.

Andy
Reply With Quote
  #5 (permalink)  
Old 08-23-04, 10:14
KimballJohnson KimballJohnson is offline
Registered User
 
Join Date: Aug 2004
Posts: 51
Well OK then!

Thanks 10 chars!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On