事务
update bank set balance = balance - 100 where cid = ‘0001’
update bank set balance = balance + 100 where cid = ‘0002’
–通过事务执行转帐
–1.打开一个事务
begin transaction
declare @cum int = 0
update bank set balance = balance - 100 where cid = ‘0001’
set @sum = @sum+@@error
update bank set balance = balance + 100 where cid = '0002'
set @sum = @sum+@@error
--只要有任何一条sql语句执行出错,那么最后的@sum就不是0
if @sum<>0
begin
--表示程序执行出错了
--回滚
rollback
end
else
begin
--如果没有出错,则提交该事务
comnit
end
–自动提交事务
–当执行一条sql语句的时候,数据库自动帮我们打一个事务,当语句
执行成功,数据库自动提交事务,执行失败,数据库自动回滚事务
insert into bbb values (www)
–隐式事务,每次执行不重要sql语句的时候,数据库自动帮我们打开一个事务,从早但是
需要我们手动提交事务,或者回滚事务
SET IMPLICIT_TRANSACTIONS ( ON | OFF) 隐式事务
insert into bank values(‘0003’,100000)
–打开隐式事务
SET IMPLICIT_TRANSACTIONS ON
select * from bank
commit
SET IMPLICIT_TRANSACTIONS OFF
–显示事务
begin tran
–…
commit tran