How to get rid of “Error 1329: No data - zero rows fetched, selected, or processed”

后端 未结 7 1599
心在旅途
心在旅途 2020-12-01 13:51

I have a stored procedure which does not need to return any values. It runs smoothly and without any problem. However, it outputs an error message after finishing its run:

相关标签:
7条回答
  • 2020-12-01 14:16

    You need to define a continue handler like:

    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
    

    So it would look like:

    DECLARE done INT DEFAULT 0;
    DECLARE l_name VARCHAR(20);
    DECLARE my_cur CURSOR FOR
        SELECT name FROM customer_tbl;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
    
    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;
    
    0 讨论(0)
  • 2020-12-01 14:17

    I tried the solutions in here and none, including the continue handler worked for me. I still get the messages in the MySQL error log. I discovered this also with my "select ... into ..." which made sense, but I really thought the continue handler would work for the cursors. Either way I found using "found_rows()" to find out if any rows were returned worked perfectly. This mean that the simple "select into" statements have to be converted to cursors, but it isn't much work and does solve the problem.

    DECLARE v_rowcount      integer unsigned;
    DECLARE cur_entries cursor for
            select app_name, proc_name, error_code, sum(occurrences) occurrences
            from that_table...; 
    open cur_entries; 
    set v_rowcount = found_rows();
    if v_rowcount > 0 then
      fetch cur_entries into v_app_name, v_proc_name, v_error_code, v_occurrences;
      ...
    end if;
    close cur_entries;
    

    I wrote this up on my personal blog here: http://tinky2jed.wordpress.com/technical-stuff/mysql/mysql-no-data-zero-rows-fetched-how-to-code-for-it/

    0 讨论(0)
  • 2020-12-01 14:21

    Normally this happens when you overshoot a cursor range, so checkout the loop conditions where the FETCH statement is

    0 讨论(0)
  • 2020-12-01 14:24

    I was getting the same error in my code, and I realized that I had not incremented my loop variable (using while loop) and hence, the loop was going infinite.

    In your code too, you are not setting "done" to 1 anywhere, and I think the code is showing error because of that.

    In the below code, instead of the variable "done", I have added a variable "count" that is initialized with the number of records in the table and is decremented after each insertion. The loop is terminated when count=0:

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

    I hope this helps!

    0 讨论(0)
  • 2020-12-01 14:26

    I ran into this and pulled out my hair till I ran across this in the official mysql docs

    Before MySQL 5.6.3, if a statement that generates a warning or error causes a condition handler to be invoked, the handler may not clear the diagnostic area. This might lead to the appearance that the handler was not invoked. The following discussion demonstrates the issue and provides a workaround.

    Click the link and scroll to the bottom for details but the fix was to include a successful select INSIDE the CONTINUE HANDLER:

    DECLARE CONTINUE HANDLER FOR NOT FOUND
    BEGIN
       SELECT 1 INTO @handler_invoked FROM (SELECT 1) AS t;
    END;
    
    0 讨论(0)
  • 2020-12-01 14:27

    I don't know if this fixes the cursor issue, but I ran into this warning with a stored function and found that if you use:

    RETURN (SELECT x From myTable...);
    

    instead of

    SELECT x into myVar...return myVar
    

    I got this from this helpful doc: http://bugs.mysql.com/bug.php?id=42834

    0 讨论(0)
提交回复
热议问题