MySQL 錯(cuò)誤處理例子[譯]
更新時(shí)間:2009年10月21日 22:05:57 作者:
MySQL 錯(cuò)誤處理例子,國外人寫的,大家可以參考下。
from http://www.devshed.com/c/a/MySQL/Error-Handling-Examples/
Error Handler Examples
Here are some examples of handler declarations:
If any error condition arises (other than a NOT FOUND ), continue execution after setting l_error=1 :
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
SET l_error=1;
If any error condition arises (other than a NOT FOUND ), exit the current block or stored program after issuing a ROLLBACK statement and issuing an error message:
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
SELECT 'Error occurred – terminating';
END;
If MySQL error 1062 (duplicate key value) is encountered, continue execution after executing the SELECT statement (which generates a message for the calling program):
DECLARE CONTINUE HANDER FOR 106 2
SELECT 'Duplicate key in index';
If SQLSTATE 23000 (duplicate key value) is encountered, continue execution after executing the SELECT statement (which generates a message for the calling program):
DECLARE CONTINUE HANDER FOR SQLSTATE '23000'
SELECT 'Duplicate key in index';
When a cursor fetch or SQL retrieves no values, continue execution after setting l_done=1 :
DECLARE CONTINUE HANDLER FOR NOT
FOUND
SET l_done=1;
Same as the previous example, except specified using a SQLSTATE variable rather than a named condition:
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000 '
SET l_done=1;
Same as the previous two examples, except specified using a MySQL error code variable rather than a named condition or SQLSTATE variable:
DECLARE CONTINUE HANDLER FOR 1329
SET l_done=1;
錯(cuò)誤處理例子
有幾種錯(cuò)誤處理的聲明形式:
§ 如果任何錯(cuò)誤(不是 NOT FOUND ) , 設(shè)置 l_error 為 1 后繼續(xù)執(zhí)行:
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
SET l_error=1;
§ 如果發(fā)生任何錯(cuò)誤(不是 NOT FOUND), 執(zhí)行 ROLLBACK和產(chǎn)生一條錯(cuò)誤消息后退出當(dāng)前塊或存儲(chǔ)過程。
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
SELECT 'Error occurred – terminating';
END;
§ 如果 MySQL 1062錯(cuò)誤 (重復(fù)的健值 )發(fā)生,執(zhí)行 SELECT語句(向調(diào)用程序發(fā)一條消息)后繼續(xù)執(zhí)行
DECLARE CONTINUE HANDER FOR 106 2
SELECT 'Duplicate key in index';
§ 如果 SQLSTATE 2300錯(cuò)誤 (重復(fù)的健值 )發(fā)生,執(zhí)行 SELECT語句(向調(diào)用程序發(fā)一條消息)后繼續(xù)執(zhí)行
DECLARE CONTINUE HANDER FOR SQLSTATE '23000'
SELECT 'Duplicate key in index';
§ 當(dāng)游標(biāo)或者 SQL 選擇語句沒有返回值時(shí),設(shè)置 l_done=1 后繼續(xù)執(zhí)行
DECLARE CONTINUE HANDLER FOR NOT
FOUND
SET l_done=1;
§ 此例除了用 SQLSTATE 變量而不是命名條件以外,跟前一個(gè)例子一樣
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000 '
SET l_done=1;
§ 此例除了用 MySQL 的錯(cuò)誤碼變量而不是命名條件或者 SQLSTATE 變量以外,跟前兩個(gè)例子一樣
DECLARE CONTINUE HANDLER FOR 1329
SET l_done=1;
Error Handler Examples
Here are some examples of handler declarations:
If any error condition arises (other than a NOT FOUND ), continue execution after setting l_error=1 :
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
SET l_error=1;
If any error condition arises (other than a NOT FOUND ), exit the current block or stored program after issuing a ROLLBACK statement and issuing an error message:
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
SELECT 'Error occurred – terminating';
END;
If MySQL error 1062 (duplicate key value) is encountered, continue execution after executing the SELECT statement (which generates a message for the calling program):
DECLARE CONTINUE HANDER FOR 106 2
SELECT 'Duplicate key in index';
If SQLSTATE 23000 (duplicate key value) is encountered, continue execution after executing the SELECT statement (which generates a message for the calling program):
DECLARE CONTINUE HANDER FOR SQLSTATE '23000'
SELECT 'Duplicate key in index';
When a cursor fetch or SQL retrieves no values, continue execution after setting l_done=1 :
DECLARE CONTINUE HANDLER FOR NOT
FOUND
SET l_done=1;
Same as the previous example, except specified using a SQLSTATE variable rather than a named condition:
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000 '
SET l_done=1;
Same as the previous two examples, except specified using a MySQL error code variable rather than a named condition or SQLSTATE variable:
DECLARE CONTINUE HANDLER FOR 1329
SET l_done=1;
錯(cuò)誤處理例子
有幾種錯(cuò)誤處理的聲明形式:
§ 如果任何錯(cuò)誤(不是 NOT FOUND ) , 設(shè)置 l_error 為 1 后繼續(xù)執(zhí)行:
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
SET l_error=1;
§ 如果發(fā)生任何錯(cuò)誤(不是 NOT FOUND), 執(zhí)行 ROLLBACK和產(chǎn)生一條錯(cuò)誤消息后退出當(dāng)前塊或存儲(chǔ)過程。
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
SELECT 'Error occurred – terminating';
END;
§ 如果 MySQL 1062錯(cuò)誤 (重復(fù)的健值 )發(fā)生,執(zhí)行 SELECT語句(向調(diào)用程序發(fā)一條消息)后繼續(xù)執(zhí)行
DECLARE CONTINUE HANDER FOR 106 2
SELECT 'Duplicate key in index';
§ 如果 SQLSTATE 2300錯(cuò)誤 (重復(fù)的健值 )發(fā)生,執(zhí)行 SELECT語句(向調(diào)用程序發(fā)一條消息)后繼續(xù)執(zhí)行
DECLARE CONTINUE HANDER FOR SQLSTATE '23000'
SELECT 'Duplicate key in index';
§ 當(dāng)游標(biāo)或者 SQL 選擇語句沒有返回值時(shí),設(shè)置 l_done=1 后繼續(xù)執(zhí)行
DECLARE CONTINUE HANDLER FOR NOT
FOUND
SET l_done=1;
§ 此例除了用 SQLSTATE 變量而不是命名條件以外,跟前一個(gè)例子一樣
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000 '
SET l_done=1;
§ 此例除了用 MySQL 的錯(cuò)誤碼變量而不是命名條件或者 SQLSTATE 變量以外,跟前兩個(gè)例子一樣
DECLARE CONTINUE HANDLER FOR 1329
SET l_done=1;
相關(guān)文章
mysql 中 replace into 與 insert into on duplicate key update 的
這篇文章主要介紹了mysql 中 replace into 與 insert into on duplicate key update 的用法和不同點(diǎn),結(jié)合實(shí)例形式分析了replace into 與 insert into on duplicate key update的功能、基本用法與操作注意事項(xiàng),需要的朋友可以參考下2020-02-02關(guān)于MyBatis連接MySql8.0版本的配置問題
這篇文章主要介紹了關(guān)于MyBatis連接MySql8.0版本的配置問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12MySQL如何比較兩個(gè)表數(shù)據(jù)的差異
這篇文章主要介紹了MySQL比較兩個(gè)表數(shù)據(jù)的差異,這些方式可以根據(jù)具體需求和數(shù)據(jù)結(jié)構(gòu)選擇合適的方法來比較兩個(gè)表的數(shù)據(jù)差異,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10mysql 按照時(shí)間段來獲取數(shù)據(jù)的方法
mysql 按照時(shí)間段來獲取數(shù)據(jù)的方法,需要的朋友可以參考一下2013-03-03mysql定時(shí)任務(wù)(event事件)實(shí)現(xiàn)詳解
這篇文章主要介紹了mysql定時(shí)任務(wù)(event事件)實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08