1. 程式人生 > >Sql Server使用事務實現--轉賬問題

Sql Server使用事務實現--轉賬問題

create table Bank
(
  Cid char(4) primary key,
  balance money
)
alter table Bank
add constraint CH_balance check (balance>=10)
insert into Bank values('0001',1000)
insert into Bank values('0002',10)

update Bank set balance=balance-1000 where Cid='0001'
update Bank set balance=balance+1000 where Cid='0002'

begin tran
begin try
declare @errorsum int=0

update Bank set balance=balance-900 where Cid='0001'
set @[email protected][email protected]@error
update Bank set balance=balance+900 where Cid='0002'
set @[email protected][email protected]@error
commit
print '提交!!'
end try
begin catch
  rollback
  print '回滾!'

end catch

開啟"隱式事務":set implicit_Transactions on

關閉“隱式事務”:set implicit_Transactions off