Example of a MySQL stored procedure with parameters:
Http://wwty.iteye.com/blog/698239
The mysql Stored Procedure also provides the exception handling function: the implementation of exception declaration is completed by defining HANDLER.
Syntax:
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 | limit
Handlers type:
1. EXIT: EXIT the current code block when an error occurs (it may be a Subcode block or a main code block)
2. CONTINUE: CONTINUE executing the subsequent code when an error is sent
Condition_value:
Condition_value supports standard SQLSTATE definitions;
SQLWARNING is a shorthand for all SQLSTATE codes starting with 01.
Not found is a shorthand for all SQLSTATE codes starting with 02.
SQLEXCEPTION is a shorthand for all SQLSTATE code NOT captured by SQLWARNING or not found.
Besides the SQLSTATE value, the MySQL error code is also supported.
However, for mysql, the priority is as follows:
MySQL Error code> SQLSTATE code> naming Conditions
Use SQLSTATE or MySQL Error Code?
1. SALSTATE is a standard and seems to be more portable, but in fact, the syntax of MySQL, DB2, Oracle and so on is quite different, so the advantage of portable does not exist.
2. the MySQL error code and SQLSTATE do not correspond one by one. For example, many MySQL error codes are mapped to the same SQLSTATE code (HY000)
When the MySQL client encounters an error, it will report the MySQL error code and related SQLSATE code:
Mysql> CALL nosuch_sp ();
ERROR 1305 (42000): PROCEDURE sqltune. nosuch_sp does not exist
You can find the latest error codes in Appendix B of mysql reference manual at http://dev.mysql.com/doc /.
Condition_name: naming Condition
MySQL error code or SQLSTATE code is too readable, so the naming conditions are introduced:
Syntax:
Java code
- DECLARE condition_name condition for condition_value
- Condition_value:
- SQLSTATE [VALUE] sqlstate_value
- | Mysql_error_code
Usage:
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;
UseCondition_name is an alias for the error code.
Example 1: Duplicate entry Handler
SQL code
- Create procedure sp_add_location
- (In_location VARCHAR (30 ),
- In_address1 VARCHAR (30 ),
- In_address2 VARCHAR (30 ),
- Zipcode VARCHAR (10 ),
- OUT out_status VARCHAR (30 ))
- 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
- SELECT department_id FROM orders;
- 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 (30 ),
- P_manager_surname VARCHAR (30 ),
- P_manager_firstname VARCHAR (30 ),
- P_location 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
- 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 orders ments (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