当前位置: 移动技术网 > IT编程>数据库>Mysql > MySQL 错误处理例子[译]

MySQL 错误处理例子[译]

2017年12月12日  | 移动技术网IT编程  | 我要评论
from http://www.devshed.com/c/a/mysql/error-handling-examples/
error handler examples
here are some examples of handler declarations:
if any error condition arises (other than a not found ), continue execution after setting l_error=1 :
declare continue handler for sqlexception
set l_error=1;
if any error condition arises (other than a not found ), exit the current block or stored program after issuing a rollback statement and issuing an error message:
declare exit handler for sqlexception
begin
rollback;
select 'error occurred – terminating';
end;
if mysql error 1062 (duplicate key value) is encountered, continue execution after executing the select statement (which generates a message for the calling program):
declare continue hander for 106 2
select 'duplicate key in index';
if sqlstate 23000 (duplicate key value) is encountered, continue execution after executing the select statement (which generates a message for the calling program):
declare continue hander for sqlstate '23000'
select 'duplicate key in index';
when a cursor fetch or sql retrieves no values, continue execution after setting l_done=1 :
declare continue handler for not
found
set l_done=1;
same as the previous example, except specified using a sqlstate variable rather than a named condition:
declare continue handler for sqlstate '02000 '
set l_done=1;
same as the previous two examples, except specified using a mysql error code variable rather than a named condition or sqlstate variable:
declare continue handler for 1329
set l_done=1;

错误处理例子
有几种错误处理的声明形式:
§ 如果任何错误(不是 not found ) , 设置 l_error 为 1 后继续执行:
declare continue handler for sqlexception
set l_error=1;
§ 如果发生任何错误(不是 not found), 执行 rollback和产生一条错误消息后退出当前块或存储过程。
declare exit handler for sqlexception
begin
rollback;
select 'error occurred – terminating';
end;
§ 如果 mysql 1062错误 (重复的健值 )发生,执行 select语句(向调用程序发一条消息)后继续执行
declare continue hander for 106 2
select 'duplicate key in index';
§ 如果 sqlstate 2300错误 (重复的健值 )发生,执行 select语句(向调用程序发一条消息)后继续执行
declare continue hander for sqlstate '23000'
select 'duplicate key in index';
§ 当游标或者 sql 选择语句没有返回值时,设置 l_done=1 后继续执行
declare continue handler for not
found
set l_done=1;
§ 此例除了用 sqlstate 变量而不是命名条件以外,跟前一个例子一样
declare continue handler for sqlstate '02000 '
set l_done=1;
§ 此例除了用 mysql 的错误码变量而不是命名条件或者 sqlstate 变量以外,跟前两个例子一样
declare continue handler for 1329
set l_done=1;

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网