T-SQL 错误处理与 try catch 回滚,错误行被删除
T-SQL error handling with try catch rollback, error rows were deleted
我有以下结构。我注意到错误编号明显大于 0 的错误行在 while 循环结束时被删除。我不明白错误捕获哪里出了问题。我是否需要在 catch 部分进行第二次提交来提交错误号的更新?
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
删除了提交事务,错误条目不再被删除。谢谢@kevchadders。
我有以下结构。我注意到错误编号明显大于 0 的错误行在 while 循环结束时被删除。我不明白错误捕获哪里出了问题。我是否需要在 catch 部分进行第二次提交来提交错误号的更新?
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
删除了提交事务,错误条目不再被删除。谢谢@kevchadders。