LOOP Statement with LEAVE : LOOP « Procedure Function « SQL / MySQL






LOOP Statement with LEAVE

  


mysql>
mysql> DELIMITER //
mysql> CREATE FUNCTION myFunction(quantity INT(10)) RETURNS INT(10)
    -> BEGIN
    ->
    ->     increment: LOOP
    ->
    ->     IF quantity MOD 10 < 1 THEN
    ->     LEAVE increment;
    ->     END IF;
    ->
    ->     SET quantity = quantity - 1;
    ->
    ->     END LOOP increment;
    ->
    ->     RETURN quantity;
    ->
    -> END
    -> //
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;
mysql>
mysql> select myFunction(10);
+----------------+
| myFunction(10) |
+----------------+
|             10 |
+----------------+
1 row in set (0.00 sec)

mysql>
mysql>
mysql> drop function myFunction;
Query OK, 0 rows affected (0.00 sec)

mysql>

          
  








Related examples in the same category

1.Simple LOOP
2.Nesting if statement with LOOP statement
3.LOOP with LEAVE
4.LOOP with ITERATE
5.Loop with label