|
/* |
|
OUTPUT: |
|
This is our MAIN Procedure. |
|
This is our TestA.usp_Proc2 |
|
This is our TestB.usp_Proc3 |
|
This is our TestB.usp_Proc4 |
|
|
|
Msg 50000 Level 16 State 1 Line 34 |
|
Error 508134, Level 16, State 1, Procedure [dbo].[usp_Proc1], Line 24, Message: Divide by zero error encountered., |
|
StackTrace: |
|
Procedure: [TestA].[usp_Proc2], Line: 24 |
|
Procedure: [TestB].[usp_Proc3], Line: 20 |
|
Procedure: [TestC].[usp_Proc4], Line: 37 |
|
Procedure: [TestC].[usp_Proc4], Line: 17 |
|
*/ |
|
|
|
ALTER PROCEDURE dbo.usp_Proc1 AS |
|
BEGIN |
|
SET NOCOUNT ON; |
|
|
|
-- error catch variables |
|
DECLARE |
|
@ErrorMessage NVARCHAR(4000), @ErrorNumber INT, @ErrorSeverity INT, @ErrorState INT = 0 |
|
,@proc_name nvarchar(256) = CONCAT(QUOTENAME(OBJECT_SCHEMA_NAME(@@PROCID)),'.', QUOTENAME(OBJECT_NAME(@@PROCID))); |
|
|
|
BEGIN TRY |
|
PRINT 'This is our MAIN Procedure.' |
|
EXECUTE TestA.usp_Proc2 --execute the Stored Procedure |
|
PRINT '*The error halted the procedure, but our MAIN code can continue.' |
|
PRINT 1/0 --generate another "Divide By Zero" error. |
|
PRINT 'We will not make it to this line.' |
|
END TRY |
|
BEGIN CATCH |
|
EXEC dbo.ErrorHandler @proc_name, @ErrorMessage OUTPUT, @ErrorSeverity OUTPUT, @ErrorState OUTPUT; |
|
RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState); |
|
END CATCH |
|
END; |
|
GO |
|
|
|
ALTER PROCEDURE TestA.usp_Proc2 AS |
|
BEGIN |
|
SET NOCOUNT ON; |
|
|
|
-- error catch variables |
|
DECLARE |
|
@ErrorMessage NVARCHAR(4000), @ErrorNumber INT, @ErrorSeverity INT, @ErrorState INT = 0 |
|
,@proc_name nvarchar(256) = CONCAT(QUOTENAME(OBJECT_SCHEMA_NAME(@@PROCID)),'.', QUOTENAME(OBJECT_NAME(@@PROCID))); |
|
|
|
BEGIN TRY |
|
PRINT 'This is our TestA.usp_Proc2' |
|
/* let's take up some space and chagne the line number */ |
|
/* let's take up some space and chagne the line number */ |
|
/* let's take up some space and chagne the line number */ |
|
EXEC TestB.usp_Proc3; |
|
PRINT 1/0 --generate a "Divide By Zero" error. |
|
|
|
/* let's take up some space and change the line number */ |
|
/* let's take up some space and chagne the line number */ |
|
PRINT 'We will not make it to this line.' |
|
END TRY |
|
BEGIN CATCH |
|
EXEC dbo.ErrorHandler @proc_name, @ErrorMessage OUTPUT, @ErrorSeverity OUTPUT, @ErrorState OUTPUT; |
|
RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState); |
|
END CATCH |
|
END; |
|
GO |
|
|
|
ALTER PROCEDURE TestB.usp_Proc3 AS |
|
BEGIN |
|
SET NOCOUNT ON; |
|
|
|
-- error catch variables |
|
DECLARE |
|
@ErrorMessage NVARCHAR(4000), @ErrorNumber INT, @ErrorSeverity INT, @ErrorState INT = 0 |
|
,@proc_name nvarchar(256) = CONCAT(QUOTENAME(OBJECT_SCHEMA_NAME(@@PROCID)),'.', QUOTENAME(OBJECT_NAME(@@PROCID))); |
|
|
|
BEGIN TRY |
|
PRINT 'This is our TestB.usp_Proc3' |
|
/* let's take up some space and change the line number */ |
|
/* let's take up some space and change the line number */ |
|
EXEC TestC.usp_Proc4; |
|
/* let's take up some space and change the line number */ |
|
PRINT 'We will not make it to this line.' |
|
END TRY |
|
BEGIN CATCH |
|
EXEC dbo.ErrorHandler @proc_name, @ErrorMessage OUTPUT, @ErrorSeverity OUTPUT, @ErrorState OUTPUT; |
|
RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState); |
|
END CATCH |
|
END; |
|
GO |
|
|
|
ALTER PROCEDURE TestC.usp_Proc4 AS |
|
BEGIN |
|
SET NOCOUNT ON; |
|
|
|
-- error catch variables |
|
DECLARE |
|
@ErrorMessage NVARCHAR(4000), @ErrorNumber INT, @ErrorSeverity INT, @ErrorState INT = 0 |
|
,@proc_name nvarchar(256) = CONCAT(QUOTENAME(OBJECT_SCHEMA_NAME(@@PROCID)),'.', QUOTENAME(OBJECT_NAME(@@PROCID))); |
|
|
|
BEGIN TRY |
|
PRINT 'This is our TestB.usp_Proc4' |
|
BEGIN TRY |
|
EXEC sp_executesql N'select 1; select 1/0'; |
|
END TRY |
|
BEGIN CATCH |
|
SELECT @ErrorMessage = ERROR_MESSAGE(), @ErrorNumber = 500000 + ERROR_NUMBER(); |
|
THROW @ErrorNumber, @ErrorMessage, 1; |
|
END CATCH |
|
|
|
DECLARE @P1 INT; |
|
BEGIN TRY |
|
EXEC sp_prepare @P1 OUTPUT, |
|
N'@P1 NVARCHAR(128), @P2 NVARCHAR(100)', |
|
N'SELECT database_id, name FROM sys.databases WHERE name=@P1 AND state_desc = @P2'; |
|
EXEC sp_execute @P1, N'tempdb', N'ONLINE'; |
|
EXEC sp_unprepare @P1; |
|
END TRY |
|
BEGIN CATCH |
|
SELECT @ErrorMessage = ERROR_MESSAGE(), @ErrorNumber = 500000 + ERROR_NUMBER(); |
|
THROW @ErrorNumber, @ErrorMessage, 1; |
|
END CATCH |
|
PRINT 'We will not make it to this line.' |
|
|
|
END TRY |
|
BEGIN CATCH |
|
EXEC dbo.ErrorHandler @proc_name, @ErrorMessage OUTPUT, @ErrorSeverity OUTPUT, @ErrorState OUTPUT; |
|
RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState); |
|
END CATCH |
|
END; |
|
GO |
|
|
|
SET XACT_ABORT ON; |
|
EXEC dbo.usp_Proc1 |