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.
Note that LEAVE cannot be used to exit a DECLARE HANDLER block.
If you try to LEAVE a non-existing label, or if you try to LEAVE a HANDLER block, the following error will be produced:
ERROR 1308 (42000): LEAVE with no matching label: <label_name>
The following example uses
LEAVE to exit the procedure if a condition is true:
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 | +---+
- ITERATE - Repeats a loop execution