程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> 關於MYSQL數據庫 >> mysql存儲過程學習筆記--錯誤處理

mysql存儲過程學習筆記--錯誤處理

編輯:關於MYSQL數據庫
定義:
DECLARE {CONTINUE | EXIT} HANDLER FOR {SQLSTATE sqlstate_code| MySQL error code| condition_name} handler_actions
上述定義包括:
· Handler type (CONTINUE, EXIT)//處理類型,繼續或退出
· Handler condition (SQLSTATE, MySQL error code, named condition) //觸發條件
· Hander actions(錯誤觸發的操作)

注意:
a、 exit只退出當前的block。
b、 如果定義了handler_action,會在continue或exit之前執行

發生錯誤的條件有:
· MySQL錯誤代碼
· ANSI-standard SQLSTATE code.
· 命名條件,可自定可使用系統內置的SQLEXCEPTION, SQLWARNING,和 NOT FOUND.
例:
//當錯誤代碼為1062時將duplicate_key的值設為1,並繼續執行當前任務
DECLARE CONTINUE HANDLER FOR 1062 SET duplicate_key=1;
//下面的跟上面的一樣,只是使用的條件為ansi標准錯誤代碼 DECLARE CONTINUE HANDLER FOR SQLSTATE '23000' SET duplicate_key=1;

//當發生SQLEXCEPTION時,將l_error設為1,並繼續 DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET l_error=1;

小提示:
當你在mysql客戶端執行命令並產生錯誤時,會得到MySQL和ANSI的SQLSTATE code,如:
MySQL> CALL nosuch_sp(  );
ERROR 1305 (42000): PROCEDURE sqltune.nosuch_sp does not exist
附,常見錯誤號對照表
MySQL error code SQLSTATE code Error message

1011 HY000 Error on delete of '%s' (errno: %d)
1021 HY000 Disk full (%s); waiting for someone to free some space . . .
1022 23000 Can't write; duplicate key in table '%s'
1027 HY000 '%s' is locked against change
1036 HY000 Table '%s' is read only
1048 23000 Column '%s' cannot be null
1062 23000 Duplicate entry '%s' for key %d
1099 HY000 Table '%s' was locked with a READ lock and can't be updated
1100 HY000 Table '%s' was not locked with LOCK TABLES
1104 42000 The SELECT would examine more than MAX_JOIN_SIZE rows; check your WHERE and use SET SQL_BIG_SELECTS=1 or SET SQL_MAX_JOIN_SIZE=# if the SELECT is okay
1106 42000 Incorrect parameters to procedure '%s'
1114 HY000 The table '%s' is full
1150 HY000 Delayed insert thread couldn't get requested lock for table %s
1165 HY000 INSERT DELAYED can't be used with table '%s' because it is locked with LOCK TABLES
1242 21000 Subquery returns more than 1 row
1263 22004 Column set to default value; NULL supplIEd to NOT NULL column '%s' at row %ld
1264 22003 Out of range value adjusted for column '%s' at row %ld
1265 1000 Data truncated for column '%s' at row %ld
1312 0A000 SELECT in a stored program must have INTO
1317 70100 Query execution was interrupted
1319 42000 Undefined CONDITION: %s
1325 24000 Cursor is already open
1326 24000 Cursor is not open
1328 HY000 Incorrect number of FETCH variables
1329 2000 No data to FETCH
1336 42000 USE is not allowed in a stored program
1337 42000 Variable or condition declaration after cursor or handler declaration
1338 42000 Cursor declaration after handler declaration
1339 20000 Case not found for CASE statement
1348 HY000 Column '%s' is not updatable
1357 HY000 Can't drop a %s from within another stored routine
1358 HY000 GOTO is not allowed in a stored program handler
1362 HY000 Updating of %s row is not allowed in %s trigger
1363 HY000 There is no %s row in %s trigger

命名條件:

DECLARE condition_name CONDITION FOR {SQLSTATE sqlstate_code | MySQL_error_code};
例:
DECLARE foreign_key_error CONDITION FOR 1216; DECLARE CONTINUE HANDLER FOR foreign_key_error MySQL_statements;

優先級:
當同時使用MySQL錯誤碼,標准SQLSTATE錯誤碼,命名條件(SQLEXCEPTION)來定義錯誤處理時,其捕獲順序是(只可捕獲一條錯誤):
MySQL碼->SQLSTATE->命名條件
作用域:
1、包括begni..end;內的語句
DECLARE CONTINUE HANDLER FOR 1048 SELECT 'Attempt to insert a null value';
BEGIN
    INSERT INTO a  VALUES (6,NULL);
END;
若a表第二字段定義為非空,則會觸發1048錯誤
2、若錯誤處理在begin..end內定義,則在之外的語句不會觸發錯誤發生
BEGIN
    BEGIN
       DECLARE CONTINUE HANDLER FOR 1216 select
                'Foreign key constraint violated';
    END;
    INSERT INTO departments (department_name,manager_id,location)
         VALUES ('Elbonian HR','Catbert','Catbertia');
END;

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved