bank表:
事务:
1 begin transaction --开启事务 2 declare @err int 3 set @err=0 4 update bank set currentMoney=currentMoney+1000 5 where customerName='张三' 6 set @err=@err+@@error 7 update bank set currentMoney=currentMoney-1000 8 where customerName='陈晨' 9 set @err=@err+@@error 10 if(@err>0) --确定是否有误,确定事务是提交还是撤销 11 begin 12 rollback transaction --回滚事务 13 end 14 else 15 begin 16 commit transaction --提交事务 17 end 18 19 select * from bank
执行结果如下图: