T-SQL之事务示例

在这里插入图片描述
有约束balance不能小于50

--通过事务执行转账
--打开一个事务
begin transaction
declare @money money=10,@sum int =0
	update bank set balance=balance-@money where account_id='00001'
	set @sum=@sum+@@error
	update bank set balance=balance+@money where account_id='00002'
	set @sum=@sum+@@error
	
	--只要有任何一个语句出错那么最后的sum就不是0
	
	if @sum<>0
		begin
			--表示程序执行出错,回滚
			rollback
		end
	else
		begin
			--表示没有出错,则提交该事务
			commit
		end

猜你喜欢

转载自blog.csdn.net/sqylqq/article/details/106097381