devxlogo

Read All SQL Server Log Files

Read All SQL Server Log Files

You can use a script similar to the following to read through all SQL Server Log files:

CREATE PROCEDURE SearchLogFiles (@LogType INT = 1, Filter NVARCHAR(MAX) = '')ASBEGIN   DECLARE @LogsTable TABLE (LogIndex INT, LogDate DATETIME, LogSize INT)      DECLARE @LogRows TABLE (LogDate DATETIME,                               ProcessInfo NVARCHAR (4000),                               Test NVARCHAR (4000))    INSERT INTO @LogsTable   EXEC xp_enumerrorlogs @LogType    DECLARE @index INT = 0   WHILE @index <= (SELECT MAX (LogIndex) FROM @LogsTable)   BEGIN      INSERT INTO @LogRows       EXEC xp_readerrorlog @index          , @LogType -- 1=SQL Server log, 2=SQL Agent log         , @filterstr -- filter string         , @filterstr       SET @index += 1   END   SELECT *   FROM @LogRows    ORDER BY LogDate DESCEND

Use it as follows:

Exec SearchLogFiles 1,'ERROR'
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist