SQL Server 事務及回滾事務

2021-09-30 04:56:59 字數 1363 閱讀 2912

第一種:

declare

@ierrorcount

intset

@ierrorcount=0

begin

tran

tran1

insert

into

t1(id, c1) 

values(1

,'1'

)set

@ierrorcount

=@ierrorcount

+@@error

insert

into

t1(id, c1) 

values('

xx2','

2')set

@ierrorcount

=@ierrorcount

+@@error

if@ierrorcount=0

begin

commit

tran

tran1  

--執行事務

endelse

begin

rollback

tran

tran1  

--回滾事務

end

第二種:

begin

trybegin

tran

tran1

insert

into

t1(id, c1) 

values(1

,'1'

)insert

into

t1(id, c1) 

values('

xx2','

2')  --

此句產生錯誤

commit

tran

tran1

endtry

begin

catch 

raiserror

50005n

'出錯了

'rollback

tran

tran1     

---出錯後呼叫回滾

endcatch 

第三種:

setxact_abort 

on--

--語句產生執行時錯誤,則整個事務將終止並回滾。 

begin

tran

insert

into

t1(id, c1) 

values(1

,'1'

)insert

into

t1(id, c1) 

values('

xx2','

2') --

此句產生錯誤時,就會回滾整個事務

commit

tran

sql server儲存過程回滾事務

set nocount on這個很常用 作用 阻止在結果集中返回顯示受t sql語句或則usp影響的行計數資訊。當set oncount on時候,不返回計數,當set nocount off時候,返回計數 即使當set nocount on 時候,也更新 rowcount 當set nocount...

c mysql事務提交及回滾

之前在做有關資料庫的操作時發現,有些內容應該作為乙個事務一起提交,而不是每個都單獨提交,這就需要把這些操作當做乙個事務來處理。而我之前寫過簡單的資料庫的操作,因為mysql預設的是自動提交,我們就需要用到api mysql commit mysql commit mysql mysql,my boo...

事務回滾與手動回滾

一般我們在開發時,在方法或者類上加了 transactional事務註解,然後會用 try catch 將可能會出問題的 塊包起來,在catch裡面處理捕獲的異常,但是,如果在catch裡面沒有把異常丟擲去,此時事務是不會自動回滾的 比如這種情況 這裡既沒有丟擲異常,也沒有手動回滾,在插入流水表之後...