0

When I run the following MySQL server stored procedure I would expect that it run forever because there is no CONTINUE HANDLER

BEGIN
   DECLARE p INT(11) DEFAULT 0;
   DECLARE no_more_rows BOOLEAN DEFAULT false;

   DECLARE testA INT;

   DECLARE iCursor CURSOR FOR SELECT test_a FROM Temp;

   OPEN iCursor;
       id_loop: LOOP
           FETCH iCursor INTO testA;
           SET p = p + 1;
           INSERT INTO Temp2 (test1, test2) VALUES (testA, p);
       END LOOP; 
   INSERT INTO Temp3 SET Finished=1;        
   CLOSE iCursor;
END

But it seems that the CURSOR is working perfect and wrote all the values from the Temp table to the Temp2 table (and there are also no more values of the counter p written into the table). The procedure is also not running any more when I check the running processes on the MySQL server.

But the Finished code is not written into the Temp3 table and I therefore suppose the cursor is also not closed properly.

Does anybody know what happens here?

Why is the loop not running endless?

Is it ok to implement a cursor like that (in case there is no code that should be run after the loop)?

1 Answer 1

1

How are you invoking the stored procedure?

I get:

mysql> CALL sp_test;
ERROR 1329 (02000): No data - zero rows fetched, selected, or processed

The error terminates execution of the stored procedure, so do not write on temp3 nor closes the cursor.

SQL Fiddle demo

Sign up to request clarification or add additional context in comments.

1 Comment

It was executed from php before and the error was just ignored. Thank you for the answer that clarified things for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.