mysql存储过程没提供UNDO的handler,处理上一般都用其他方法替代了,下面写出测试环境及方法
表:
CREATE TABLE `a` (
`a` int(11) NOT NULL AUTO_INCREMENT,
`b` int(11) DEFAULT NULL,
PRIMARY KEY (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1
过程:
CREATE ` PROCEDURE `test1`()
BEGIN
declare err INT default 0;
declare continue handler for sqlexception set err=1;
start transaction;
insert a values(1,1);
insert a values(1,1);
insert a values(2,2);
if (err=0) then
commit;
select 'OK';
else
rollback;
select 'err';
end if;
END
character_set_client: utf8
collation_connection: utf8_general_ci
Database Collation: latin1_swedish_ci
执行结果:
) 11:54:12>call test1();
+-----+
| err |
+-----+
| err |
+-----+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
) 11:54:14>select * from a;
Empty set (0.00 sec)
完整的事务处理,如果有一个不OK则都回滚
阅读(1757) | 评论(1) | 转发(0) |