Reading log file

Read and search the SQL Server error log and the SQL Agent log from T-SQL.

Last updated:

Warning: Review and test in a non-production environment before running.

Back to results

-- Summary. Full tip: "How to Read Log File in SQL Server using TSQL", MSSQLTips – https://www.mssqltips.com/sqlservertip/1476/how-to-read-log-file-in-sql-server-using-tsql/

-- sp_readerrorlog parameters:
-- 1: log number (0 = current, 1 = previous archive, 2 = the one before, ...)
-- 2: log type (1 = SQL Server error log, 2 = SQL Agent log)
-- 3, 4: search strings (a row must contain both)
EXEC sp_readerrorlog 0, 1, N'error';
EXEC sp_readerrorlog 0, 1, N'login', N'failed';

-- xp_readerrorlog adds a time range and a sort order:
-- log number, log type, search 1, search 2, start time, end time, N'asc' / N'desc'
EXEC master.dbo.xp_readerrorlog 0, 1, N'error', NULL, '2026-01-01', NULL, N'desc';