LEAVE to a label : LEAVE « Procedure Function « MySQL Tutorial






LEAVE label

LEAVE label statement is used to exit any labeled flow control construct.

It can be used within BEGIN ... END or loop constructs (LOOP, REPEAT, WHILE).

The LEAVE statement must be accompanied by a label.

mysql>
mysql> delimiter $$
mysql>
mysql> CREATE PROCEDURE myProc()
    ->  outer_block: BEGIN
    ->         DECLARE l_status int;
    ->         SET l_status=1;
    ->         inner_block: BEGIN
    ->                 IF (l_status=1) THEN
    ->                         LEAVE inner_block;
    ->                 END IF;
    ->                 SELECT 'This statement will never be executed';
    ->         END inner_block;
    ->         SELECT 'End of program';
    -> END outer_block$$
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;
mysql> call myProc();
+----------------+
| End of program |
+----------------+
| End of program |
+----------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.02 sec)

mysql> drop procedure myProc;
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql>
mysql>








11.17.LEAVE
11.17.1.LEAVE to a label
11.17.2.LEAVE with Label out of an inner loop