Home Database Mysql Tutorial MySQL存储过程的异常处理方法_MySQL

MySQL存储过程的异常处理方法_MySQL

Jun 01, 2016 pm 12:59 PM
mysql stored procedure Exception handling

本文实例讲述了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)

Copy after login

希望本文所述对大家的MySQL数据库程序设计有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

How to optimize MySQL query performance in PHP?

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

How to use MySQL backup and restore in PHP?

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into a MySQL table using PHP?

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

How to fix mysql_native_password not loaded errors on MySQL 8.4

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

How to use MySQL stored procedures in PHP?

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

How to create a MySQL table using PHP?

The difference between oracle database and mysql The difference between oracle database and mysql May 10, 2024 am 01:54 AM

The difference between oracle database and mysql

How does C++ exception handling support custom error handling routines? How does C++ exception handling support custom error handling routines? Jun 05, 2024 pm 12:13 PM

How does C++ exception handling support custom error handling routines?

See all articles