MySQL Error Handling in Stored Procedures---转载
This tutorial shows you how to use MySQL handler to handle exceptions or errors encountered in stored procedures.
When an error occurs inside a stored procedure, it is important to handle it appropriately, such as continuing or exiting the current code block’s execution, and issuing a meaningful error message.
MySQL provides an easy way to define handlers that handle from general conditions such as warnings or exceptions to specific conditions e.g., specific error codes.
Declaring a handler
To declare a handler, you use the DECLARE HANDLER statement as follows:
|
1
|
DECLARE action HANDLER FOR condition_value statement;
|
If a condition whose value matches the condition_value, MySQL will execute the statement and continue or exit the current code block based on the action.
The action accepts one of the following values:
CONTINUE: the execution of the enclosing code block (BEGIN…END) continues.EXIT: the execution of the enclosing code block, where the handler is declared, terminates.
The condition_value specifies a particular condition or a class of conditions that activates the handler. The condition_value accepts one of the following values:
- A MySQL error code.
- A standard
SQLSTATEvalue. Or it can be anSQLWARNING,NOTFOUNDorSQLEXCEPTIONcondition, which is shorthand for the class ofSQLSTATEvalues. TheNOTFOUNDcondition is used for a cursororSELECT INTO variable_liststatement. - A named condition associated with either a MySQL error code or
SQLSTATEvalue.
The statement could be a simple statement or a compound statement enclosing by the BEGIN andEND keywords.
MySQL error handling examples
Let’s look into several examples of declaring handlers.
The following handler means if an error occurs, set the value of the has_error variable to 1 and continue the execution.
|
1
|
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET has_error = 1;
|
The following is another handler; it means that in case any error occurs, rollback the previous operation, issue an error message and exit the current code block. If you declare it inside the BEGIN END block of a stored procedure, it will terminate stored procedure immediately.
|
1
2
3
4
5
|
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
SELECT 'An error has occurred, operation rollbacked and the stored procedure was terminated';
END;
|
If there are no more rows to fetch, in case of a cursoror SELECT INTOstatement, set the value of the no_row_found variable to 1 and continue execution.
|
1
|
DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_row_found = 1;
|
If a duplicate key error occurs, MySQL error 1062 is issued. The following handler issues an error message and continues execution.
|
1
2
|
DECLARE CONTINUE HANDLER FOR 1062
SELECT 'Error, duplicate key occurred';
|
MySQL handler example in stored procedures
First, we create a new table named article_tags for the demonstration:
|
1
2
3
4
5
|
CREATE TABLE article_tags(
article_id INT,
tag_id INT,
PRIMARY KEY(article_id,tag_id)
);
|
The article_tags table stores the relationships between articles and tags. Each article may have many tags and vice versa. For the sake of simplicity, we don’t create articles and tags tables, as well as the foreign keys in the article_tags table.
Second, we create a stored procedure that inserts a pair of ids of article and tag into the article_tags table:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
DELIMITER $$
CREATE PROCEDURE insert_article_tags(IN article_id INT, IN tag_id INT)
BEGIN
DECLARE CONTINUE HANDLER FOR 1062
SELECT CONCAT('duplicate keys (',article_id,',',tag_id,') found') AS msg;
-- insert a new record into article_tags
INSERT INTO article_tags(article_id,tag_id)
VALUES(article_id,tag_id);
-- return tag count for the article
SELECT COUNT(*) FROM article_tags;
END
|
Third, we add tag id 1, 2 and 3 for the article 1 by calling the insert_article_tags stored procedure as follows:
|
1
2
3
|
CALL insert_article_tags(1,1);
CALL insert_article_tags(1,2);
CALL insert_article_tags(1,3);
|
Fourth, let’s try to insert a duplicate key to see if the handler is really invoked.
|
1
|
CALL insert_article_tags(1,3);
|
We got an error message. However, because we declared the handler as a CONTINUE handler, the stored procedure continued execution. As the result, we got the tag count for the article as well.

If we change the CONTINUE in the handler declaration to EXIT, we will get only the error message.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
DELIMITER $$
CREATE PROCEDURE insert_article_tags_2(IN article_id INT, IN tag_id INT)
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
SELECT 'SQLException invoked';
DECLARE EXIT HANDLER FOR 1062
SELECT 'MySQL error code 1062 invoked';
DECLARE EXIT HANDLER FOR SQLSTATE '23000'
SELECT 'SQLSTATE 23000 invoked';
-- insert a new record into article_tags
INSERT INTO article_tags(article_id,tag_id)
VALUES(article_id,tag_id);
-- return tag count for the article
SELECT COUNT(*) FROM article_tags;
END
|
Now, we can try to add a duplicate key to see the effect.
|
1
|
CALL insert_article_tags_2(1,3);
|
MySQL handler precedence
In case there are multiple handlers that are eligible for handling an error, MySQL will call the most specific handler to handle the error.
An error always maps to one MySQL error code so a MySQL it is the most specific. An SQLSTATE may map to many MySQL error codes therefore it is less specific. An SQLEXCPETION or an SQLWARNINGis the shorthand for a class of SQLSTATES values so it is the most generic.
Based on the handler precedence’s rules, MySQL error code handler, SQLSTATE handler andSQLEXCEPTION takes the first, second and third precedence.
Suppose we declare three handlers in the insert_article_tags_3 stored procedure as follows:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
DELIMITER $$
CREATE PROCEDURE insert_article_tags_3(IN article_id INT, IN tag_id INT)
BEGIN
DECLARE EXIT HANDLER FOR 1062 SELECT 'Duplicate keys error encountered';
DECLARE EXIT HANDLER FOR SQLEXCEPTION SELECT 'SQLException encountered';
DECLARE EXIT HANDLER FOR SQLSTATE '23000' SELECT 'SQLSTATE 23000';
-- insert a new record into article_tags
INSERT INTO article_tags(article_id,tag_id)
VALUES(article_id,tag_id);
-- return tag count for the article
SELECT COUNT(*) FROM article_tags;
END
|
We now try to insert a duplicate key into the article_tags table by calling the stored procedure:
|
1
|
CALL insert_article_tags_3(1,3);
|
As you see the MySQL error code handler is called.

Using named error condition
Let’s start with an error handler declaration.
|
1
2
|
DECLARE EXIT HANDLER FOR 1051 SELECT 'Please create table abc first';
SELECT * FROM abc;
|
What does the number 1051 really mean? Imagine you have a big stored procedure polluted with those numbers all over places; it will become a nightmare for the maintenance developers.
Fortunately, MySQL provides us with the DECLARE CONDITION statement that declares a named error condition, which associates with a condition. The syntax of the DECLARE CONDITION statement is as follows:
|
1
|
DECLARE condition_name CONDITION FOR condition_value;
|
The condition_value can be a MySQL error code such as 1015 or a SQLSTATE value. The condition_value is represented by the condition_name.
After declaration, you can refer to the condition_name instead of the condition_value.
So we can rewrite the code above as follows:
|
1
2
3
|
DECLARE table_not_found CONDITION for 1051;
DECLARE EXIT HANDLER FOR table_not_found SELECT 'Please create table abc first';
SELECT * FROM abc;
|
This code is obviously more readable than the previous one.
Notice that the condition declaration must appear before handler or cursor declarations.
原文:http://www.mysqltutorial.org/mysql-error-handling-in-stored-procedures/
MySQL Error Handling in Stored Procedures---转载的更多相关文章
- MySQL Error Handling in Stored Procedures 2
Summary: this tutorial shows you how to use MySQL handler to handle exceptions or errors encountered ...
- MySQL Error Handling in Stored Procedures
http://www.mysqltutorial.org/mysql-error-handling-in-stored-procedures/ mysql存储过程中的异常处理 定义异常捕获类型及处 ...
- mysql Error Handling and Raising in Stored Procedures
MySQL的存储过程错误捕获方式和Oracle的有很大的不同. MySQL中可以使用DECLARE关键字来定义处理程序.其基本语法如下: DECLARE handler_type HANDLER FO ...
- Spring MVC-表单(Form)标签-错误处理(Error Handling)示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_errors.htm 说明:示例基于Spring MVC 4.1.6. 以下示例显 ...
- An Introduction to Stored Procedures in MySQL 5
https://code.tutsplus.com/articles/an-introduction-to-stored-procedures-in-mysql-5--net-17843 MySQL ...
- [MySQL] Stored Procedures 【转载】
Stored routines (procedures and functions) can be particularly useful in certain situations: When mu ...
- Home / Python MySQL Tutorial / Calling MySQL Stored Procedures in Python Calling MySQL Stored Procedures in Python
f you are not familiar with MySQL stored procedures or want to review it as a refresher, you can fol ...
- Cursors in MySQL Stored Procedures
https://www.sitepoint.com/cursors-mysql-stored-procedures/ After my previous article on Stored Proce ...
- MySql Error: Can't update table in stored function/trigger
MySql Error: Can't update table in stored function/trigger because it is already used by statement w ...
随机推荐
- 开启g++ 编辑器 c++11特性
以前都是在windows下用vs和cvi写C和C++代码,最近练习Linux下的使用. 编译的时候使用C++11的新特性比如auto 和 iteration特性都报不支持,后来在知乎看到答案需要在编译 ...
- 练习2 G题 - 数值统计
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 统计给 ...
- 局部视图(partial)
局部视图(partial) 原文:Partial Views作者:Steve Smith翻译:张海龙(jiechen).刘怡(AlexLEWIS)校对:许登洋(Seay).何镇汐.魏美娟(初见) AS ...
- ligerUI路径问题
ligerUI放mv的Content目录下,路径为固定的并且必须引进一下文件 <link href="~/Content/Ligerui/Source/lib/ligerUI/skin ...
- 转:靠谱的代码和DRY
http://www.cppblog.com/vczh/archive/2014/07/15/207658.html 靠谱的代码和DRY 上次有人来要求我写一篇文章谈谈什么代码才是好代码,是谁我已经忘 ...
- 关于setCharacterEncoding报错
有时候,代码已搬家,就会报这个错,导致这个错误的原因是: HttpServletResponse存在于servlet-api.jar中, 2.3版本的servlet-api.jar中HttpServl ...
- js optimization and performance
http://www.codeproject.com/Articles/551733/Walkthrough-3aplusUsingplustheplusRequireJSplusOpt http:/ ...
- codeforces C. Little Pony and Expected Maximum
题意:一个筛子有m个面,然后扔n次,求最大值的期望; 思路:最大值为1 有1种,2有2n-1种, 3有3n -2n 种 所以为m的时有mn -(m-1)n 种,所以分别求每一种的概率,然后乘以这 ...
- hdu 5067 Harry And Dig Machine
http://acm.hdu.edu.cn/showproblem.php?pid=5067 思路:问题可以转化成:从某一点出发,遍历网格上的一些点,每个点至少访问一次需要的最小时间是多少.这就是经典 ...
- 能分析压缩的日志,且基于文件输入的PYTHON代码实现
确实感觉长见识了. 希望能坚持,并有多的时间用来分析这些思路和模式. #!/usr/bin/python import sys import gzip import bz2 from optparse ...
