MySQL 毛病處置例子[譯]。本站提示廣大學習愛好者:(MySQL 毛病處置例子[譯])文章只能為提供參考,不一定能成為您想要的結果。以下是MySQL 毛病處置例子[譯]正文
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;
毛病處置例子
有幾種毛病處置的聲明情勢:
§ 假如任何毛病(不是 NOT FOUND ) , 設置 l_error 為 1 後持續履行:
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
SET l_error=1;
§ 假如產生任何毛病(不是 NOT FOUND), 履行 ROLLBACK和發生一條毛病新聞撤退退卻出以後塊或存儲進程。
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
SELECT 'Error occurred – terminating';
END;
§ 假如 MySQL 1062毛病 (反復的健值 )產生,履行 SELECT語句(向挪用法式發一條新聞)後持續履行
DECLARE CONTINUE HANDER FOR 106 2
SELECT 'Duplicate key in index';
§ 假如 SQLSTATE 2300毛病 (反復的健值 )產生,履行 SELECT語句(向挪用法式發一條新聞)後持續履行
DECLARE CONTINUE HANDER FOR SQLSTATE '23000'
SELECT 'Duplicate key in index';
§ 當游標或許 SQL 選擇語句沒有前往值時,設置 l_done=1 後持續履行
DECLARE CONTINUE HANDLER FOR NOT
FOUND
SET l_done=1;
§ 此例除用 SQLSTATE 變量而不是定名前提之外,跟前一個例子一樣
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000 '
SET l_done=1;
§ 此例除用 MySQL 的毛病碼變量而不是定名前提或許 SQLSTATE 變量之外,跟前兩個例子一樣
DECLARE CONTINUE HANDLER FOR 1329
SET l_done=1;