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

所属分类: 数据库 / Mysql 阅读数: 460
收藏 0 赞 0 分享
下面是示例代码,在发生异常的时候会将异常信息存入日志表中,并继续运行后面的语句.

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

存储过程异常处理示例
复制代码 代码如下:

-- --------------------------------------------------------------------------------
-- 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 > 命名条件
更多精彩内容其他人还在看

Linux安装MySQL教程(二进制分发版)

这篇文章主要为大家详细介绍了Linux安装MySQL教程,二进制分发版,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

MAC下MySQL初始密码忘记怎么办

MySQL初始密码忘记如何解决,这篇文章主要介绍了MAC下MySQL忘记初始密码的解决办法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Oracle10个分区和Mysql分区区别详解

MySQL分区常用的是:range、list、hash、key,Oracle10g分区常用的是:range(范围分区)、list(列表分区)、hash(哈希分区)、range-hash(范围—哈希分区)、range-list(列表—复合分区)。下面通过本文详细给大家介绍Oracl
收藏 0 赞 0 分享

Windows10下mysql 5.7.17 安装配置方法图文教程

这篇文章主要为大家详细介绍了Windows10下mysql5.7.17安装配置方法图文教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Windows下MySQL服务无法停止和删除的解决办法

我在 Windows 操作系统上,使用解压压缩包的方式安装 MySQL。遇到一点问题,下面通过本文给大家分享Windows下MySQL服务无法停止和删除的解决办法,需要的朋友可以参考下
收藏 0 赞 0 分享

MySQL配置文件无法修改的解决方法(Win10)

这篇文章主要为大家详细介绍了MySQL配置文件无法修改的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Ubuntu手动安装mysql5.7.10

这篇文章主要为大家详细介绍了Ubuntu手动安装mysql5.7.10的详细过程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

mysql 5.7.13 安装配置方法图文教程(win10 64位)

这篇文章主要为大家分享了win10 64位下mysql 5.7.13 安装配置方法图文教程,感兴趣的朋友可以参考一下
收藏 0 赞 0 分享

mysql 5.7 安装配置方法图文教程

这篇文章主要为大家分享了mysql 5.7 安装配置方法图文教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

完美解决MySQL通过localhost无法连接数据库的问题

下面小编就为大家带来一篇完美解决MySQL通过localhost无法连接数据库的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多