当前位置: 首页 > 图文教程 > 数据库 > MYSQL > MySQL 错误处理例子[译]

MYSQL
mysql 常用数据库语句 小练习
MYSQL ERROR 1045 (28000): Access denied for user (using password: YES)问题的解决
mysql 字符集的系统变量说明
MySQL 在触发器里中断记录的插入或更新?
将MySQL数据库移植为PostgreSQL
mysql 操作总结 INSERT和REPLACE
linux mysql忘记密码的多种解决或Access denied for user ''root''@''localhost''
运用mysqldump 工具时需要注意的问题
mysql 优化日记
MySQL 字符串函数大全
mysql 截取指定的两个字符串之间的内容
MySQL 备份还原数据库批处理
mysql 数据库中my.ini的优化 2G内存针对站多 抗压型的设置
Mysql 数字类型转换函数
mysql 动态生成测试数据
mysql 显示SQL语句执行时间的代码
mysql 设置查询缓存
MYSQL explain 执行计划
MySQL 有输入输出参数的存储过程实例
巧用mysql提示符prompt清晰管理数据库的方法

MYSQL 中的 MySQL 错误处理例子[译]


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-10   浏览: 156 ::
收藏到网摘: n/a

MySQL 错误处理例子,国外人写的,大家可以参考下。 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;