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