Simple LOOP : LOOP « Procedure Function « MySQL Tutorial

Home
MySQL Tutorial
1.Introduction
2.Select Query
3.Database
4.Table
5.Table Join
6.Subquery
7.Insert Update Delete
8.Logic Operator
9.View
10.Data Types
11.Procedure Function
12.Cursor
13.Trigger
14.Date Time Functions
15.Comparison Functions Operators
16.Aggregate Functions
17.Cast Functions Operators
18.Control Flow Functions
19.Encryption Compression Functions
20.Information Functions
21.Math Numeric Functions
22.Miscellaneous Functions
23.String Functions
24.Regular Expressions
25.Data Dictionary
26.MySQL Utilities
27.Privilege
PostgreSQL
SQL / MySQL
MySQL Tutorial » Procedure Function » LOOP 
11.18.1.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>
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
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.