mysql Error Handling and Raising in Stored Procedures

MySQL的存储过程错误捕获方式和Oracle的有很大的不同。

MySQL中可以使用DECLARE关键字来定义处理程序。其基本语法如下:

DECLARE handler_type HANDLER FOR condition_value[,...] sp_statement 

handler_type: CONTINUE | EXIT  

condition_value:
SQLSTATE [VALUE] sqlstate_value |
condition_name |
SQLWARNING |
NOT FOUND  |
SQLEXCEPTION |
mysql_error_code 

handler_type参数指明错误的处理方式,该参数有2个取值。这1个取值分别是CONTINUE、EXIT

CONTINUE表示遇到错误,执行预先定义的方式,继续向下执行;

EXIT表示遇到错误后,执行预先定义的方式,马上退出;

注意:通常情况下,执行过程中遇到错误应该立刻停止执行下面的语句,并且撤回前面的操作。

condition_value参数指明错误类型,该参数有6个取值。

sqlstate_value和mysql_error_code与条件定义中的是同一个意思。

condition_name是DECLARE定义的条件名称。

SQLWARNING表示所有以01开头的sqlstate_value值。

NOT FOUND表示所有以02开头的sqlstate_value值。

SQLEXCEPTION表示所有没有被SQLWARNING或NOT FOUND捕获的sqlstate_value值。

sp_statement表示一些存储过程或函数的执行语句。

下面是定义处理程序的几种方式。代码如下:

//方法一:捕获sqlstate_value
DECLARE CONTINUE HANDLER FOR SQLSTATE ‘42000‘
SET @info=‘CAN NOT FIND‘;
//方法二:捕获mysql_error_code
DECLARE CONTINUE HANDLER FOR 1148SET @info=‘CAN NOT FIND‘;
//方法三:先定义条件,然后调用
DECLARE can_not_find CONDITION FOR 1146 ;
DECLARE CONTINUE HANDLER FOR can_not_find SET
@info=‘CAN NOT FIND‘;
//方法四:使用SQLWARNING
DECLARE EXIT HANDLER FOR SQLWARNING SET @info=‘ERROR‘;
//方法五:使用NOT FOUND
DECLARE EXIT HANDLER FOR NOT FOUND SET @info=‘CAN NOT FIND‘;
//方法六:使用SQLEXCEPTION
DECLARE EXIT HANDLER FOR SQLEXCEPTION SET @info=‘ERROR‘;

上述代码是6种定义处理程序的方法。

第一种方法是捕获sqlstate_value值。如果遇到sqlstate_value值为42000,执行CONTINUE操作,并且输出"CAN NOT FIND"信息。

第二种方法是捕获mysql_error_code值。如果遇到mysql_error_code值为1148,执行CONTINUE操作,并且输出"CAN NOT FIND"信息。

第三种方法是先定义条件,然后再调用条件。这里先定义can_not_find条件,遇到1148错误就执行CONTINUE操作。

第四种方法是使用SQLWARNING。SQLWARNING捕获所有以01开头的sqlstate_value值,然后执行EXIT操作,并且输出"ERROR"信息。

第五种方法是使用NOT FOUND。NOT FOUND捕获所有以02开头的sqlstate_value值,然后执行EXIT操作,并且输出"CAN NOT FIND"信息。

第六种方法是使用SQLEXCEPTION。SQLEXCEPTION捕获所有没有被SQLWARNING或NOT FOUND捕获的sqlstate_value值。

单句的

DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_row_found = 1;
DECLARE CONTINUE HANDLER FOR 1062
SELECT ‘Error, duplicate key occurred‘;

如果错误捕获的时候要做的操作比较多可以这样

DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
SELECT ‘An error has occurred, operation rollbacked and the stored procedure was terminated‘;
END;

example:

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

参考:

http://www.mysqltutorial.org/mysql-error-handling-in-stored-procedures/

http://www.mysqltutorial.org/mysql-signal-resignal/

http://www.cnblogs.com/lyhabc/p/3793524.html

http://www.zhdba.com/mysqlops/2013/08/31/mysql-handler-2/

时间: 2024-10-10 02:43:18

mysql Error Handling and Raising 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 in stored procedures. When an error occurs inside a stored procedure, it is important to handle it appropriately, such as continuing or exiting the c

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 co

MySQL Error Handling in Stored Procedures

http://www.mysqltutorial.org/mysql-error-handling-in-stored-procedures/ mysql存储过程中的异常处理 定义异常捕获类型及处理方法: DECLARE handler_action HANDLER FOR condition_value [, condition_value] ... statement handler_action: CONTINUE | EXIT | UNDO condition_value: mysql_

An Introduction to Stored Procedures in MySQL 5

https://code.tutsplus.com/articles/an-introduction-to-stored-procedures-in-mysql-5--net-17843 MySQL 5 introduced a plethora of new features - stored procedures being one of the most significant. In this tutorial, we will focus on what they are, and h

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 follow the MySQL stored procedures tutorial. We will create two stored procedures for the demonstration in this tutorial. The first stored procedure gets

[MySQL] Stored Procedures

Stored routines (procedures and functions) can be particularly useful in certain situations: When multiple client applications are written in different languages or work on different platforms, but need to perform the same database operations. When s

转 InnoDB Error Handling

14.20.4 InnoDB Error Handling Error handling in InnoDB is not always the same as specified in the SQL standard. According to the standard, any error during an SQL statement should cause rollback of that statement. InnoDB sometimes rolls back only par

Error Handling 错误处理

This tutorials aims to teach you how to create an error handler for your programs to deal with the clean-up operation when something in the code goes wrong. by:http://lee-mac.com/errorhandling.html What can go Wrong? Errors can arise in many forms: f

pt-osc 变更时遇到 “MySQL error 1300” 报错问题解决

目的 线上一张表的字段长度变更 `sGuid` varchar(255) DEFAULT NULL COMMENT 'sGuid' => `sGuid` varchar(512) DEFAULT NULL COMMENT 'sGuid' 方法 pt-online-schema-change --user=xxxx--password=xxxxxx --host=127.0.0.1 --port=3306 --charset=utf8mb4 D=db_main,t=tb_main --alter