MySql存儲過程異常處理示例


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 > 命名條件

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM