Updated query to view data in the error log
5May 31, 2018 by Kenneth Fisher
This is one of my favorite scripts. It pulls all of the data from the error log and dumps it into a temp table. I did one version here but it was pretty specific to I/O errors. I also gave some homework to find/build a script like it. So if you didn’t do the homework here is a nice little (more generic) script that I personally find extraordinarily handy. There are two parameters at the top that will restrict the data pulled in, and a query against #LogInfo at the bottom that filters out backup and logon entries since they tend to get in the way of what I’m looking for. That said, if you want to create a report on logons (or backups) you’ll want to modify the query. I do leave #LogInfo open and the end so you can run multiple queries against it.
--DROP TABLE #LogInfo DECLARE @searchstring1 nvarchar(500) = '' DECLARE @searchstring2 nvarchar(500) = '' DECLARE @Limit int = 10000 ---------------------------------------------------------------------- -- This part of the code was found here: -- https://ask.sqlservercentral.com/questions/99484/number-of-error-log-files.html DECLARE @FileList AS TABLE ( subdirectory NVARCHAR(4000) NOT NULL ,DEPTH BIGINT NOT NULL ,[FILE] BIGINT NOT NULL ); DECLARE @ErrorLog NVARCHAR(4000), @ErrorLogPath NVARCHAR(4000); SELECT @ErrorLog = CAST(SERVERPROPERTY(N'errorlogfilename') AS NVARCHAR(4000)); SELECT @ErrorLogPath = SUBSTRING(@ErrorLog, 1, LEN(@ErrorLog) - CHARINDEX(N'\', REVERSE(@ErrorLog))) + N'\'; INSERT INTO @FileList EXEC xp_dirtree @ErrorLogPath, 0, 1; DECLARE @NumberOfLogfiles INT; SET @NumberOfLogfiles = (SELECT COUNT(*) FROM @FileList WHERE [@FileList].subdirectory LIKE N'ERRORLOG%'); -- SELECT @NumberOfLogfiles; If @Limit IS NOT NULL AND @NumberOfLogfiles > @Limit SET @NumberOfLogfiles = @Limit ---------------------------------------------------------------------- CREATE TABLE #LogInfo ( LogDate datetime, ProcessInfo nvarchar(500), ErrorText nvarchar(max)) DECLARE @p1 INT = 0 WHILE @p1 < @NumberOfLogfiles BEGIN -- P1 is the file number starting at 0 DECLARE @p2 INT = 1, -- P2 1 for SQL logs, 2 for SQL Agent logs @p3 NVARCHAR(255) = @searchstring1, -- P3 is a value to search on @p4 NVARCHAR(255) = @searchstring2 -- P4 is another search value BEGIN TRY INSERT INTO #LogInfo EXEC sys.xp_readerrorlog @p1,@p2,@p3,@p4 END TRY BEGIN CATCH PRINT 'Error occurred processing file ' + cast(@p1 as varchar(10)) END CATCH SET @p1 = @p1 + 1 END SELECT * FROM #LogInfo WHERE ProcessInfo NOT IN ('Backup','Logon') ORDER BY LogDate DESC
[…] Kenneth Fisher has a script to take information out of the error log and put it into a table: […]
[…] Query – Search the error log […]
[…] Once that’s done you you’ll have information going forward. Assuming it was already set that way all logins are stored in the SQL Server log files. Which you can search using a script. Here’s mine. […]
[…] client are the same so all of that information ends up in $myData. In my particular case I used a script to pull data from the SQL log and pull back out the most recent logins/failed […]
[…] Loading the log into a temp table. […]