MYSQL存储过程中事务和DECLARE EXIT/CONTINUE HANDLER的使用
1 -- 1、DECLARE EXIT HANDLER FOR SQLEXCEPTION 语句后面可以跟一个 begin end的复合语句块,也可以直接跟一个简单语句例如 :DECLARE EXIT HANDLER FOR SQLEXCEPTION v_succ=0; 3 -- 2、EXIT会在执行异常后执行执行 FOR SQLEXCEPTION 后的语句或块而整个停止下来;CONTINUE选项会在异常后继续执行,从而将id为2的记录写入到数据库中。 4 5 DROP PROCEDURE IF EXISTS p_test; 6 delimiter // 7 CREATE PROCEDURE p_test(OUT v_succ tinyint) 8 BEGIN 9 DECLARE EXIT HANDLER FOR SQLEXCEPTION 10 BEGIN 11 SET v_succ=0; 12 ROLLBACK; 13 END; 14 SET v_succ=1; 15 START TRANSACTION; 16 INSERT INTO tb_test (id) VALUES (1); 17 SIGNAL SQLSTATE '45001' SET MYSQL_ERRNO=2000,MESSAGE_TEXT='故意触发异常'; 18 INSERT INTO tb_test (id) VALUES (2); 19 COMMIT; 20 END;// 21 delimiter ;
相关推荐
bearhoopIT之道 2020-08-16
Kakoola 2020-08-01
凯哥Java 2020-07-04
haidaoxianzi 2020-07-04
delmarks 2020-06-28
aNian 2020-06-16
qingjiuquan 2020-06-07
fraternityjava 2020-06-04
明瞳 2020-06-04
HappyHeng 2020-05-28
88473166 2020-05-14
zkwgpp 2020-05-07
oXiaoChong 2020-05-07
ChainDestiny 2020-05-07
doomvsjing 2020-05-07
xuMelon 2020-05-04
MrLiar 2020-04-27