Simple LOOP : LOOP : Procedure Function MySQL TUTORIALS


MySQL TUTORIALS » Procedure Function » LOOP »

 

Simple LOOP


[begin_label:LOOP
    statement_list
END LOOP [end_label]

A LOOP statement can be labeled.

end_label cannot be given unless begin_label also is present.

If both are present, they must be the same.

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

mysql>
mysql> delimiter ;
mysql>
mysql> call myProc();
+---------+
| counter |
+---------+
|       |
+---------+
row in set (0.00 sec)

+---------+
| counter |
+---------+
|       |
+---------+
row in set (0.02 sec)

+---------+
| counter |
+---------+
|       |
+---------+
row in set (0.02 sec)

+---------+
| counter |
+---------+
|       |
+---------+
row in set (0.02 sec)

+---------+
| counter |
+---------+
|       |
+---------+
row in set (0.02 sec)

+---------+
| counter |
+---------+
|       |
+---------+
row in set (0.02 sec)

+---------+
| counter |
+---------+
|       |
+---------+
row in set (0.02 sec)

+---------+
| counter |
+---------+
|       |
+---------+
row in set (0.02 sec)

+---------+
| counter |
+---------+
|       |
+---------+
row in set (0.33 sec)

+---------+
| counter |
+---------+
|      10 |
+---------+
row in set (0.33 sec)

+-------------------+
| I can count to 10 |
+-------------------+
| I can count to 10 |
+-------------------+
row in set (0.33 sec)

Query OK, rows affected (0.33 sec)

mysql>
mysql> DROP PROCEDURE myProc;
Query OK, rows affected (0.00 sec)

mysql>
mysql>



Leave a Comment / Note


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

Follow Navioo On Twitter

MySQL TUTORIALS

 Navioo Procedure Function
» LOOP