LOOP Statement with LEAVE : LOOP : Procedure Function MySQL TUTORIALS


MySQL TUTORIALS » Procedure Function » LOOP »

 

LOOP Statement with LEAVE


The LOOP statement creates a loop that will run until the LEAVE statement is invoked.

Optional to the LOOP is a label.

A label is a name and a colon prefixed to the LOOP statement.

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

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

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

mysql>



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .

Follow Navioo On Twitter

MySQL TUTORIALS

 Navioo Procedure Function
» LOOP