MySQL stored procedures also provide functionality for exception handling: the implementation of exception declarations is accomplished by defining handler
The syntax is as follows:
DECLARE Handler_type handler for condition_value[,...] Sp_statement handler_type:continue | EXIT condition_value:sqlstate [value] sqlstate_value | Condition_name | sqlwarning | Not FOUND | SQLEXCEPTION | Mysql_error_code
Handlers Type:
1, exit: Exits the current code block (possibly a child code block or a main code block) when an error occurs
2, CONTINUE: Continue execution of subsequent code when sending an error
Condition_value:
Condition_value supports the SQLSTATE definition of the standard;
SQLWarning is shorthand for all SQLSTATE codes that begin with a
Not FOUND is shorthand for all SQLSTATE codes that begin with a
SQLEXCEPTION is shorthand for all SQLSTATE codes that are not captured by sqlwarning or not FOUND
In addition to the SQLSTATE value, theMySQL error code is also supported
But for MySQL, the priority is as follows:
MySQL Error code > SQLSTATE code > naming criteria
Use SQLState or MySQL Error Code?
1,salstate is the standard, seemingly will be more portable, but actually MySQL, DB2, Oracle and so on the storage program syntax is very different, so portable advantage does not exist
2,mysql error code and SQLSTATE are not one by one corresponding, such as many MySQL error code are mapped to the same SQLState code ( HY000)
When the MySQL client encounters an error, it reports the MySQL error code and the associated Sqlsate code:
MySQL > Call nosuch_sp ();
ERROR 1305 (42000): PROCEDURE sqltune.nosuch_sp does not exist
The specific sqlsdate and MySQL error code correspondence can be found in the http://dev.mysql.com/doc/MySQL reference manual Appendix B to find the complete latest error codes
Condition_name: Naming criteria
MySQL error code or SQLSTATE code is poorly readable, so a naming condition is introduced:
Grammar:
Java code
- DECLARE condition_name Condition for Condition_value
- Condition_value:
- SQLSTATE [VALUE] Sqlstate_value
- | Mysql_error_code
Use:
Java code
- # Original
- DECLARE CONTINUE HANDLER for 1216 mysql_statements;
- # changed
- DECLARE foreign_key_error CONDITION for 1216;
- DECLARE CONTINUE HANDLER for Foreign_key_error mysql_statements;
An individual name was used for the error code with Condition_name.
Example 1:duplicate entry Handler
SQL code
- CREATE PROCEDURE sp_add_location
- (In_location VARCHAR (),
- In_address1 VARCHAR (+),
- In_address2 VARCHAR (+),
- ZipCode VARCHAR (Ten),
- out Out_status VARCHAR (+))
- BEGIN
- DECLARE CONTINUE HANDLER
- For 1062
- SET out_status=' Duplicate Entry ';
- SET out_status=' OK ';
- INSERT into locations
- (Location,address1,address2,zipcode)
- VALUES
- (In_location,in_address1,in_address2,zipcode);
- END;
Example 2:last Row Handler
SQL code
- CREATE PROCEDURE sp_not_found ()
- READS SQL DATA
- BEGIN
- DECLARE l_last_row INT DEFAULT 0;
- DECLARE l_dept_id INT:
- DECLARE c_dept CURSOR for
- SELECT department_id from departments;
- DECLARE CONTINUE HANDLER for not FOUND SET l_last_row=1;
- OPEN c_dept;
- Dept_cursor:loop
- FETCH c_dept into l_dept_id;
- IF (l_last_row=1) Then
- LEAVE Dept_cursor;
- END IF;
- END LOOP dept_cursor;
- CLOSE c_dept;
- END;
Comprehensive Example:
SQL code
- CREATE PROCEDURE sp_add_department
- (P_department_name VARCHAR (),
- P_manager_surname VARCHAR (+),
- P_manager_firstname VARCHAR (+),
- P_location VARCHAR (+),
- Out p_sqlcode INT,
- out P_status_message VARCHAR (+))
- 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 employee_id from employees
- WHERE surname=UPPER (p_manager_surname)
- and firstname=UPPER (p_manager_firstname);
- /* 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/* Got Manager ID, we can try and Insert * /
- INSERT into departments (Department_name, manager_id, location)
- VALUES (UPPER (p_department_name), l_manager_id, UPPER (p_location));
- 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
- Original: http://wwty.iteye.com/blog/698239
exception handling of MySQL stored procedure