mysql游标遍历中sql语句出现异常_MySQL中的异常处理,游标⼀、异常处理的理解
ERROR 1062 (23000): Duplicate entry ‘2’ for key ‘PRIMARY’
错误 4位error code(5位sql statis):错误内容
⼆、异常处理的重要性
没有异常处理的存储过程,执⾏过程中⾮常难以预测执⾏结果。
建议:存储过程中加上异常处理部分。
三、异常处理的实现
异常处理的格式:
DECLARECONTINUE/EXITHANDLER FORSQLSTATE ‘23000’ (错误代码)
1.错误是什么?
eg:1062(23000)
2.怎么处理错误?
先执⾏SQL,再执⾏EXIT/CONTINUE
⼩结:
针对什么错误,⾸先执⾏SQL语句,可以是⼀个d;语句块;
根据是continue还是exit,确定是接着执⾏还是退出d;
接着执⾏的话,就是接着执⾏出错的SQL的下⼀条语句;
如果是退出,就退出这个declare所在的begin…end。
例⼦1:
DELIMITER $$
CREATE PROCEDURE small_mistake1(
OUT error VARCHAR(5))
BEGIN
DECLARE CONTINUE HANDLER FOR SQLSTATE '23000' --》这是个异常
SET error = '23000'; //⽤来记录错误发⽣时的⼀些信息,异常捕获、处理
select error;
SET error = '00000';
select error;
INSERT INTO TEAMS VALUES(2,27,'third');
SET error = '23001';
END$$
mysql> call small_mistake1(@a); --》上来直接就是select error,因为先执⾏sql +-------+
| error |
+-------+
| NULL |
+-------+
1 row in set (0.00 sec)
+-------+
| error |
+-------+
| 00000 |
+-------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> select @a; --》
+-------+
| @a |
+-------+
| 23001 |
+-------+
1 row in set (0.00 sec)
===例⼦2:
CREATE TABLE test.t (s1 INT, PRIMARY KEY (s1));
delimiter $$
CREATE PROCEDURE handlerdemo ()
BEGIN
DECLARE CONTINUE HANDLER FOR SQLSTATE '23000' SET @x2 = 1; SET @x = 1;
INSERT INTO test.t VALUES (1); ---》能执⾏
SET @x = 2; --》报错,因为@x==1
INSERT INTO test.t VALUES (1);
SET @x = 3;
CALL handlerdemo()
执⾏结果:
mysql> select @x2; --》捕获到异常,就令x2=1
+------+
| @x2 |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
mysql> select @x; --》set @x=3
+------+
| @x |
+------+
| 3 |
+------+
1 row in set (0.00 sec)
===例⼦3:
DELIMITER $$
CREATE PROCEDURE small_mistake2(
OUT error VARCHAR(5))
BEGIN
DECLARE EXIT HANDLER FOR SQLSTATE '23000' --》EXIT直接退出d
SET error = '23000';
select error;
SET error = '00000';
select error;
INSERT INTO TEAMS VALUES(2,27,'third');
-
server error啥意思-》此语句出错,捕获到异常后,因为是exit,所以不会再执⾏下⾯的set error='23001'语句SET error = '23001';
END$$
DELIMITER ;
mysql> call small_mistake2(@a); --》先执⾏sql
1 row in set (0.00 sec) --》捕获到异常
+-------+
| error |
+-------+
| 00000 |
+-------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> select @a; ---》证明没有执⾏set error='23001'
+-------+
| @a |
+-------+
| 23000 |
+-------+
1 row in set (0.00 sec)
===例⼦4:⼀个begin后⾯可以接多个DECLARE DELIMITER $$
CREATE PROCEDURE small_mistake3(
OUT error VARCHAR(5))
BEGIN
DECLARE CONTINUE HANDLER FOR SQLSTATE '23000' SET error = '23000';
DECLARE CONTINUE HANDLER FOR SQLSTATE '21S01' SET error = '21S01';
INSERT INTO TEAMS VALUES(2,27,'third',5);
END$$
DELIMITER ;
mysql> call small_mistake3(@error);
Query OK, 0 rows affected (0.00 sec)
+--------+
1 row in set (0.00 sec)
四、错误捕获快捷⽅式
异常处理的好处:
①出错不报错
②出错可以进⾏处理;记录出错时的⼀些信息
③处理所有的错误:
===例⼦1:
DELIMITER $$
CREATE PROCEDURE small_mistake5(
OUT error VARCHAR(5))
BEGIN
DECLARE CONTINUE HANDLER FOR SQLWARNING,NOT FOUND,SQLEXCEPTION ---》错误是1开头的赋给SQLWARNING,2开头的NOT FOUND,其他给SQLEXCEPTION SET error = 'xxxxx';
INSERT INTO teams VALUES(2,27,'third');
END$$
DELIMITER ;
mysql> call small_mistake5(@a);
Query OK, 0 rows affected (0.00 sec)
mysql> select @a;
+-------+
| @a |
+-------+
| xxxxx |
+-------+
1 row in set (0.00 sec)
===忽略⼀个错误:
忽略⼀个条件
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论