There are three standard loops in the statements of the MySQL stored procedure: The while loop, the loop loop, and the repeat loop. There is also a non-standard way of cycling: GOTO, but this cycle is best not to use, it is easy to cause confusion in the program, here is a good introduction.
Today we'll take a look at the loop loop.
MySQL Tutorials >
Mysql> delimiter $$
Mysql>
Mysql> CREATE PROCEDURE MyProc ()
-> BEGIN
->
-> DECLARE I int;
-> SET i=0;
-> Loop1:loop
-> SET i=i+1;
-> IF i>=10 THEN/*last number-exit loop*/
-> LEAVE Loop1;
-> ELSEIF MOD (i,2) =0 THEN/*even number-try again*/
-> iterate Loop1;
-> End IF;
->
-> SELECT CONCAT (i, "is a odd number");
->
-> End LOOP Loop1;
->
->
-> end$$
Query OK, 0 rows affected (0.02 sec)
Mysql>
Mysql> delimiter;
Mysql> call MyProc ();
+-------------------------------+
| CONCAT (i, "is a odd number") |
+-------------------------------+
| 1 is a odd number |
+-------------------------------+
1 row in Set (0.00 sec)
+-------------------------------+
| CONCAT (i, "is a odd number") |
+-------------------------------+
| 3 is a odd number |
+-------------------------------+
1 row in Set (0.00 sec)
+-------------------------------+
| CONCAT (i, "is a odd number") |
+-------------------------------+
| 5 is a odd number |
+-------------------------------+
1 row in Set (0.00 sec)
+-------------------------------+
| CONCAT (i, "is a odd number") |
+-------------------------------+
| 7 is a odd number |
+-------------------------------+
1 row in Set (0.01 sec)
+-------------------------------+
| CONCAT (i, "is a odd number") |
+-------------------------------+
| 9 is a odd number |
+-------------------------------+
1 row in Set (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
Mysql> drop procedure MyProc;
Query OK, 0 rows Affected (0.00 sec)
Mysql>