--A账户向B账户转账
alter table bank add constraint check (balance>=10) --添加约束
insert into bank(‘001‘,1000)
insert into bank(‘002‘,10)
//============================================================================
update bank set balance=balance-1000 where cid=‘001‘
update bank set balance=balance+1000 where cid=‘002‘
//由于存在大于等于10的约束,第一句执行失败,数据没有发生变化,第二句执行成功(1010)//
--1.打开事物
begin tran
declare @sum int=0
update bank set balance=balance-1000 where cid=‘001‘
set @sum=@sum+@@error
update bank set balance=balance+1000 where cid=‘002‘
set @sum=@sum+@@error
if @sum<>0
begin
rollback
print ‘回滚‘
end
else
begin
commit tran
print ‘提交‘
end
begin tran
begin try
decalre @sum int=0
update bank set balance=balance-900 where cid=‘001‘
set @sum=@sum+@@error
update bank set balance=balance+900 where cid=‘002‘
set @sum=@sum+@@error
end try
begin catch
rollback
end catch
end tran
原文:http://www.cnblogs.com/wupd2014/p/4970243.html