![]() |
|
|
By: Jeremy Kadlec | Read Comments | Print Jeremy is the CTO @ Edgewood Solutions, co-founder of MSSQLTips.com and SQL Server MVP since 2009. Related Tips: 1 | 2 | 3 | More |
|
Problem
Error handling was not always used in SQL Server 2000 because the techniques were cumbersome. With SQL Server 2005 the TRY and CATCH syntax is available which simplifies the error handling process in all T-SQL code. To take the error handling to the next level, why not standardize the error handling across your T-SQL code and centralize the location of the errors? Seem like a good idea, right? So let's jump into how to do this.
Solution
Let's break this down how to setup the error handling into the following steps:
Sample TRY\CATCH code
BEGIN TRY
END TRY BEGIN CATCH
END CATCH |
Sample stored procedure
| CREATE procedure dbo.spErrorHandling AS
-- Declaration statements DECLARE @Error_Message varchar(4000) DECLARE @Error_Severity int DECLARE @Error_State int DECLARE @Error_Procedure varchar(200) DECLARE @Error_Line int DECLARE @UserName varchar(200) DECLARE @HostName varchar(200) DECLARE @Time_Stamp datetime -- Initialize variables @Error_Message = isnull(error_message(),'NULL Message'), @Error_Severity = isnull(error_severity(),0), @Error_State = isnull(error_state(),1), @Error_Line = isnull(error_line(), 0), @Error_Procedure = isnull(error_procedure(),''), @UserName = SUSER_SNAME(), @HostName = HOST_NAME(), @Time_Stamp = GETDATE(); -- Insert into the dbo.ErrorHandling table Error_Procedure, UserName, HostName, Time_Stamp) SELECT @Error_Number, @Error_Message, @Error_Severity, @Error_State, @Error_Line, @Error_Procedure, @UserName, @HostName, @Time_Stamp |
Sample logging table
| CREATE TABLE [dbo].[ErrorHandling]( [pkErrorHandlingID] [int] IDENTITY(1,1) NOT NULL, [Error_Number] [int] NOT NULL, [Error_Message] [varchar](4000) COLLATE Latin1_General_BIN NULL, [Error_Severity] [smallint] NOT NULL, [Error_State] [smallint] NOT NULL DEFAULT ((1)), [Error_Procedure] [varchar](200) COLLATE Latin1_General_BIN NOT NULL, [Error_Line] [int] NOT NULL DEFAULT ((0)), [UserName] [varchar](128) COLLATE Latin1_General_BIN NOT NULL DEFAULT (''), [HostName] [varchar](128) COLLATE Latin1_General_BIN NOT NULL DEFAULT (''),[Time_Stamp] datetime NOT NULL, PRIMARY KEY CLUSTERED ( [pkErrorHandlingID] ASC )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] GO |
Verification process
| -- Sample procedure
CREATE PROCEDURE dbo.spTest ASBEGIN TRY
END TRY BEGIN CATCH
END CATCH-- Execute the sample procedure -- Review the error handling data captured FROM dbo.ErrorHandling; |
Implementation options
Below are some options to implement the standardized error logging:
Next Steps
| Share: | Share | Tweet |
|
![]() |
![]() |
Free SQL Server Learning |
|
privacy | disclaimer | copyright | advertise | about authors | contribute | feedback | giveaways | user groups Some names and products listed are the registered trademarks of their respective owners. Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com |