SQL Stored procedure with error handling using Try/Catch
This is improved version of the script from 2009 using try/catch syntax USE [MyDatabase] GO PRINT ' DROP PROCEDURE [usp_mysproc] ' GO if exists ( select * from dbo.sysobjects where id = object_id(N' [dbo].[usp_mysproc] ') and OBJECTPROPERTY(id, N' IsProcedure ') = 1) drop procedure [dbo].[usp_mysproc] GO PRINT ' CREATE PROCEDURE [usp_mysproc] ' GO CREATE PROCEDURE [dbo].[usp_mysproc] ( @RaiseError bit = 0 ) AS BEGIN TRY BEGIN TRAN IF @RaiseError = 1 BEGIN DECLARE @ TEMP TABLE ( Column 1 varchar (2)) INSERT INTO @ TEMP ( Column 1) VALUES (' 123 ') END COMMIT TRAN END TRY BEGIN CATCH DECLARE @ErrorInfo VARCHAR (4000) SET @ErrorInfo = ' ERROR [ '+LTRIM(STR(ERROR_NUMBER()))+' ][ '+ERROR_PROCEDURE()+' : '+LTRIM(STR(ERROR_LINE()))+' ] - ' + ERROR_MESSAGE() PRINT CONVERT ( VARCHAR ,GETDATE(),21) + ' : '+@ErrorInfo ROLLBACK...