The LEAVE statement is used to exit a labeled control-of-flow statement. If the label is used in the outermost stored program block, the LEAVE statement will exit the program.
Syntax
LEAVE label
Parameters
Parameter |
Description |
|---|---|
| label | The label name, specifying the control-of-flow statement to exit. |
The LEAVE statement can be used in BEGIN ... END blocks, as well as in LOOP, REPEAT, and WHILE loop statements.
Examples
Use LEAVE in a LOOP statement to exit the 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
