delimiter //
create procedure t_insert_table()
begin
/** 标记是否出错 */
declare t_error int default 0;
/** 如果出现sql异常,则将t_error设置为1后继续执行后面的操作 */
declare continue handler for sqlexception set t_error=1; -- 出错处理
/** 显式的开启事务,它开启后,事务会暂时停止自动提交*/
-- start transaction;
/** 关闭事务的自动提交 */
set autocommit = 0;
insert into aaa value(1,2);
insert into aaa value(1,2);
insert into aaa value(3,4);
/** 标记被改变,表示事务应该回滚 */
if t_error=1 then
rollback; -- 事务回滚
else
commit; -- 事务提交
end if;
end//
delimiter ;
?
原文:http://chendaiming.iteye.com/blog/2191627