LEAVE is a control-flow statement available in MySQL and MariaDB stored procedures, functions, triggers, and events. When executed, it terminates the innermost statement block or loop that has the specified label and transfers control to the first statement following that block. LEAVE works with LOOP, WHILE, REPEAT, and unlabeled BEGIN...END sections, provided each block is explicitly labeled. Unlike ITERATE, which restarts the loop, LEAVE behaves like a break statement in conventional languages. It does not implicitly roll back data changes; any modifications remain unless a ROLLBACK is issued or the routine ends with an unhandled error. LEAVE can only reference labels defined within the same stored program level, and the label name must be unique within that scope.
ITERATE, LOOP, WHILE, REPEAT, BEGIN...END, EXIT (PL/pgSQL), BREAK (T-SQL)
MySQL 5.0
LEAVE immediately terminates the labeled loop or BEGIN...END block and resumes execution after that block.
Yes. LEAVE requires an explicit label that matches the target loop or block. Omitting the label triggers a syntax error.
LEAVE exits the loop entirely, while ITERATE skips the remaining statements in the current iteration and restarts the loop.
No. LEAVE is valid only inside stored procedures, functions, triggers, or events in MySQL or MariaDB.