SQL中使用事務

2021-06-06 12:15:58 字數 896 閱讀 9459

begin transaction 開始事務

rollback transaction 提交事務

commit transaction 回滾事務

use pubs   

declare @interrorcode int  

begin tran   

update authors     set phone = '415 354-9866'   where au_id = '724-80-9391'  

select @interrorcode = @@error   

if (@interrorcode <> 0) goto problem   

update publishers    set city = 'calcutta', country = 'india'    where pub_id = '9999'   

select @interrorcode = @@error   

if (@interrorcode <> 0) goto problem   

commit tran     

problem:   

if (@interrorcode <> 0) begin  

print 'unexpected error occurred!'  

rollback tran   

end  

在真正處理開始之前,使用begin tran標記:下面所有的動作作為乙個事務。裡邊包含兩個update語句。如果沒有出錯,當執行到commit tran所有的改變被提交到資料庫,儲存過程完成。如果在執行update出錯,通過檢測@@error,跳轉到problem標籤,顯示錯誤資訊,回滾這期間所有的修改。

注意:確保begin tran 和commit 或 rollback成對出現

SQL使用事務

begin transaction 開始事務 declare errorsun int 定義錯誤計數器 set errorsun 0 沒錯為0 update a set id 232 where a 1 事務操作sql語句 set errorsun errorsun error 累計是否有錯 upd...

tp中使用事務

事務是為了防止,多個操作,其中有失敗,資料有部分被執行成功的時候使用的。比如,銀行,使用者轉賬。張三錢扣了,結果李四錢還增加!這個時候需要使用事務,確保張三錢扣了,李四的錢也增加,才真正的成功!確保資料的一致性!啟動事務 access public return void public functi...

tp中使用事務

事務是為了防止,多個操作,其中有失敗,資料有部分被執行成功的時候使用的。比如,銀行,使用者轉賬。張三錢扣了,結果李四錢還增加!這個時候需要使用事務,確保張三錢扣了,李四的錢也增加,才真正的成功!確保資料的一致性!啟動事務 access public return void public functi...