Exit a labeled block or loop immediately. This statement terminates the execution of the current loop or compound statement and continues after the block.
This statement is used to exit the flow control construct that has the given label. The label must be in the same stored program, not in a caller procedure. LEAVE can be used within BEGIN ... END or loop constructs (LOOP, REPEAT, WHILE). In Stored Procedures, Triggers and Events, LEAVE can refer to the outmost BEGIN ... END construct; in that case, the program exits the procedure. In Stored Functions, RETURN can be used instead.
If you try to LEAVE a non-existing label, or if you try to LEAVE a HANDLER block, the following error will be produced:
The following example uses LEAVE to exit the procedure if a condition is true:
- Repeats a loop execution
This page is licensed: GPLv2, originally from
LEAVE labelERROR 1308 (42000): LEAVE with no matching label: <label_name>CREATE PROCEDURE proc(IN p TINYINT)
CONTAINS SQL
`whole_proc`:
BEGIN
SELECT 1;
IF p < 1 THEN
LEAVE `whole_proc`;
END IF;
SELECT 2;
END;
CALL proc(0);
+---+
| 1 |
+---+
| 1 |
+---+