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 > MySQL > Declare handler not found with curosr issue

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-23-11, 02:26
nithinics nithinics is offline
Registered User
 
Join Date: Jun 2011
Posts: 4
Question Declare handler not found with curosr issue

Hi,

Code:
....
DECLARE done INT DEFAULT 0;
DECLARE s VARCHAR(100) DEFAULT '';

#first query
DECLARE reader CURSOR FOR
    SELECT id, title FROM products ;
  DECLARE  CONTINUE HANDLER FOR NOT FOUND SET done=1;

OPEN reader 
REPEAT 

FETCH reader INTO vId, vTitle;

SET s = CONTACT(s, done, ':') ;
#second query
SELECT someother, details FROM another_table WHERE id=someid;
SET s = CONTACT(s, done) ;

UNTIL done
END REPEAT;

I've 10 records in products table so i want to iterate this loop 10 times. But when there is no item found in second query( another_table) loop exits. When i select the var s, i get the result
0:1.

That means When there is no result in SECOND query the DECLARE HANDLER NOT FOUND will execute.

Actually I dont want to stop the loop if there is no result in second query, I want to stop only if there is
no more items in first query.

How to do that, please help me.

thanks to all..
Reply With Quote
  #2 (permalink)  
Old 08-23-11, 04:34
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
Try this:
Use BEGIN/END and declare continue handler for inside this.

Code:
....
DECLARE s VARCHAR(100) DEFAULT '';

#first query
DECLARE done INT DEFAULT 0;
DECLARE reader CURSOR FOR
    SELECT id, title FROM products ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

OPEN reader 
REPEAT 

FETCH reader INTO vId, vTitle;

#second query
BEGIN
  DECLARE innerdone INT DEFAULT 0;
  DECLARE  CONTINUE HANDLER FOR NOT FOUND SET innerdone=1;

  SELECT someother, details FROM another_table WHERE id=someid;
  SET s = CONCAT(s, done) ;
END;

UNTIL done
END REPEAT;
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
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