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 > Nested stored procedures in DB2

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-07-07, 08:58
Yogesh Pandit Yogesh Pandit is offline
Registered User
 
Join Date: Aug 2007
Posts: 11
Nested stored procedures in DB2

Hi,

I have 2 stored procedures A and B. A makes a call to B and B returns a cursor to A. Stored Procedure B executes fine individually. However, when I execute A, I get the following error:

SQLState = 07003
SQLCode = -518

Code snippet for stored procedure B:
DECLARE SampleCursor CURSOR WITH RETURN TO CALLER FOR
SELECT *
FROM TABLE
FETCH FIRST 5 ROWS ONLY;

OPEN SampleCursor

Code snippet for stored procedure A:
DECLARE LOC1 RESULT_SET_LOCATOR VARYING;

CALL B(OUT_SQLCODE, OUT_SQLSTATE, OUT_MESSAGE);

ASSOCIATE RESULT SET LOCATOR (:LOC1) WITH PROCEDURE B;
ALLOCATE CSR1 CURSOR FOR RESULT SET :LOC1;

EXECUTE CSR1 USING LOC1;

Can anyone please help me with this?
Reply With Quote
  #2 (permalink)  
Old 08-07-07, 10:49
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
I think cursors are supposed to be OPEN'ed, not EXECUTE'd...
Reply With Quote
  #3 (permalink)  
Old 08-07-07, 11:48
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
EXECUTE is for dynamic SQL, which you have to PREPARE first. The explanation for message SQL0518 tells you that.

But as n_i said, you want to use OPEN instead.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #4 (permalink)  
Old 08-08-07, 00:49
Yogesh Pandit Yogesh Pandit is offline
Registered User
 
Join Date: Aug 2007
Posts: 11
I had tried to OPEN the cursor. However, I got the following error:

THE OPEN STATEMENT FOR CURSOR "CSR1" IS INVALID BECAUSE THE CURSOR WAS DEFINED BY AN ALLOCATE CURSOR STATEMENT

Any idea about how this can be done?
Reply With Quote
  #5 (permalink)  
Old 08-08-07, 07:10
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Well, since the cursor was in fact opened by the nested procedure, you should be able to fetch from it after the ALLOCATE statement.
Reply With Quote
  #6 (permalink)  
Old 08-08-07, 08:55
Yogesh Pandit Yogesh Pandit is offline
Registered User
 
Join Date: Aug 2007
Posts: 11
Hmm. I just need to return the cursor from the calling stored procedure. I don't want to fetch values from the cursor into local variables by looping. Can you please suggest how this can be done?
Reply With Quote
  #7 (permalink)  
Old 08-08-07, 10:04
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by Yogesh Pandit
Hmm. I just need to return the cursor from the calling stored procedure.
Then you should have declared it WITH RETURN TO CLIENT.
Reply With Quote
  #8 (permalink)  
Old 08-09-07, 05:42
Yogesh Pandit Yogesh Pandit is offline
Registered User
 
Join Date: Aug 2007
Posts: 11
I tried using "WITH RETURN TO CLIENT" in the cursor definition. However, I am getting a compilation error:

ILLEGAL SYMBOL "CLIENT". SOME SYMBOLS THAT MIGHT BE LEGAL ARE: CALLER

I am using DB2 V8. Has it got anything to do with this error?
Reply With Quote
  #9 (permalink)  
Old 08-10-07, 02:41
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
I guess you are on DB2 for z/OS? Because DB2 LUW definitively supports WITH RETURN TO CLIENT, whereas DB2 z/OS does not yet allow this syntax.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #10 (permalink)  
Old 08-13-07, 05:24
Yogesh Pandit Yogesh Pandit is offline
Registered User
 
Join Date: Aug 2007
Posts: 11
Yes Stolze, I am using DB2 on z/OS. I tried looking for pointers on the net. But, unfortunately could not get any. Can you please help me with this?
Reply With Quote
  #11 (permalink)  
Old 08-20-07, 07:40
Yogesh Pandit Yogesh Pandit is offline
Registered User
 
Join Date: Aug 2007
Posts: 11
Hey Stolze, can you please help me with this? I am trying to figure out a way to call my nested stored procedure.
Reply With Quote
  #12 (permalink)  
Old 08-21-07, 06:43
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
The call is not the problem. Returning result sets to the client (instead to the caller) is. As far as I have found, this is not yet supported.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #13 (permalink)  
Old 08-21-07, 23:58
Yogesh Pandit Yogesh Pandit is offline
Registered User
 
Join Date: Aug 2007
Posts: 11
We found a workaround for this.Get the ResultSet in caller SP, loop through it to read the data and make entries in a temporary table. Then have another cursor which reads this temporary table and sends ResultSet out.
Reply With Quote
  #14 (permalink)  
Old 08-22-07, 04:41
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Yes, that will work, of course.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
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