title: Having trouble doing multiple cursors in a MySQL stored procedure
body: <p>I'm writing a store procedure to create two temporary tables do an union select of the two.
  When using either first or second cursor alone with the other commented the procedure works,
  but when I run the query to create the procedure with the 2 cursors, it fails.i've changed the code to reflect Ike Walker's suggestion.</p>
  
  <p>Here is the script:</p>
  
  <pre><code>DELIMITER //
  
  DROP PROCEDURE IF EXISTS joinemailsmsdailygraph//
  
  CREATE PROCEDURE joinemailsmsdailygraph(IN previousDay VARCHAR(20), IN today VARCHAR(20))
  READS SQL DATA
  
  BEGIN
  
  DECLARE hours INT;
  DECLARE sms INT;
  DECLARE email INT;
  DECLARE smsdone INT DEFAULT 0;
  DECLARE emaildone INT DEFAULT 0;
  
  
  DECLARE cursorsms CURSOR FOR SELECT HOUR(sm.date_created) AS `HOUR OF DAY`, COUNT(*) AS smscount 
  FROM sms_message_delivery smd 
  JOIN sms_message sm ON sm.sms_message_id = smd.sms_message_id
  WHERE DATE(sm.date_created) &gt;= DATE(previousDay) AND DATE(sm.date_created) &lt; DATE(today) 
  GROUP BY HOUR(sm.date_created);
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET smsdone =1;
  
  
  DECLARE cursoremail CURSOR FOR SELECT HOUR(em.date_created) AS `HOUR OF DAY`, COUNT(*) AS emailcount 
  FROM email_message_delivery emd
  LEFT JOIN email_message em ON emd.email_message_id=em.email_message_id
  WHERE DATE(em.date_created) &gt;= DATE(previousDay) AND DATE(em.date_created) &lt; DATE(today)  
  GROUP BY HOUR(em.date_created);
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET emaildone =1;
  
  DROP TEMPORARY TABLE IF EXISTS tempsms;
  CREATE TEMPORARY TABLE tempsms (hours_day INT, sms_count INT, email_count INT);
  
  OPEN cursorsms;
  sms_loop: LOOP
  
  FETCH cursorsms INTO hours , sms;
  
  IF smsdone = 1 THEN
   LEAVE sms_loop;
  END IF;
  
  INSERT INTO tempsms (hours_day, sms_count) VALUES (hours, sms);
  
  END LOOP sms_loop;
  CLOSE cursorsms;
  
  
  DROP TEMPORARY TABLE IF EXISTS tempemail;
  
  CREATE TEMPORARY TABLE tempemail (hours_day INT , sms_count INT , email_count INT);
  
  OPEN cursoremail;
  email_loop: LOOP
  
  FETCH cursoremail INTO hours, email;
  
  IF emaildone=1 THEN
   LEAVE email_loop;
  END IF;
  
  INSERT INTO tempemail(hours_day, email_count) VALUES(hours, email);
  
  END LOOP email_loop;
  CLOSE cursoremail;
  
  
  SELECT hours_day, sms_count , email_count FROM tempsms
  UNION
  SELECT hours_day, sms_count, email_count FROM tempemail;
  
  END//
  DELIMITER;
  </code></pre>
  
  <p>it gives this as error </p>
  
  <blockquote>
    <p>Query : CREATE PROCEDURE joinemailsmsdailygraph(IN previousDay VARCHAR(20), IN today   VARCHAR(20)) READS SQL DATA BEGIN DECLARE hours INT...<br>
    Error Code : 1338<br>
    Cursor declaration after handler declaration<br>
    Execution Time : 00:00:00:000<br>
    Transfer Time  : 00:00:00:000<br>
    Total Time     : 00:00:00:000  </p>
  </blockquote>
  
  <p>ive tried putting both continue handlers at the end of all declare section but it complains about declare block overlapping or so.</p>
  
  <p>Can you please tell me what I'm doing wrong?  Thanks for reading.</p>