程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> MySQL存儲進程的異常處置辦法

MySQL存儲進程的異常處置辦法

編輯:MySQL綜合教程

MySQL存儲進程的異常處置辦法。本站提示廣大學習愛好者:(MySQL存儲進程的異常處置辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是MySQL存儲進程的異常處置辦法正文


本文實例講述了MySQL存儲進程的異常處置辦法。分享給年夜家供年夜家參考。詳細以下:

mysql>
mysql> delimiter $$
mysql>
mysql> CREATE PROCEDURE myProc
  ->   (p_first_name     VARCHAR(30),
  ->    p_last_name      VARCHAR(30),
  ->    p_city        VARCHAR(30),
  ->    p_description     VARCHAR(30),
  ->    OUT p_sqlcode     INT,
  ->    OUT p_status_message VARCHAR(100))
  -> BEGIN
  ->
  -> /* START Declare Conditions */
  ->
  ->  DECLARE duplicate_key CONDITION FOR 1062;
  ->  DECLARE foreign_key_violated CONDITION FOR 1216;
  ->
  -> /* END Declare Conditions */
  ->
  -> /* START Declare variables and cursors */
  ->
  ->   DECLARE l_manager_id    INT;
  ->
  ->   DECLARE csr_mgr_id CURSOR FOR
  ->    SELECT id
  ->     FROM employee
  ->    WHERE first_name=p_first_name
  ->       AND last_name=p_last_name;
  ->
  -> /* END Declare variables and cursors */
  ->
  -> /* START Declare Exception Handlers */
  ->
  ->  DECLARE CONTINUE HANDLER FOR duplicate_key
  ->   BEGIN
  ->    SET p_sqlcode=1052;
  ->    SET p_status_message='Duplicate key error';
  ->   END;
  ->
  ->  DECLARE CONTINUE HANDLER FOR foreign_key_violated
  ->   BEGIN
  ->    SET p_sqlcode=1216;
  ->    SET p_status_message='Foreign key violated';
  ->   END;
  ->
  ->  DECLARE CONTINUE HANDLER FOR not FOUND
  ->   BEGIN
  ->    SET p_sqlcode=1329;
  ->    SET p_status_message='No record found';
  ->   END;
  ->
  -> /* END Declare Exception Handlers */
  ->
  -> /* START Execution */
  ->
  ->  SET p_sqlcode=0;
  ->  OPEN csr_mgr_id;
  ->  FETCH csr_mgr_id INTO l_manager_id;
  ->
  ->  IF p_sqlcode<>0 THEN      /* Failed to get manager id*/
  ->   SET p_status_message=CONCAT(p_status_message,' when fetching manager id');
  ->  ELSE
  ->   INSERT INTO employee (first_name,id,city)
  ->   VALUES(p_first_name,l_manager_id,p_city);
  ->
  ->   IF p_sqlcode<>0 THEN   /* Failed to insert new department */
  ->    SET p_status_message=CONCAT(p_status_message,
  ->              ' when inserting new department');
  ->   END IF;
  ->  END IF;
  ->
  ->  CLOSE csr_mgr_id;
  ->
  -> /* END Execution */
  ->
  -> END$$
Query OK, 0 rows affected (0.02 sec)
mysql>
mysql> delimiter ;
mysql> set @myCode = 0;
Query OK, 0 rows affected (0.00 sec)
mysql> set @myMessage = 0;
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> call myProc('Jason','Martin','New City','New Description',@myCode,@myMessage);
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> select @myCode, @myMessage;
+---------+------------+
| @myCode | @myMessage |
+---------+------------+
| 0    | NULL    |
+---------+------------+
1 row in set (0.00 sec)
mysql>
mysql> drop procedure myProc;
Query OK, 0 rows affected (0.00 sec)

願望本文所述對年夜家的MySQL數據庫法式設計有所贊助。

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