程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> 關於MYSQL數據庫 >> 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類型:
, 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 > 命名條件
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved