Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 760|回复: 0

MySql存储过程异常处理示例

[复制链接]
  • TA的每日心情
    奋斗
    2024-11-24 15:47
  • 签到天数: 804 天

    [LV.10]以坛为家III

    2053

    主题

    2111

    帖子

    72万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    726782
    发表于 2021-4-17 05:33:59 | 显示全部楼层 |阅读模式

    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类型:
     
    1, EXIT: 发生错误时退出当前代码块(可能是子代码块或者main代码块)
    2, 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 > 命名条件

     

     

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-12-22 18:51 , Processed in 0.064117 second(s), 28 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表