服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Mysql - MySql存储过程异常处理示例代码分享

MySql存储过程异常处理示例代码分享

2019-12-03 15:19MYSQL教程网 Mysql

在网上查了好多资料,发现关于mysql的异常处理资料都是一些错误号列表,对于平时运行中,我们可能更多的希望能够记录准确的错误消息到日志中

下面是示例代码,在发生异常的时候会将异常信息存入日志表中,并继续运行后面的语句. 

如果您有更好的建议,望不吝赐教. 

存储过程异常处理示例 

复制代码代码如下:


-- -------------------------------------------------------------------------------- 
-- Routine DDL 
-- Note: comments before and after the routine body will not be stored by the server 
-- -------------------------------------------------------------------------------- 
DELIMITER $$ 
CREATE DEFINER=`driveradmin`@`%` PROCEDURE `Merge_BrandProductKey`() 
BEGIN 
DECLARE EXIT HANDLER FOR SQLEXCEPTION 
begin 
insert into t_runninglog values(default,default,'exception in MergeBrandProductKey',concat(@@error_count,' errors')); 
commit; 
end; 
DECLARE CONTINUE HANDLER FOR SQLWARNING 
begin 
insert into t_runninglog values(default,default,'warnings in MergeBrandProductKey',concat(@@warning_count,' warnings')); 
commit; 
end; 
insert into t_runninglog values(default,default,'start in MergeBrandProductKey',''); 
commit; 
-- 任务执行主体 开始 
-- /* 
-- normal 
update brandproductkey as bpk, 
(select bp.brandproductid, bp.brandproductenname, bp.brandid 
from brandproduct as bp 
inner join (select brandid,brandproductid from brandproductdriverrelation group by brandid,brandproductid) as bpr 
on bp.brandid=bpr.brandid and bp.brandproductid = bpr.brandproductid 
) as bpp 
set bpk.brandproductid=bpp.brandproductid 
where bpk.brandproductid = 0 
-- and bpk.computertype = 2 -- 0 
and bpk.brandid = bpp.brandid 
and upper(bpk.brandproductkeyname) = upper(replace(bpp.brandproductenname,' ','')); 
commit; 
insert into t_runninglog values(default,default,'rule normal in MergeBrandProductKey',''); 
commit; 
-- sony rule 1 
-- VPCEA37EC --> (VPCEA37EC/B,VPCEA37EC/L,VPCEA37EC/P,VPCEA37EC/W) 
update brandproductkey as bpk, 
(select bp.brandproductid, bp.brandproductenname, bp.brandid 
from brandproduct as bp 
inner join (select brandid,brandproductid from brandproductdriverrelation group by brandid,brandproductid) as bpr 
on bp.brandid=bpr.brandid and bp.brandproductid = bpr.brandproductid and bp.brandid=60 
) as bpp 
set bpk.brandproductid=bpp.brandproductid 
where bpk.brandproductid = 0 
-- and bpk.computertype = 2 -- 0 
and bpk.brandid = bpp.brandid 
and bpp.brandproductenname like concat(bpk.brandproductkeyname,'/%'); 
commit; 
insert into t_runninglog values(default,default,'rule sony 1 in MergeBrandProductKey',''); 
commit; 
-- sony rule 2 
-- VGN-TZ37N_X --> VGN-TZ37N/X 
update brandproductkey as bpk, 
(select bp.brandproductid, bp.brandproductenname, bp.brandid 
from brandproduct as bp 
inner join (select brandid,brandproductid from brandproductdriverrelation group by brandid,brandproductid) as bpr 
on bp.brandid=bpr.brandid and bp.brandproductid = bpr.brandproductid and bp.brandid=60 
) as bpp 
set bpk.brandproductid=bpp.brandproductid 
where bpk.brandproductid = 0 
-- and bpk.computertype = 2 -- 0 
and bpk.brandid = bpp.brandid 
and upper(bpk.brandproductkeyname) = upper(replace(bpp.brandproductenname,'/','_')); 
commit; 
insert into t_runninglog values(default,default,'rule sony 2 in MergeBrandProductKey',''); 
commit; 
-- lenovo rule 1 
-- ZHAOYANG E45 --> 昭阳E45 
update brandproductkey as bpk, 
(select bp.brandproductid, bp.brandproductenname, bp.brandid,bpr.driverid 
from brandproduct as bp 
inner join (select brandid,brandproductid,max(driverinfoid) as driverid from brandproductdriverrelation group by brandid,brandproductid) as bpr 
on bp.brandid=bpr.brandid and bp.brandproductid = bpr.brandproductid and bp.brandid=37 
) as bpp 
set bpk.brandproductid=bpp.brandproductid 
where bpk.brandproductid = 0 
-- and bpk.computertype = 2 -- 0 
and bpk.brandid = bpp.brandid 
and bpk.brandproductkeyname <> '' 
and instr(bpp.brandproductenname,SUBSTRING_INDEX(bpk.brandproductkeyname,' ',-1))>0 
and bpp.brandproductenname regexp concat('^[^\x00-\xff]+', SUBSTRING_INDEX(bpk.brandproductkeyname,' ',-1),'$'); 
commit; 
insert into t_runninglog values(default,default,'rule lenovo 1 in MergeBrandProductKey',''); 
commit; 
-- HP rule 1 
-- HP Compaq 6535s --> HP Compaq 6535s 笔记本电脑 
update brandproductkey as bpk, 
(select bp.brandproductid, bp.brandproductenname, bp.brandid 
from brandproduct as bp 
inner join (select brandid,brandproductid from brandproductdriverrelation group by brandid,brandproductid) as bpr 
on bp.brandid=bpr.brandid and bp.brandproductid = bpr.brandproductid and bp.brandid=36 
) as bpp 
set bpk.brandproductid = bpp.brandproductid 
where bpk.brandproductid = 0 
-- and bpk.computertype = 2 -- 0 
and bpk.brandid = bpp.brandid 
and bpk.brandproductkeyname <> '' 
and bpp.brandproductenname = concat(bpk.brandproductkeyname,' 笔记本电脑'); 
insert into t_runninglog values(default,default,'rule hp 1 in MergeBrandProductKey',''); 
commit; 
-- HP rule 2 
-- HP Compaq 6535s --> HP Compaq 6535s Notebook PC 
update brandproductkey as bpk, 
(select bp.brandproductid, bp.brandproductenname, bp.brandid 
from brandproduct as bp 
inner join (select brandid,brandproductid from brandproductdriverrelation group by brandid,brandproductid) as bpr 
on bp.brandid=bpr.brandid and bp.brandproductid = bpr.brandproductid and bp.brandid=36 
) as bpp 
set bpk.brandproductid = bpp.brandproductid 
where bpk.brandproductid = 0 
-- and bpk.computertype = 2 -- 0 
and bpk.brandid = bpp.brandid 
and bpk.brandproductkeyname <> '' 
and upper(bpp.brandproductenname) = upper(concat(bpk.brandproductkeyname,' Notebook PC')); 
insert into t_runninglog values(default,default,'rule hp 2 in MergeBrandProductKey',''); 
commit; 
-- */ 
-- 任务执行主体 结束 
insert into t_runninglog values(default,default,'finish in MergeBrandProductKey',''); 
commit; 
END 


有关HANDLER的语法结构如下: 

复制代码代码如下:


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 
Handlers类型: 
, EXIT: 发生错误时退出当前代码块(可能是子代码块或者main代码块) 
, CONTINUE: 发送错误时继续执行后续代码 
condition_value: 
condition_value支持标准的SQLSTATE定义; 
SQLWARNING是对所有以01开头的SQLSTATE代码的速记 
NOT FOUND是对所有以02开头的SQLSTATE代码的速记 
SQLEXCEPTION是对所有没有被SQLWARNING或NOT FOUND捕获的SQLSTATE代码的速记 
除了SQLSTATE值,MySQL错误代码也被支持 
但是对于mysql而言,优先级如下: 
MySQL Error code > SQLSTATE code > 命名条件 

延伸 · 阅读

精彩推荐
  • Mysql详解MySQL中的分组查询与连接查询语句

    详解MySQL中的分组查询与连接查询语句

    这篇文章主要介绍了MySQL中的分组查询与连接查询语句,同时还介绍了一些统计函数的用法,需要的朋友可以参考下 ...

    GALAXY_ZMY5442020-06-03
  • MysqlERROR: Error in Log_event::read_log_event()

    ERROR: Error in Log_event::read_log_event()

    ERROR: Error in Log_event::read_log_event(): read error, data_len: 438, event_type: 2 ...

    MYSQL教程网6412020-03-13
  • Mysql浅谈mysql 树形结构表设计与优化

    浅谈mysql 树形结构表设计与优化

    在诸多的管理类,办公类等系统中,树形结构展示随处可见,本文主要介绍了mysql 树形结构表设计与优化,具有一定的参考价值,感兴趣的小伙伴们可以参...

    小码农叔叔5242021-11-16
  • MysqlMySQL 数据备份与还原的示例代码

    MySQL 数据备份与还原的示例代码

    这篇文章主要介绍了MySQL 数据备份与还原的相关知识,本文通过示例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下...

    逆心2972019-06-23
  • Mysqlmysql 不能插入中文问题

    mysql 不能插入中文问题

    当向mysql5.5插入中文时,会出现类似错误 ERROR 1366 (HY000): Incorrect string value: '\xD6\xD0\xCE\xC4' for column ...

    MYSQL教程网5722019-11-25
  • Mysql解决MySQl查询不区分大小写的方法讲解

    解决MySQl查询不区分大小写的方法讲解

    今天小编就为大家分享一篇关于解决MySQl查询不区分大小写的方法讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起...

    Veir_dev5592019-06-25
  • MysqlMySQL锁的知识点总结

    MySQL锁的知识点总结

    在本篇文章里小编给大家整理了关于MySQL锁的知识点总结以及实例内容,需要的朋友们学习下。...

    别人放弃我坚持吖4362020-12-14
  • MysqlMySQL数据库varchar的限制规则说明

    MySQL数据库varchar的限制规则说明

    本文我们主要介绍了MySQL数据库中varchar的限制规则,并以一个实际的例子对限制规则进行了说明,希望能够对您有所帮助。 ...

    mysql技术网4192019-11-23