MySQL error Handling-Example of error handling

Source: Internet
Author: User
Tags error handling

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.