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_error_code
      | SQLSTATE [VALUE] sqlstate_value
      | condition_name
      | SQLWARNING
      | NOT FOUND
      | SQLEXCEPTION  

这里面需要注意几点:

  a、condition_value [,condition_value],这个的话说明可以包括多种情况(方括弧表示可选的),也就是一个handler可以定义成针对多种情况进行相应的 操作;另外condition_value可以包括的值有上面列出来的6种:

1、mysql_error_code,这个表示mysql的错误代码,错误代码是一个数字,完成是由mysql自己定义的,这个值可以参考mysql数据库错误代码及信息

2、SQLSTATE [VALUE] sqlstate_value,这个同错误代码类似形成一一对应的关系,它是一个5个字符组成的字符串,关键的地方是它从ANSI SQL和ODBC这些标准中引用过来的,因此更加标准化,而不像上面的error_code完全是mysql自己定义给自己用的,这个和第一个类似也可以 参考mysql数据库错误代码及信息

3、condtion_name,这个是条件名称,它使用DECLARE...CONDITION语句来定义,这个后面我们会介绍如何定义自己的condition_name。

4、SQLWARNING,表示SQLTATE中的字符串以‘01’起始的那些错误,比如Error: 1311 SQLSTATE: 01000 (ER_SP_UNINIT_VAR)

5、NOT FOUND,表示SQLTATE中的字符串以‘02’起始的那些错误,比如Error: 1329 SQLSTATE: 02000 (ER_SP_FETCH_NO_DATA)

6、SQLEXCEPTION,表示SQLSTATE中的字符串不是以‘00‘、‘01‘、‘02‘ 起始的那些错误,这里‘00‘起始的SQLSTATE其实表示的是成功执行而不是错误,另外两个就是上面的4和5的两种情况。

上面的6种情况其实可以分为两类:

      一类就是比较明确的处理,就是对指定的错误情况进行处理,包括1、2、3这三种方式;

      另一类是对对应类型的错误的 处理,就是对某一群错误的处理,包括4、5、6这三种方式。这个是介绍了condition_value。另外还要注意的一个内容是MySQL在默认情况 下(也就是我们没有定义处理错误的方法-handler)自己的错误处理机制:1、对于SQLWARNING和NOT FOUND的处理方法就是无视错误继续执行,所以在游标的例子里面如果我们没有对repeat的条件判断的那个值做个no_more_products=1的handler来处理,那么循环就会一直下去。2、对于SQLEXCEPTION的话,其默认的处理方法是在出现错误的地方就终止掉了。

b、statement,这个比较简单就是当出现某种条件/错误时,我们要执行的语句,可以是简单的如 SET  var = value这样的简单的语句,也可以是复杂的多行的语句,多行的话可以使用BEGIN  .....  END这里把语句包括在里面(这个好比delphi里面的情况,注意到我们的存储过程也是多行的,所以也要BEGIN .... END)。

c、handler_action,这个表示当执行完上面的statement后,希望执行怎样的动作,这里包括CONTINUE、EXIT、UNDO, 表示继续、退出、撤销(暂时不支持)。这边就是两种动作,其实这两种动作在上面也说过了,CONTINUE就是一个是SQLWARNING和NOT FOUND的默认处理方法,而EXIT就是SQLEXCEPTION的默认处理方法。

另:

condition_name:命名条件

MySQL error code或者SQLSTATE code的可读性太差,所以引入了命名条件:

语法:

    DECLARE condition_name CONDITION FOR condition_value  

    condition_value:
        SQLSTATE [VALUE] sqlstate_value
      | mysql_error_code  

使用:

    # original
    DECLARE CONTINUE HANDLER FOR 1216 MySQL_statements;  

    # changed
    DECLARE foreign_key_error CONDITION FOR 1216;
    DECLARE CONTINUE HANDLER FOR foreign_key_error MySQL_statements;  

示例:

    CREATE PROCEDURE sp_add_location
        (in_location    VARCHAR(30),
         in_address1    VARCHAR(30),
         in_address2    VARCHAR(30),
         zipcode        VARCHAR(10),
         OUT out_status VARCHAR(30))
    BEGIN
        DECLARE CONTINUE HANDLER
            FOR 1062
            SET out_status=‘Duplicate Entry‘;  

        SET out_status=‘OK‘;
        INSERT INTO locations
            (location,address1,address2,zipcode)
        VALUES
            (in_location,in_address1,in_address2,zipcode);
    END;  

时间: 2024-10-12 09:56:52

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 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 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 | NO

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