--1、自动提交事务
select * from bank
insert into bank values('李猪猪',1000),('张小宏',200)
update bank set currentMoney=1000
where customerName='张小宏'
declare @money money=1000
update bank set currentMoney=currentMoney-@money
where customerName='李猪猪'
update bank set currentMoney=currentMoney+@money
where customerName='张小宏'
--2、隐式事务
set implicit_transactions on --on开启事务 off关闭事务
update bank set currentMoney=currentMoney-200
where customerName='李猪猪'
rollback --回滚
commit --提交(持久化,永久保存了数据)
Go
--3、显示事务(存储过程)
begin transaction --开启事务
--业务逻辑代码(转账业务)
--自定义变量
--系统(全局变量) @@error 如果结果为0则没有错误
declare @errorNum int=0
declare @money money=1
update bank set currentMoney=currentMoney-@money
where customerName='李猪猪'
set @errorNum=@errorNum+@@ERROR --记录是否报错
update bank set currentMoney=currentMoney+@money
where customerName='张小宏'
set @errorNum=@errorNum+@@ERROR --记录是否报
if @errorNum=0 --没有报错才会提交
begin
commit transaction --提交事务
print '数据提交了'
end
else
begin
rollback transaction --事务回滚
print '数据回滚了'
end
commit transaction --提交事务
rollback transaction --事务回滚
事务笔记.
最新推荐文章于 2023-01-26 15:21:49 发布