[笔记]MySQL 错误errors 处理例子_MySQL, Oracle及数据库讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  MySQL, Oracle及数据库讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2080 | 回复: 0   主题: [笔记]MySQL 错误errors 处理例子        下一篇 
kim
注册用户
等级:中校
经验:1729
发帖:222
精华:0
注册:2011-7-21
状态:离线
发送短消息息给kim 加好友    发送短消息息给kim 发消息
发表于: IP:您无权察看 2014-12-8 16:51:03 | [全部帖] [楼主帖] 楼主

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;

--转自 北京联动北方科技有限公司




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论