LOOP with LEAVE : LOOP « Procedure Function « MySQL Tutorial






mysql>
mysql> delimiter $$
mysql>
mysql> CREATE PROCEDURE myProc()
    -> BEGIN
    ->     DECLARE i int;
    ->     SET i=1;
    ->     myloop: LOOP
    ->          SET i=i+1;
    ->          IF i=10 THEN
    ->                   LEAVE myloop;
    ->          END IF;
    ->     END LOOP myloop;
    ->     SELECT 'I can count to 10';
    ->
    -> END$$
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> delimiter ;
mysql>
mysql> call myProc();
+-------------------+
| I can count to 10 |
+-------------------+
| I can count to 10 |
+-------------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql>
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