LOOP with ITERATE : LOOP « Procedure Function « MySQL Tutorial






mysql>
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 an odd number");
    ->
    ->     END LOOP loop1;
    ->
    ->
    -> END$$
Query OK, 0 rows affected (0.02 sec)

mysql>
mysql> delimiter ;
mysql> call myProc();
+-------------------------------+
| CONCAT(i," is an odd number") |
+-------------------------------+
| 1 is an odd number            |
+-------------------------------+
1 row in set (0.00 sec)

+-------------------------------+
| CONCAT(i," is an odd number") |
+-------------------------------+
| 3 is an odd number            |
+-------------------------------+
1 row in set (0.00 sec)

+-------------------------------+
| CONCAT(i," is an odd number") |
+-------------------------------+
| 5 is an odd number            |
+-------------------------------+
1 row in set (0.00 sec)

+-------------------------------+
| CONCAT(i," is an odd number") |
+-------------------------------+
| 7 is an odd number            |
+-------------------------------+
1 row in set (0.01 sec)

+-------------------------------+
| CONCAT(i," is an odd number") |
+-------------------------------+
| 9 is an 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>








11.18.LOOP
11.18.1.Simple LOOP
11.18.2.Nesting if statement with LOOP statement
11.18.3.LOOP with LEAVE
11.18.4.LOOP with ITERATE
11.18.5.LOOP Statement with LEAVE