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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章