Skip to content

LEAVE

DECLARE variable_name INT;
DECLARE exit handler FOR NOT FOUND SET variable_name = 1;
START TRANSACTION;
SELECT * FROM table_name;
IF variable_name = 1 THEN
ROLLBACK;
LEAVE proc_name;
END IF;
COMMIT;

No output will be displayed if the condition is met and the LEAVE statement is executed.

This example represents a transaction using the LEAVE statement in a stored procedure named proc_name. Initially variable_name is declared, also an exit handler is declared which sets variable_name = 1 if a NOT FOUND situation occurs.

Then, it starts a transaction and it selects data from table_name. If the variable_name = 1 condition is met (which is possible if NOT FOUND situation occurred), it rolls back the transaction and using the LEAVE statement, control is immediately transferred out of the procedure proc_name. Therefore, no output will be displayed if the LEAVE statement is executed.