In this Article, we will learn How to Handle Exception in SQL Server and also see How to capture or Log the Exception in case of any DB Level Exception occurs so that the Developer can refer to that Error log, can check the severity of the Exception, and fix it without wasting too much time in finding the exception causing procedure or function or line which is causing the exception.
CREATE PROCEDURE dbo.[usp_SampleProcedure] AS BEGIN SELECT
1 / 0 AS
Result; END |
In the above procedure, I have written a query which will thow Divide by Zero Exception on the execution of the procedure.
CREATE TABLE dbo.DBErrorLogs ( ErrorLogID BIGINT IDENTITY NOT NULL, UserName NVARCHAR (200) NOT NULL, ErrorNumber INT NOT NULL, ErrorState INT NOT NULL, ErrorSeverity INT NOT NULL, ErrorLine INT NOT NULL, ErrorProcedure VARCHAR
(max) NOT NULL, ErrorMessage VARCHAR (max) NOT NULL, ErrorOccuredOn DATETIME
NOT NULL, CONSTRAINT
PK_DBErrorLogs PRIMARY KEY (ErrorLogID) ) |
CREATE PROCEDURE dbo.[usp_SampleProcedure] AS BEGIN BEGIN TRY SELECT
1 / 0 AS
Result; END TRY
BEGIN CATCH INSERT
INTO dbo.dbErrorLogs ( UserName ,ErrorNumber ,ErrorState ,ErrorSeverity ,ErrorLine ,ErrorProcedure ,ErrorMessage ,ErrorOccuredOn ) VALUES ( SUSER_SNAME() ,ERROR_NUMBER() ,ERROR_STATE() ,ERROR_SEVERITY() ,ERROR_LINE() ,ERROR_PROCEDURE() ,ERROR_MESSAGE() ,GETDATE() ) END CATCH END |
As you can see, we are capturing the various fields with the help of predefined functions provided by SQL like UserName, ErrorNumber, ErrorState, ErrorSeverity, ErrorLine, ErrorProcedure, ErrorMessage, ErrorOccuredOn, etc. which will help us to find us the real root causing issue of the Exception. Let’s understand each function quickly.
1. SUSER_SNAME(): Returns the Login Name for the current Security Context.
2. ERROR_NUMBER(): Returns the error number of the error which caused the catch block of a try-catch construct to execute. ERROR_NUMBER() returns NULL when called outside of the scope of the Catch Block.
3. ERROR_STATE(): Returns the State Number of the error message that caused the Catch Block to Run and it returns NULL when called outside of the scope of the Catch Block.
4. ERROR_SEVERITY(): returns the Severity of the Error when the error or exception occurs. There are several levels of Error Severity defined by Microsoft which can be used to identify the type of the problem encountered by the SQL Server. For more, you can visit https://docs.microsoft.com/en-us/sql/relational-databases/errors-events/database-engine-error-severities?view=sql-server-ver15 link. ERROR_SEVERITY() returns NULL when called outside of the scope of the Catch Block.
5. ERROR_LINE(): returns the line number of the occurrence of the Error. ERROR_LINE returns NULL when called outside of the scope of the Catch Block.
6. ERROR_PROCEDURE(): returns the name of the procedure or trigger on the occurrence of the Error. ERROR_PROCEDURE() returns NULL in case error did not occur in the stored procedure or trigger or when called outside of the scope of the Catch Block.
7. ERROR_MESSAGE(): returns the message text of the error that caused the catch block of a try-catch block to execute.
CREATE PROCEDURE dbo.[usp_SampleProcedure] AS BEGIN BEGIN TRY BEGIN
TRANSACTION
--For
Demo, delete query written below will throw error --The
DELETE statement conflicted with the REFERENCE constraint DELETE FROM
Person.Person WHERE
BusinessEntityID = 20777
COMMIT
TRANSACTION END TRY
BEGIN CATCH --@@TRANCOUNT
for number of BEGIN TRANSACTION statements --that
have occurred on the current connection. IF
@@TRANCOUNT >
0 ROLLBACK
TRANSACTION;
INSERT
INTO dbo.dbErrorLogs ( UserName ,ErrorNumber ,ErrorState ,ErrorSeverity ,ErrorLine ,ErrorProcedure ,ErrorMessage ,ErrorOccuredOn ) VALUES ( SUSER_SNAME() ,ERROR_NUMBER() ,ERROR_STATE() ,ERROR_SEVERITY() ,ERROR_LINE() ,ERROR_PROCEDURE() ,ERROR_MESSAGE() ,GETDATE() ) END CATCH END |
0 comments:
Post a Comment