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

后端 未结 7 1656
心在旅途
心在旅途 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: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!

提交回复
热议问题