There are several declarative forms of error handling:
§ If any error (not FOUND), set L_error to 1 to continue execution:
DECLARE CONTINUE HANDLER for SQLEXCEPTION
SET l_error=1;
§ If any errors occur (not FOUND), execute rollback and produce an error message after exiting the current block or stored procedure.
DECLARE EXIT HANDLER for SQLEXCEPTION
BEGIN
ROLLBACK;
SELECT ' Error occurred–terminating ';
END;
§ If the MySQL 1062 error (Duplicate health value) occurs, execute the SELECT statement (sending a message to the caller)
DECLARE CONTINUE hander for 1062
SELECT ' Duplicate key in index ';
§ If the SQLSTATE 2300 error (Duplicate health value) occurs, execute the SELECT statement (sending a message to the calling program)
DECLARE CONTINUE hander for SQLSTATE ' 23000 '
SELECT ' Duplicate key in index ';
§ When a cursor or SQL SELECT statement does not return a value, the l_done=1 is set to continue execution
DECLARE CONTINUE HANDLER for not
FOUND
SET l_done=1;
§ This example, in addition to using the SQLSTATE variable instead of a naming condition, is the same as in the previous example
DECLARE CONTINUE HANDLER for SQLSTATE ' 02000 '
SET l_done=1;
§ This example uses the MySQL error code variable instead of the naming condition or the SQLSTATE variable, as in the first two examples
DECLARE CONTINUE HANDLER for 1329
SET l_done=1;
DECLARE CONTINUE HANDLER for not FOUND SET not_found = 1; --If the data is not found, change the tag to unavailable
DECLARE EXIT HANDLER for SQLEXCEPTION ROLLBACK; --Abnormal jumping out
Start TRANSACTION;
COMMIT;
MySQL error Handling-Example of error handling