The scope of a block label does not include the code for handlers declared within the block. Therefore, the statement associated with a handler cannot use ITERATE  or LEAVE  to refer to labels for blocks that enclose the handler declaration. Consider the following example, where the REPEAT  block has a label of retry : 

CREATE PROCEDURE p () BEGIN DECLARE i INT DEFAULT 3; retry: REPEAT BEGIN DECLARE CONTINUE HANDLER FOR SQLWARNING BEGIN ITERATE retry; # illegal END; IF i < 0 THEN LEAVE retry; # legal END IF; SET i = i - 1; END; UNTIL FALSE END REPEAT; END; 

The retry  label is in scope for the IF  statement within the block. It is not in scope for the CONTINUE handler, so the reference there is invalid and results in an error: 

ERROR 1308 (42000): LEAVE with no matching label: retry 

To avoid references to outer labels in handlers, use one of these strategies: 

•To leave the block, use an EXIT  handler. If no block cleanup is required, the BEGIN ... END  handler body can be empty: 

DECLARE EXIT HANDLER FOR SQLWARNING BEGIN END; 

Otherwise, put the cleanup statements in the handler body: 

DECLARE EXIT HANDLER FOR SQLWARNING BEGIN block cleanup statements END; 

•To continue execution, set a status variable in a CONTINUE  handler that can be checked in the enclosing block to determine whether the handler was invoked. The following example uses the variable done  for this purpose: 

CREATE PROCEDURE p () BEGIN DECLARE i INT DEFAULT 3; DECLARE done INT DEFAULT FALSE; retry: REPEAT BEGIN DECLARE CONTINUE HANDLER FOR SQLWARNING BEGIN SET done = TRUE; END; IF done OR i < 0 THEN LEAVE retry; END IF; SET i = i - 1; END; UNTIL FALSE END REPEAT; END; 

