2015-07-02 26 views
1

我有以下結構。我注意到在while循環結束時刪除錯誤號錯誤> 0的錯誤行被刪除。我不明白我在錯誤捕捉方面出錯的地方。我是否需要catch部分中的第二次提交來提交錯誤號的更新?用try catch回滾T-SQL錯誤處理,錯誤行被刪除

while @@FETCH_STATUS = 0 
    begin try 
    begin transaction 
     [...insert something into a table here...] 
     set @countrec = @countrec + @@rowcount 

    update Alarmtable 
     set success = 0 
     where Recno = @recno 

    commit transaction 

    end try 
    begin catch 
    if @@trancount > 0 rollback transaction 

    select @error = error_number() 
     , @errormsg = error_message() 

    update Alarmtable 
     set success = @error 
    where Recno = @recno 

    if @@trancount > 0 commit transaction 

    end catch 

    fetch next from listofrecords into 
     @recno, @alarmcontent 

end /* while */ 

close listofrecords 
deallocate listofrecords 

delete Alarmtable 
    where success = 0 
+2

在catch塊中刪除'if @@ trancount> 0 commit transaction'。我不認爲有任何需要它在那裏 – kevchadders

+0

你可以發佈你的整個代碼,包括插入'Alarmtable'的代碼 – ughai

+0

刪除提交在catch塊中爲我工作。由於我不明白的原因,這是導致錯誤行被刪除的語句。我認爲捕獲的第一次回滾將會清空所有的東西。我的額外提交事務實際上提交了一個零成功列。成功列中的錯誤號不需要提交即可正確保存。謝謝@kevchadders。 – Pho

回答

0

刪除了提交事務,錯誤條目不再被刪除。謝謝@kevchadders。

相關問題