The LEAVE statement is used to exit a flow control statement with the specified label. If the label refers to the outermost stored program block, using LEAVE will exit the program.
Syntax
LEAVE label
Parameters
Parameter |
Note |
|---|---|
| label | The tag name specifies the flow control statement from which to exit. |
The LEAVE statement can be used within BEGIN ... END blocks, as well as in LOOP, REPEAT, and WHILE loop statements.
Examples
Use LEAVE to exit a LOOP loop.
obclient> DELIMITER //
obclient> CREATE PROCEDURE leave_example()
BEGIN
DECLARE i INT DEFAULT 0;
loop_label: LOOP
SET i = i + 1;
IF i >= 5 THEN
LEAVE loop_label;
END IF;
END LOOP loop_label;
SELECT i;
END //
obclient> DELIMITER ;
obclient> CALL leave_example();
The query result is as follows:
+------+
| i |
+------+
| 5 |
+------+
1 row in set