2015-07-02 24 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。

相关问题