-
Notifications
You must be signed in to change notification settings - Fork 14
/
dbo.usp_LogError.sql
83 lines (74 loc) · 2.57 KB
/
dbo.usp_LogError.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
IF OBJECT_ID('[dbo].[usp_LogError]', 'P') IS NULL
EXECUTE('CREATE PROCEDURE [dbo].[usp_LogError] AS SELECT 1');
go
IF OBJECT_ID('[ErrorLog]','U') IS NULL
CREATE TABLE [dbo].[ErrorLog] (
[ErrorLogID] INT IDENTITY(1, 1) NOT NULL
,[ErrorTime] DATETIME NOT NULL CONSTRAINT [DF_ErrorLog_ErrorTime] DEFAULT(getdate())
,[UserName] SYSNAME NOT NULL
,[ErrorNumber] INT NOT NULL
,[ErrorSeverity] INT NULL
,[ErrorState] INT NULL
,[ErrorProcedure] NVARCHAR(126) NULL
,[ErrorLine] INT NULL
,[ErrorMessage] NVARCHAR(4000) NOT NULL
,CONSTRAINT [PK_ErrorLog_ErrorLogID] PRIMARY KEY CLUSTERED ([ErrorLogID] ASC) WITH (
PAD_INDEX = OFF
,STATISTICS_NORECOMPUTE = OFF
,IGNORE_DUP_KEY = OFF
,ALLOW_ROW_LOCKS = ON
,ALLOW_PAGE_LOCKS = ON
) ON [PRIMARY]
) ON [PRIMARY]
go
ALTER PROCEDURE [dbo].[usp_LogError]
@ErrorLogID [int] = 0 OUTPUT -- contains the ErrorLogID of the row inserted
AS -- by usp_LogError in the ErrorLog table
BEGIN
SET NOCOUNT ON;
-- Output parameter value of 0 indicates that error
-- information was not logged
SET @ErrorLogID = 0;
BEGIN TRY
-- Return if there is no error information to log
IF ERROR_NUMBER() IS NULL
RETURN;
-- Return if inside an uncommittable transaction.
-- Data insertion/modification is not allowed when
-- a transaction is in an uncommittable state.
IF XACT_STATE() = -1
BEGIN
PRINT 'Cannot log error since the current transaction is in an uncommittable state. '
+ 'Rollback the transaction before executing usp_LogError in order to successfully log error information.';
RETURN;
END
INSERT [dbo].[ErrorLog]
(
[UserName],
[ErrorNumber],
[ErrorSeverity],
[ErrorState],
[ErrorProcedure],
[ErrorLine],
[ErrorMessage]
)
VALUES
(
CONVERT(sysname, CURRENT_USER),
ERROR_NUMBER(),
ERROR_SEVERITY(),
ERROR_STATE(),
ERROR_PROCEDURE(),
ERROR_LINE(),
ERROR_MESSAGE()
);
-- Pass back the ErrorLogID of the row inserted
SET @ErrorLogID = @@IDENTITY;
END TRY
BEGIN CATCH
PRINT 'An error occurred in stored procedure usp_LogError: ';
EXECUTE [dbo].[usp_PrintError];
RETURN -1;
END CATCH
END;
go