MySQL error handling example

Source: Internet
Author: User

From http://www.devshed.com/c/a/MySQL/Error-Handling-Examples/
Error handler examples
Here are some examples of handler declarations:
If any error condition arises (other than a not found), continue execution after setting l_error = 1:
Declare continue handler for sqlexception
Set l_error = 1;
If any error condition arises (other than a not found), exit the current block or stored program after issuing a rollback statement and issuing an error message:
Declare exit handler for sqlexception
Begin
Rollback;
Select 'error occurred-terminating ';
End;
If MySQL error 1062 (duplicate key value) is encountered, continue execution after executing the SELECT statement (which generates a message for the calling program ):
Declare continue hander for 106 2
Select 'duplicate key in Index ';
If sqlstate 23000 (duplicate key value) is encountered, continue execution after executing the SELECT statement (which generates a message for the calling program ):
Declare continue hander for sqlstate '20140901'
Select 'duplicate key in Index ';
When a cursor fetch or SQL retrieves no values, continue execution after setting l_done = 1:
Declare continue handler for not
Found
Set l_done = 1;
Same as the previous example, except T specified using a sqlstate variable rather than a named condition:
Declare continue handler for sqlstate '20140901'
Set l_done = 1;
Same as the previous two examples, counter t specified using a MySQL error code variable rather than a named condition or sqlstate variable:
Declare continue handler for 1329
Set l_done = 1;

Error Handling example
There are several types of error handling statements:
§ If any error (not found) occurs, set l_error to 1 and continue execution:
Declare continue handler for sqlexception
Set l_error = 1;
§ If any error occurs (not found), execute rollback and generate an error message to exit the current block or stored procedure.
Declare exit handler for sqlexception
Begin
Rollback;
Select 'error occurred-terminating ';
End;
§ If a MySQL 1062 error (duplicate key value) occurs, execute the SELECT statement (to call Program Send a message) and continue execution
Declare continue hander for 106 2
Select 'duplicate key in Index ';
§ If the sqlstate 2300 error (duplicate key value) occurs, execute the SELECT statement (send a message to the calling program) and continue to execute
Declare continue hander for sqlstate '20140901'
Select 'duplicate key in Index ';
§ When no return value is returned for a cursor or SQL selection statement, set l_done = 1 and continue execution.
Declare continue handler for not
Found
Set l_done = 1;
§ In this example, apart from using the sqlstate variable rather than the naming condition, this is the same as in the previous example.
Declare continue handler for sqlstate '20140901'
Set l_done = 1;
§ In this example, besides using the MySQL error code variable instead of the naming condition or sqlstate variable
Declare continue handler for 1329
Set l_done = 1;

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.