保持存储过程 运行 即使有错误
Keep Stored Procedure running even if there is error
我有一个存储过程需要 运行 即使有一些错误。所以这里有一个小例子
CREATE PROCEDURE FirstProcedure
@SomeParams
AS BEGIN
Declare Db_Cursor Cursor For
Select * From #TmpR
Open Db_Cursor
Fetch Next From Db_cursor Into
@SomaValues
While @@Fetch_Status = 0
Begin
set @ERR = 0
exec [SecondProcedure] @SomaValues
set @ERR = @@ERROR
if @ERR <> 0 begin
-- do some things in case of error
end
Fetch Next From Db_Cursor Into
@SomaValues
End
END
如果第二个程序 returns 出现错误,我将它捕获到 @ERR 变量并做一些其他事情,但我希望光标移动到 运行 直到 [=19= 结束],因为如果不能使用 SecondProcedure 插入 100.000 中的 2-3 行不是很重要?
我能做什么?
使用Try/Catch
CREATE PROCEDURE FirstProcedure
@SomeParams
AS BEGIN
Declare Db_Cursor Cursor For
Select * From #TmpR
Open Db_Cursor
Fetch Next From Db_cursor Into
@SomaValues
While @@Fetch_Status = 0
Begin
begin try
exec [SecondProcedure] @SomaValues
end try
begin catch
SELECT ERROR_NUMBER() AS ErrorNumber
,ERROR_SEVERITY() AS ErrorSeverity
,ERROR_STATE() AS ErrorState
,ERROR_PROCEDURE() AS ErrorProcedure
,ERROR_LINE() AS ErrorLine
,ERROR_MESSAGE() AS ErrorMessage;
end catch
Fetch Next From Db_Cursor Into @SomaValues
End
close db_cursor
deallocate db_cursor
END
我有一个存储过程需要 运行 即使有一些错误。所以这里有一个小例子
CREATE PROCEDURE FirstProcedure
@SomeParams
AS BEGIN
Declare Db_Cursor Cursor For
Select * From #TmpR
Open Db_Cursor
Fetch Next From Db_cursor Into
@SomaValues
While @@Fetch_Status = 0
Begin
set @ERR = 0
exec [SecondProcedure] @SomaValues
set @ERR = @@ERROR
if @ERR <> 0 begin
-- do some things in case of error
end
Fetch Next From Db_Cursor Into
@SomaValues
End
END
如果第二个程序 returns 出现错误,我将它捕获到 @ERR 变量并做一些其他事情,但我希望光标移动到 运行 直到 [=19= 结束],因为如果不能使用 SecondProcedure 插入 100.000 中的 2-3 行不是很重要?
我能做什么?
使用Try/Catch
CREATE PROCEDURE FirstProcedure
@SomeParams
AS BEGIN
Declare Db_Cursor Cursor For
Select * From #TmpR
Open Db_Cursor
Fetch Next From Db_cursor Into
@SomaValues
While @@Fetch_Status = 0
Begin
begin try
exec [SecondProcedure] @SomaValues
end try
begin catch
SELECT ERROR_NUMBER() AS ErrorNumber
,ERROR_SEVERITY() AS ErrorSeverity
,ERROR_STATE() AS ErrorState
,ERROR_PROCEDURE() AS ErrorProcedure
,ERROR_LINE() AS ErrorLine
,ERROR_MESSAGE() AS ErrorMessage;
end catch
Fetch Next From Db_Cursor Into @SomaValues
End
close db_cursor
deallocate db_cursor
END