一個SQL Server數據庫事務的使用示例
作者:張超的博客
本文主要介紹了SQL Server數據庫處理事務的一個示例,其中涵蓋了開始事務、提交事務和回滾事務等,希望能夠對您有所幫助。
SQL Server數據庫事務的知識是本文要介紹的內容,本文通過一個例子來介紹了SQL Server數據庫事務的使用方法,接下來我們就一起來了解下這部分內容。
開始事物:begin transaction
提交事物:commit transaction
回滾事物:rollback transaction
例子:
- begin transaction
- declare @errorSum int --定義局部變量
- set @errorSum=0 --初始化臨時變量
- update bank set currentMoneycurrentMoney= currentMoney-1000 where customerName='張三'
- set @errorSum=@errorSum+@@error --累計是否有錯誤
- update bank set currentMoneycurrentMoney= currentMoney+1000 where customerName='李四'
- set @errorSum=@errorSum+@@error --累計是否有錯誤
- if @errorSum<>0 --如果有錯誤
- begin
- rollback transaction
- end
- else
- begin
- commit transaction
- end
- go
關于SQL Server數據庫事務的使用示例就介紹到這里了,希望本次的介紹能夠對您有所收獲!
【編輯推薦】
責任編輯:趙鵬
來源:
博客園