声明异常处理的语法

DECLARE
{EXIT | CONTINUE}
HANDLER FOR
{error-number | SQLSTATE error-string | condition}
SQL statement

上述定义包括:

Handler Type (CONTINUE,EXIT)//处理类型 继续或退出

Handler condition (SQLSTATE,MYSQL ERROR,CONDITION)//触发条件

Handler actions(错误触发的操作)

注意:

1、exit只退出当前的block。exit 意思是当动作成功提交后,退出所在的复合语句。即declare exit handler for... 所在的复合语句。

2、如果定义了handler action,会在continue或exit之前执行

发生错误的条件有:

1、MYSQL错误代码

2、ANSI-standard SQLSTATE code

3、命名条件。可使用系统内置的SQLEXCEPTION,SQLWARNING和NOT FOUND

例1:

当错误代码为1062时将duplicate_key的值设为1,并继续执行当前任务

declare continue handler for 1062 set duplicate_key=1;

下面的跟上面一样,只是使用的条件为ANSI标准错误代码

declare continue handler for sqlstate '23000' set duplicate_key=1;

当发生SQLEXCEPTION时,将L_error设为1,并继续

declare continue handler for SQLEXCEPTION set L_error=1;

小提示:

当你在MYSQL客户端执行命令并产生错误时,会得到MYSQL和ANSI的SQLSTATE code,如:

附常见错误号对照表

MySQL error code SQLSTATE code Error message

1011 HY000 Error on delete of '%s' (errno: %d)
1021 HY000 Disk full (%s); waiting for someone to free some space . . .
1022 23000 Can't write; duplicate key in table '%s'
1027 HY000 '%s' is locked against change
1036 HY000 Table '%s' is read only
1048 23000 Column '%s' cannot be null
1062 23000 Duplicate entry '%s' for key %d
1099 HY000 Table '%s' was locked with a READ lock and can't be updated
1100 HY000 Table '%s' was not locked with LOCK TABLES
1104 42000 The SELECT would examine more than MAX_JOIN_SIZE rows; check your WHERE and use SET SQL_BIG_SELECTS=1 or SET SQL_MAX_JOIN_SIZE=# if the SELECT is okay
1106 42000 Incorrect parameters to procedure '%s'
1114 HY000 The table '%s' is full
1150 HY000 Delayed insert thread couldn't get requested lock for table %s
1165 HY000 INSERT DELAYED can't be used with table '%s' because it is locked with LOCK TABLES
1242 21000 Subquery returns more than 1 row
1263 22004 Column set to default value; NULL supplied to NOT NULL column '%s' at row %ld
1264 22003 Out of range value adjusted for column '%s' at row %ld
1265 1000 Data truncated for column '%s' at row %ld
1312 0A000 SELECT in a stored program must have INTO
1317 70100 Query execution was interrupted
1319 42000 Undefined CONDITION: %s
1325 24000 Cursor is already open
1326 24000 Cursor is not open
1328 HY000 Incorrect number of FETCH variables
1329 2000 No data to FETCH
1336 42000 USE is not allowed in a stored program
1337 42000 Variable or condition declaration after cursor or handler declaration
1338 42000 Cursor declaration after handler declaration
1339 20000 Case not found for CASE statement
1348 HY000 Column '%s' is not updatable
1357 HY000 Can't drop a %s from within another stored routine
1358 HY000 GOTO is not allowed in a stored program handler
1362 HY000 Updating of %s row is not allowed in %s trigger
1363 HY000 There is no %s row in %s trigger

命名条件:

declare conditon_name condition for {SQLSTATE sqlstate_code | MYSQL_ERROR_CODE};

例如:

declare foreign_key_error condition for 1216;

declare continue handler for foreign_key_error mysql_statements;

优先级:当同时使用MYSQL错误码,标准SQLSTATE错误码,命名条件(SQLEXCEPTION)来定义错误处理时,其捕获顺序是(只捕获一条错误):MYSQL码->SQLSTATE->命名条件

作用域:

1、包括begin...end内的语句

declare continue handler for 1048 select 'attempt to insert a null value';
begin
insert into a values(6,null);
end;

若a表第二字段定义为非空,则会触发1048错误

2、若错误处理在begin...end内定义,则在之外的语句不会触发错误发生

BEGIN
BEGIN
DECLARE CONTINUE HANDLER FOR 1216 select 'Foreign key constraint violated';
END;
INSERT INTO departments (department_name,manager_id,location) VALUES ('Elbonian HR','Catbert','Catbertia');
END;

3、能够捕获其它存储过程抛出的错误

下面再通过几个例子来掌握MySQL存储过程中异常处理的使用。

例一:error-number

准备工作

CREATE TABLE `t1` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
CREATE TABLE `t2` (
`cid` INT(10) UNSIGNED NULL DEFAULT NULL,
INDEX `FK__t1` (`cid`),
CONSTRAINT `FK__t1` FOREIGN KEY (`cid`) REFERENCES `t1` (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;

创建存储过程

delimiter //
create procedure a(var1 int)
begin
declare exit handler for 1452 insert into error_log values(
concat('time:',current_date,'.Foreign Key Reference Failure For Value=',var1)
);
insert into t2 values(var1);
end;//

如果有1452错误,则当插入到表error_log这个语句完成后,退出(exit),这里申明异常处理的语句在上面begin...end的复合语句中,所以这里退出,其实就表示退出了该存储过程。

例二:sqlstate error-string

准备工作

CREATE TABLE `t4` (
`id` INT(10) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
create procedure p23()
begin
begin
declare exit handler for sqlstate '23000' set @x2=1;
set @x=1;
insert into t4 values(1);
set @x=2;
end;
begin
declare exit handler for sqlstate '23000' set @x2=9;
insert into t4 values(1);
end;
set @x=3;
end

结果:

例三:

begin
declare exit handler for sqlstate '23000' set @x2=1;
set @x=1;
insert into t4 values(1);
set @x=2;
begin
declare exit handler for sqlstate '23000' set @x2=9;
insert into t4 values(1);
end;
set @x=3;
end

结果:

在执行一次该存储过程,得到结果如下:

error-number的例子

create procedure p22(var1 int)
begin
declare exit handler for 1216 insert into error_log values(
concat('time:' , current_date , '.Foreign Key Reference Failure For Value='
,var1)
);
insert into t3 values(var1);
end;//

sqlstate error-string的例子

create procedure p23()
begin
declare continue handler for sqlstate '23000' set @x2=1;
set @x=1;
insert into t4 values(1);
set @x=2;
insert into t4 values(1);
set @x=3;
end;//

执行结果:

condition的例子

declare 'name' condition for sqlstate '23000';
declare exit handler for 'name' rollback;

declare handler 声明异常处理的语法的更多相关文章

  1. 翻译:DECLARE HANDLER语句(已提交到MariaDB官方手册)

    本文为mariadb官方手册:DECLARE HANDLER的译文. 原文:https://mariadb.com/kb/en/library/declare-handler/我提交到MariaDB官 ...

  2. 『忘了再学』Shell基础 — 19、使用declare命令声明变量类型

    目录 1.declare命令介绍 2.声明数组变量类型 3.声明变量为环境变量 4.声明只读属性 5.补充: 1.declare命令介绍 Shell中所有变量的默认类型是字符串类型,如果你需要进行特殊 ...

  3. Berry 异常处理 1: 语法和字节码设计

    语法 最近在实现 Berry 的异常处理特性,进过初步的调查后决定使用类似 Python 的 try-except 异常处理模式,为此要引入三个新的关键字: try:表示异常捕获块的开始,位于异常捕获 ...

  4. Block的声明与定义语法

    Block的声明 Block的声明与函数指针的声明类似 返回值类型(^变量名)(参数列表) Block的定义 ^返回值类型(参数列表) { 表达式 } 其中: 1 如果返回值类型是void,可以省略 ...

  5. 存储过程/游标/mysql 函数

    存储过程和函数(存储在 mysql数据库中的 proc表,所以检查有没有这个表)存储过程是一种存储程序(如正规语言里的子程序一样),mysql支持有两种:存储过程,在其他SQL语句中可以返回值的函数( ...

  6. MYSQL异常和错误机制

    BEGIN ; ; ; START TRANSACTION; call put_playerbehavior(i_playerid,i_gameid,i_channelid,i_acttime,@a) ...

  7. ORACLE中声明变量:define variable declare

    在sqlplus 环境中,声明变量的关键字:define variable declare 一.define关键字(host变量) host变量的作用是一个替换作用,是主机环境与oracle进行交互的 ...

  8. C语言复杂声明-void (*signal(int sig, void (*handler)(int)))(int);

    问题提出 请分析此声明:void (*signal(int sig, void (*handler)(int)))(int); 求解过程 在对上面的例子作分析之前,我们需要了解C语言的声明优先级,&l ...

  9. Mysql Condition /Handler(异常处理)

    关于介绍,可参见:http://www.cnblogs.com/end/archive/2011/04/01/2001946.html. http://blog.csdn.net/rdarda/art ...

随机推荐

  1. Device Tree(一):背景介绍【转】

    本文转载自:http://www.wowotech.net/device_model/why-dt.html 一.前言 作为一个多年耕耘在linux 2.6.23内核的开发者,各个不同项目中各种不同周 ...

  2. java深入探究16-mybatis

    链接:http://pan.baidu.com/s/1skJ4TNB 密码:koo9 1.引入mybatis jsbc简单易学,上手快,非常灵活构建SQL,效率高但代码繁琐,难以写出高质量的代码 hi ...

  3. mysql基础(4)-数据导入

    如何把数据导入(出)mysql 导出 sql语句         select * from 表名 into outfile "详细路径" fields terminated by ...

  4. 【转载】IntelliJ IDEA WEB项目的部署配置

    最近使用了一下IDEA,确实强大.在部署时出现了些问题.看了这篇文章,对ieda的一些部署配置有了些许了解,在此感谢原博.原文链接:http://blog.csdn.net/z69183787/art ...

  5. jq中同个页面点击事件和回车事件

    button元素,绑定一系列事件的时候,点击-回车-回车,会出现错误. 解决办法,就是把button变成a 详情是,一个页面中有点击按钮,按钮点击之后,出现弹框1,点击弹框1的确定,出现弹框2,点击弹 ...

  6. JMeter常用的调试工具

    JMeter常用的调试工具有如下五种: 1.View Tree:查看结果树.含请求信息.响应信息等,请求头信息中的cookie信息一般默认不会显示,可通过修改JMeter配置参数进行显示.日常大家用的 ...

  7. numpy 往array里添加一个元素

    首先这里p_arr为一个numpy的array,p_为一个元素 p_arr = np.concatenate((p_arr,[p_])) # 先将p_变成list形式进行拼接,注意输入为一个tuple ...

  8. Struts2学习(1)

    struts2概述 1.struts2框架应用javaee三层结构中web层框架. 2.strut2框架在struts1和webwork基础之上发展全新的框架. 3.struts2解决的问题: 4.版 ...

  9. Linux 下安装 jdk-7u75-linux-x64.gz,jdk1.7.0_75,jdk1.7步骤:

    摘要:近来又用到了Linux系统,所以就又新装了一个虚拟机和CentOS 6.4来用,搞开发的程序猿们可能都知道,在现在的很多企业中,生产环境大多都是Linux服务器,并且用的比较多的大都是CentO ...

  10. 【新手专属】IntelliJ IDEA删除项目

    这两天刚从Eclipse转手IDEA,每次都是直接删项目文件,后来百度一下才明白原来应该这样~~~ IntelliJ IDEA 删除项目,共三步: 第一步:记住当前项目文件路径1,然后点击file-- ...