-
Notifications
You must be signed in to change notification settings - Fork 84
/
Search the SQL Server error log files for a nominated string.sql
67 lines (54 loc) · 1.41 KB
/
Search the SQL Server error log files for a nominated string.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
-- Search the SQL Server error log files for a nominated string
-- Part of the SQL Server DBA Toolbox at https://github.com/DavidSchanzer/Sql-Server-DBA-Toolbox
-- This script enumerates all of the available SQL Server error log files on an instance, and then searches each of them for a nominated string, returning a single result set.
SET NOCOUNT ON;
DECLARE @log_number INT,
@search_string VARCHAR(255) = '<search_string>';
DROP TABLE IF EXISTS #error_log;
CREATE TABLE #error_log
(
log_number INT NOT NULL,
log_date DATE NOT NULL,
log_size INT NOT NULL
);
DROP TABLE IF EXISTS #sp_readerrorlog_output;
CREATE TABLE #sp_readerrorlog_output
(
LogDate DATETIME2 NOT NULL,
ProcessInfo VARCHAR(255) NOT NULL,
Text VARCHAR(255) NOT NULL
);
INSERT #error_log
(
log_number,
log_date,
log_size
)
EXEC ('EXEC sys.sp_enumerrorlogs;');
DECLARE log_cur CURSOR LOCAL FAST_FORWARD FOR
SELECT el.log_number
FROM #error_log AS el
ORDER BY el.log_number
FOR READ ONLY;
OPEN log_cur;
FETCH log_cur
INTO @log_number;
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO #sp_readerrorlog_output
(
LogDate,
ProcessInfo,
Text
)
EXEC sp_readerrorlog @p1 = @log_number, @p2 = 1, @p3 = @search_string;
FETCH log_cur
INTO @log_number;
END;
CLOSE log_cur;
DEALLOCATE log_cur;
SELECT LogDate,
ProcessInfo,
Text
FROM #sp_readerrorlog_output
ORDER BY LogDate DESC;