即使第一个存储过程失败,我如何能够继续执行第二个存储过程(dbo.Test2)?
CREATE PROCEDURE [dbo].[Test4]
AS
BEGIN TRY
DECLARE @RC INT
EXEC @RC = dbo.Test1
EXEC @RC = dbo.Test2
END TRY
/*
Exception handler
*/
BEGIN CATCH
IF xact_state() <> 0
ROLLBACK TRANSACTION
DECLARE
@ErrorMessage varchar(max) = error_message(),
@ErrorSeverity int = error_severity(),
@ErrorState int = error_state()
-- add to stack trace
SET @ErrorMessage = [dbo].[fnStackTrace](error_number(), error_procedure(), error_line(), @ErrorMessage)
-- push the trace up the stack
RAISERROR(@ErrorMessage, @ErrorSeverity, @ErrorState)
END CATCH;
GO
答案 0 :(得分:0)
为此,您应该将两个程序保存在两个不同的交易中。
试试这个
CREATE PROCEDURE [dbo].[Test4]
AS
begin
DECLARE
@ErrorMessage varchar(max),
@ErrorSeverity int ,
@ErrorState int
Begin Transaction
BEGIN TRY
DECLARE @RC INT
EXEC @RC = dbo.Test1
commit transaction
END TRY
BEGIN CATCH
IF xact_state() <> 0
ROLLBACK TRANSACTION
set
@ErrorMessage = error_message(),
@ErrorSeverity = error_severity(),
@ErrorState = error_state()
-- add to stack trace
SET @ErrorMessage = [dbo].[fnStackTrace](error_number(), error_procedure(), error_line(), @ErrorMessage)
-- push the trace up the stack
RAISERROR(@ErrorMessage, @ErrorSeverity, @ErrorState)
END CATCH;
begin transaction
BEGIN TRY
EXEC @RC = dbo.Test2
commit transaction
END TRY
BEGIN CATCH
IF xact_state() <> 0
ROLLBACK TRANSACTION
set
@ErrorMessage = error_message(),
@ErrorSeverity = error_severity(),
@ErrorState = error_state()
SET @ErrorMessage = [dbo].[fnStackTrace](error_number(), error_procedure(), error_line(), @ErrorMessage)
RAISERROR(@ErrorMessage, @ErrorSeverity, @ErrorState)
END CATCH;
end
GO