1.什么是事务
事务就是被绑定在一起作为一个逻辑工作单元的SQL语句组,如果任何一个语句操作失败那么整个操作就被失败,进而回滚到操作前状态,或者是上个节点。为了确保要么执行,要么不执行,就可以使用事务。要将一组语句作为事务考虑,就需要通过ACID测试,即原子性,一致性,隔离性和持久性。
2.事务用法
begin transaction tran_Charge
declare @err_nums int
set @err_nums=0
begin try
Update Customers set Remain =@remain where id=@idOut
set @err_nums+@@error
Update Customers set remain=@remain where id=@idIn
end try
begin catch
print ‘错误编号:’ +convert(varchar,error_number()) +‘错误消息’+error_message
set @err_nums=@err_nums+1
end catch
if(@err_nums>0)–说明语句有错
rollback transaction tran_Charge–回滚
else
commit transaction tran_Charge–提交
select * from Customers