Created
December 7, 2012 23:38
-
-
Save ejhayes/4237550 to your computer and use it in GitHub Desktop.
SQL Server catch compile time and runtime errors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Our runner script | |
*/ | |
CREATE PROCEDURE inner_sp AS | |
EXEC dbo.usp_RecordChangeScript '2007-01-2012 (ERIC TEST).sql', '0.1.11' | |
EXEC dbo.usp_RecordChangeScript '2004-01-2012 (ERIC TEST).sql', '0.1.11' | |
-- CODE GOES HERE | |
GO | |
IF @@ERROR <> 0 | |
BEGIN | |
DECLARE @ErrorMessage NVARCHAR(4000) | |
DECLARE @ErrorSeverity INT | |
DECLARE @ErrorState INT | |
SELECT @ErrorMessage = ERROR_MESSAGE(), | |
@ErrorSeverity = ERROR_SEVERITY(), | |
@ErrorState = ERROR_STATE() | |
RAISERROR ( @ErrorMessage, @ErrorSeverity, @ErrorState) | |
END | |
ELSE | |
BEGIN | |
BEGIN TRY | |
BEGIN TRANSACTION; | |
-- Some code | |
EXEC inner_sp | |
-- Drop this sucker | |
DROP PROCEDURE inner_sp | |
COMMIT TRANSACTION; | |
END TRY | |
BEGIN CATCH | |
IF @@TRANCOUNT > 0 | |
ROLLBACK TRANSACTION; | |
DECLARE @ErrorMessage1 NVARCHAR(4000) | |
DECLARE @ErrorSeverity1 INT | |
DECLARE @ErrorState1 INT | |
SELECT @ErrorMessage1 = ERROR_MESSAGE(), | |
@ErrorSeverity1 = ERROR_SEVERITY(), | |
@ErrorState1 = ERROR_STATE() | |
RAISERROR ( @ErrorMessage1, @ErrorSeverity1, @ErrorState1) | |
END CATCH; | |
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[inner_sp]') AND type in (N'P', N'PC')) | |
DROP PROCEDURE [dbo].[inner_sp] | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment