标签:
mysql存储过程也提供了对异常处理的功能:通过定义HANDLER来完成异常声明的实现
语法如下:
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类型:
1, EXIT: 发生错误时退出当前代码块(可能是子代码块或者main代码块)
2, CONTINUE: 发送错误时继续执行后续代码
condition_value:
condition_value支持标准的SQLSTATE定义;
SQLWARNING是对所有以01开头的SQLSTATE代码的速记
NOT FOUND是对所有以02开头的SQLSTATE代码的速记
SQLEXCEPTION是对所有没有被SQLWARNING或NOT FOUND捕获的SQLSTATE代码的速记
除了SQLSTATE值,MySQL错误代码也被支持
但是对于mysql而言,优先级如下:MySQL Error code > SQLSTATE code > 命名条件
使用SQLSTATE还是MySQL Error Code?
1,SALSTATE是标准,貌似会更portable,但是实际上MySQL、DB2、Oracle等等的存储程序语法大相径庭,所以portable的优势不存在2,MySQL error code与SQLSTATE并不是一一对应的,比如很多MySQL error code都映射到同一SQLSTATE code(HY000)
当MySQL客户端碰到错误时,它会报告MySQL error code和相关的SQLSATE code:
mysql > CALL nosuch_sp();
ERROR 1305 (42000): PROCEDURE sqltune.nosuch_sp does not exist
具体的sqlsdate和mysql error code的对应可以在http://dev.mysql.com/doc/的MySQL reference manual的附录B找到完整的最新的error codes
condition_name:命名条件
MySQL error code或者SQLSTATE code的可读性太差,所以引入了命名条件:
语法:
Java代码
DECLARE condition_name CONDITION FOR condition_value
condition_value:
SQLSTATE [VALUE] sqlstate_value
| mysql_error_code
使用:
Java代码
# 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;
用condition_name为错误代码起了个别名。
示例1:Duplicate entry Handler
Sql代码
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;
示例2: Last Row Handler
Sql代码
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;
综合示例:
Sql代码
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 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
原文:http://wwty.iteye.com/blog/698239
标签: