Condition Handling
1148
->
INSERT INTO test.t VALUES (1);
->
SET @x = 3;
->
END;
->
//
Query OK, 0 rows affected (0.00 sec)
mysql>
CALL handlerdemo()//
Query OK, 0 rows affected (0.00 sec)
mysql>
SELECT @x//
+------+
| @x |
+------+
| 3 |
+------+
1 row in set (0.00 sec)
Notice that
@x
is
3
after the procedure executes, which shows that execution continued to the end
of the procedure after the error occurred. If the
DECLARE ... HANDLER
statement had not been
present, MySQL would have taken the default action (
EXIT
) after the second
INSERT
failed due to the
PRIMARY KEY
constraint, and
SELECT @x
would have returned
2
.
To ignore a condition, declare a
CONTINUE
handler for it and associate it with an empty block. For
example:
DECLARE CONTINUE HANDLER FOR SQLWARNING BEGIN END;
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;
Summary of Contents for 5.0
Page 1: ...MySQL 5 0 Reference Manual ...
Page 18: ...xviii ...
Page 60: ...40 ...
Page 396: ...376 ...
Page 578: ...558 ...
Page 636: ...616 ...
Page 844: ...824 ...
Page 1234: ...1214 ...
Page 1427: ...MySQL Proxy Scripting 1407 ...
Page 1734: ...1714 ...
Page 1752: ...1732 ...
Page 1783: ...Configuring Connector ODBC 1763 ...
Page 1793: ...Connector ODBC Examples 1773 ...
Page 1839: ...Connector Net Installation 1819 2 You must choose the type of installation to perform ...
Page 2850: ...2830 ...
Page 2854: ...2834 ...
Page 2928: ...2908 ...
Page 3000: ...2980 ...
Page 3122: ...3102 ...
Page 3126: ...3106 ...
Page 3174: ...3154 ...
Page 3232: ...3212 ...