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>
mysql> delimiter $$
Mysql>
mysql> CREATE PROCEDURE myProc ()
-& Gt 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>