MySQL Error 1329 - Empty Cursor Issue I am experiencing an issue with a stored procedure I wrote some time ago that has recently stopped working. The SP populates a cursor and runs through a loop processing the data in another SP.

The issue is that since recreating the SP with a simple cosmetic change, the NOT FOUND Handler for the cursor always fires immediately as if the cursor were empty, and the SP returns a 1329 No data processed error. 

However, running the select query that populates the cursor succesfully returns the data fine.

Here is the code:

DELIMITER //

     CREATE PROCEDURE `Import_Leave`()
        BEGIN

        DECLARE AbsenceID INT;
        DECLARE PersonID INT;
        DECLARE EmpID INT;
        DECLARE Start_Date DATETIME;
        DECLARE Numdays INT;
        DECLARE Category VARCHAR(50);
        DECLARE WorkFlow INT;
        DECLARE exit_loop BOOLEAN ;
        DECLARE absence CURSOR FOR SELECT AbsenceID,PersonID,EmpID,Start_Date,Numdays,Category,Workflow FROM staging_holidays WHERE EmpID is NOT NULL;
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET exit_loop = TRUE;

        OPEN absence;

        shoop_da_woop: LOOP

          #SELECT exit_loop;

          FETCH absence into AbsenceID,PersonID,EmpID,Start_Date,Numdays,Category,Workflow;

          #SELECT exit_loop;

          IF exit_loop THEN
            CLOSE absence;
            LEAVE shoop_da_woop;
          END IF;

    #Code to process the data goes here, but is never executed asthe cursor is coming back empty

        END LOOP shoop_da_woop;

        END//

        DELIMITER ;


I cannot figure out why this cursor is not fetching the data succesfuly when the underlying SELECT statement works fine. This SP used to work but has stopped working since the last time it was recreated. The commented out SELECT statements before and after the FETCH statement are in for debugging and they confirm that the NOT FOUND HANDLER is firing as soon as the FETCH completes.

Any help would be appreciated!
i made 3 changes to your SP:


initialize exit_loop to FALSE
change the name of each variable by adding "V_" to make them different to the column name: 
close the cursor outside the loop 


below the end result hopefully that solved your issue: 

DELIMITER //
CREATE PROCEDURE `Import_Leave`()
BEGIN
 DECLARE V_AbsenceID INT;
 DECLARE V_PersonID INT;
 DECLARE V_EmpID INT;
 DECLARE V_Start_Date DATETIME;
 DECLARE V_Numdays INT;
 DECLARE V_Category VARCHAR(50);
 DECLARE V_WorkFlow INT;

 DECLARE exit_loop BOOLEAN DEFAULT FALSE ;

 DECLARE absence CURSOR FOR SELECT AbsenceID,PersonID,EmpID,Start_Date,Numdays,Category,Workflow FROM staging_holidays WHERE EmpID is NOT NULL;
 DECLARE CONTINUE HANDLER FOR NOT FOUND SET exit_loop = TRUE;

 OPEN absence;

 shoop_da_woop: LOOP
    FETCH absence into V_AbsenceID,V_PersonID,V_EmpID,V_Start_Date,V_Numdays,V_Category,V_Workflow;

    IF exit_loop THEN
        LEAVE shoop_da_woop;
    END IF;
    #Code to process the data goes here, but is never executed asthe cursor is coming back empty
 END LOOP shoop_da_woop;

 CLOSE absence;

 END//
 DELIMITER ;