Click here to Skip to main content
15,905,068 members
Articles / Database Development / SQL Server / SQL Server 2008

Easy way to track Stored Procedure errors in MS SQL Server 2008

Rate me:
Please Sign up or sign in to vote.
1.00/5 (1 vote)
12 Apr 2012CPOL 32.8K   8   1
Easily trace all errors of Stored Procedures in MS SQL Server.

You can easily trace all errors of Stored Procedures in MS SQL Server. To do this, first create a table called Error.

SQL
CREATE TABLE [dbo].[Error](
    [iAutoID] [int] IDENTITY(1,1) NOT NULL,
    [dErrorDate] [datetime] NOT NULL,
    [vErrorNumber] [nvarchar](max) NULL,
    [vErrorSeverity] [nvarchar](max) NULL,
    [vErrorState] [nvarchar](max) NULL,
    [vErrorProcedure] [nvarchar](max) NULL,
    [vErrorLine] [nvarchar](max) NULL,
    [vErrorMessage] [nvarchar](max) NULL
) ON [SECONDARY]

GO

ALTER TABLE [dbo].[Error] ADD  CONSTRAINT [DF_Error_dErrorDate]  DEFAULT (getdate()) FOR [dErrorDate]
GO

Now create a Stored Procedure by writing the following code:     

SQL
CREATE PROCEDURE [dbo].[prcDailyAttendanceGeneration]
WITH
EXECUTE AS CALLER
AS
BEGIN
    SET XACT_ABORT, NOCOUNT ON
    DECLARE @starttrancount INT
    Begin TRY
        SELECT @starttrancount = @@TRANCOUNT
        IF @starttrancount = 0
        BEGIN TRANSACTION

	-- Your own code start
	Execute prcInsertShiftDateWiseInfo
        -- Your own code end

	IF @starttrancount = 0
        COMMIT TRANSACTION
	End Try
    Begin Catch
        -- Test if the transaction is uncommittable.
        IF  XACT_STATE() <> 0 AND @starttrancount = 0
        BEGIN
            ROLLBACK TRANSACTION;
        END;
	-- This is the main tricks to store all the errors in error table.
	insert into Error(vErrorNumber,vErrorSeverity,vErrorState,vErrorProcedure,vErrorLine,vErrorMessage)
        SELECT ERROR_NUMBER(),ERROR_SEVERITY(),ERROR_STATE(),ERROR_PROCEDURE(),ERROR_LINE(),ERROR_MESSAGE()
	End Catch
END    

 Now if the procedure gives any error, the error details will be saved into the Error table. By this way you can easily get all error details from the Error table and can take the necessary steps.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Codexplorer Technologies
Bangladesh Bangladesh
I am:
Founder & Technical Head at Codexplorer Technologies.
IT Consultant at Meridian Group.

I was:
Manager (IT) at Meridian Group.
Assistant Manager (Software Division) at KDS Garment Industries Limited.
Assistant Manager (Software Division) at E-Vision Software Limited.

My blog:
crea8ivecode

My preferred work area:
ASP.NET & SQL SERVER.

My email:
sadeque.sharif@yahoo.com

Follow me:
twitter | facebook | linkedin

Comments and Discussions

 
QuestionSize of your image Pin
Wendelius10-Apr-12 7:49
mentorWendelius10-Apr-12 7:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.