"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Handle \"Error 1329: No Data - Zero Rows Fetched\" in MySQL Stored Procedures?

How to Handle \"Error 1329: No Data - Zero Rows Fetched\" in MySQL Stored Procedures?

Published on 2024-11-07
Browse:718

How to Handle \

No Data Warning in Stored Procedures: "Error 1329: Zero Rows Fetched, Selected, or Processed"

In database programming, stored procedures are often used to encapsulate complex operations and ensure data integrity. However, sometimes these procedures may encounter errors or produce unexpected warnings. One common issue is the "Error 1329: No data - zero rows fetched, selected, or processed" message.

This warning typically appears when a stored procedure does not explicitly handle the case where no data is returned. To resolve this, one must properly handle the "NOT FOUND" condition. In MySQL, this can be achieved using a CONTINUE HANDLER statement.

Consider the following testing_proc stored procedure:

CREATE PROCEDURE `testing_proc`()
READS SQL DATA
BEGIN
    DECLARE done INT DEFAULT 0;
    DECLARE l_name VARCHAR(20);
    DECLARE my_cur CURSOR FOR
        SELECT name FROM customer_tbl;
    OPEN my_cur;
        my_cur_loop:
        LOOP FETCH my_cur INTO l_name;
            IF done = 1 THEN
                LEAVE my_cur_loop;
            END IF;
            INSERT INTO names_tbl VALUES(l_name);
        END LOOP my_cur_loop;
    CLOSE my_cur;
END

If this procedure runs without any data in the customer_tbl table, it will trigger the "Error 1329" warning. To prevent this, we can add the following CONTINUE HANDLER statement to the end of the procedure:

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

This statement tells MySQL to set the done variable to 1 if no rows are found. This will cause the cursor loop to exit, preventing the error from occurring.

Alternatively, one can also bypass the warning by adding a "dummy" statement at the end of the procedure that involves a table and is successful, such as:

SELECT name INTO l_name FROM customer_tbl LIMIT 1;

This statement will ensure that a row is fetched, clearing the warning.

By properly handling the "NOT FOUND" condition or adding a dummy statement, one can eliminate the "Error 1329" warning in stored procedures that do not return any data.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3