Wednesday, March 4, 2009

sp_readerrorlog sql 2005 search for string and dates directly in query by using xp_readerrorlog

Just wanted to share this useful info. We can directly search for Strings between two dates using xp_readerrorlog in SQL 2005 (Undocumented Procedure)

Below is the T-SQL command and usage information. This will be really useful since most of us used to import the error log into table then query it and export the required section and send as email.

Now since DB Mail has option of specifying query directly, we can put this T-SQL Query in DBMail command and with one single line we can close the requirement.

exec xp_readerrorlog 0, 1,'login','error','2009-02-28 11:55:00.000','2009-03-03 11:55:00.000','asc'

Usage Info:
This procedure takes 7 parameters
1.Value of error log file you want to read: 0 = current, 1 = Archive #1, 2 = Archive #2, etc...
2.Log file type: 1 or NULL = SQLServer Error log, 2 = SQL Server Agent Error log
3.Search string 1: String one you want to search for
4.Search string 2: String two you want to search for to further refine the results
5.Search from start time
6.Search to end time
7.Sort order for results: N'asc' = ascending, N'desc' = descending

Above option will work only for SQL 2005 incase you want to try in SQL 2000 then you use this link for more