sql 事务

use studb
go
update bank set currentmoney=currentmoney-1000 where costomername='chen'
set nocount on
print '查看转账事务前的行数信息'
select * from bank

go
begin transaction
declare @errorsum int
set @errorsum=0update bank set currentmoney=currentmoney-1000
where customername='zhangsan'
set @errorsum=@errorsum+@@error
update bank set currentmoney=currentmoney+100 where customername='lisi'
set @errorsum=@errorsum+@@error

print '查看转账事务过程中的余额'
select * from bank

if @errorsum<>0
begin
print '交易失败。回滚事务'
rollback transaction
end
else
begin
print '交易成功,提交成功,写入硬盘,永久保存'
commit transaction
end
go

print '查看转账事务后的余额'
select * from bank
go

原文地址:https://www.cnblogs.com/jcomet/p/1320730.html