WEB开发网
开发学院数据库MSSQL Server Sql Server使用事务实现转账问题 阅读

Sql Server使用事务实现转账问题

 2012-11-08 19:49:43 来源:WEB开发网   
核心提示: create table Bank(Cid char(4) primary key,balance money)alter table Bankadd constraint CH_balance check (balance>=10)insert into Bank values('0001',

 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 @errorSum=@errorSum+@@error
update Bank set balance=balance+900 where Cid='0002'
set @errorSum=@errorSum+@@error
commit
print '提交!!'
end try
begin catch
rollback
print '回滚!'
end catch

打开"隐式事务":set implicit_Transactions on
关闭“隐式事务”:set implicit_Transactions off

Tags:Sql Server 使用

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接