![]() |
|
SQL Monitor offers straightforward server monitoring through a web-based UI, to help you prioritize your workload:
Start monitoring your servers today with a free trial.

|
|
By: Greg Robidoux | Read Comments (8) | Related Tips: 1 | 2 | 3 | More > Monitoring |
Problem
One of the issues I have is that the SQL Server Error Log is quite large and it is not always easy to view the contents with the Log File Viewer. In a previous tip "Simple way to find errors in SQL Server error log" you discussed a method of searching the error log using VBScript. Are there any other easy ways to search and find errors in the error log files?
Solution
SQL Server 2005 offers an undocumented system stored procedure sp_readerrorlog. This SP allows you to read the contents of the SQL Server error log files directly from a query window and also allows you to search for certain keywords when reading the error file. This is not new to SQL Server 2005, but this tip discusses how this works for SQL Server 2005.
This is a sample of the stored procedure for SQL Server 2005. You will see that when this gets called it calls an extended stored procedure xp_readerrorlog.
CREATE PROC [sys].[sp_readerrorlog]( |
This procedure takes four parameters:
If you do not pass any parameters this will return the contents of the current error log.
Here are a few examples:
Example 1
EXEC sp_readerrorlog 6 |
This statement returns all of the rows from the 6th archived error log.


Example 2
EXEC sp_readerrorlog 6, 1, '2005' |
This returns just 8 rows wherever the value 2005 appears.

Example 3
EXEC sp_readerrorlog 6, 1, '2005', 'exec' |
This returns only rows where the value '2005' and 'exec' exist.

xp_readerrrorlog
Even though sp_readerrolog accepts only 4 parameters, the extended stored procedure accepts at least 7 parameters.
If this extended stored procedure is called directly the parameters are as follows:
EXEC master.dbo.xp_readerrorlog 6, 1, '2005', 'exec', NULL, NULL, N'desc' |

Next Steps
| Tuesday, April 15, 2008 - 7:18:24 AM - apostolp | Read The Tip |
|
I was not aware of this functionality but I cannot seem to get the search facility to work in SQLServer 2000. It works fine in SQLServer 2005 but when I run EXEC sp_readerrorlog 1, null, 'master' (EXEC sp_readerrorlog 1, null, 'master' actually returns an error in SQLServer 2000)
in SQLServer 2000 it does not narrow the result set and just returns everything. Something similar happens with xp_readerrorlog. Any ideas ? Many thanks, Peter A. |
|
| Tuesday, April 15, 2008 - 8:01:19 AM - grobido | Read The Tip |
|
I think the format for SQL Server 2000 is different than SQL Server 2005. Take a look at this article: http://vyaskn.tripod.com/sp_readerrorlog_undocumented.htm Regards, |
|
| Tuesday, April 15, 2008 - 8:14:08 AM - apostolp | Read The Tip |
|
Many thanks Greg. |
|
| Tuesday, June 17, 2008 - 5:30:26 AM - hexiaomail | Read The Tip |
|
This procedure takes 7 parameters: --the 5 and 6 paramenters use VARCHAR type,desc |
|
| Wednesday, June 25, 2008 - 12:33:05 PM - Pardo | Read The Tip |
|
Great tip, Parameters 6 and 7 are between dates!!! exec xp_readerrorlog 0, 1,'succeeded','pardo','2008-06-23 10:06:59.250','2008-06-24 16:40:56.790','asc'It is only for SQL Server 2005 Pardo |
|
| Thursday, January 31, 2013 - 12:40:28 AM - Deepu | Read The Tip |
|
Can any one help me to reading errorlogs, and send alert mails if any errors found |
|
| Thursday, January 31, 2013 - 7:04:49 AM - Greg Robidoux | Read The Tip |
|
@Deepu - you could use sp_readerrorlog to get the errors and then use sp_send_dbmail to send the messages. You would need to setup a job to periodically check the error log. Stay tuned for a future tip to do what you are requesting. Here is a tip that show you how to send emails: http://www.mssqltips.com/sqlservertip/2347/send-email-in-a-tabular-format-using-sql-server-database-mail/ Greg |
|
| Wednesday, February 27, 2013 - 11:57:46 AM - Hillsman | Read The Tip |
|
Many thanks for this Greg - very useful. I used your code it to loop through the SQL Server Logs to return information about database restores. I found that this is the only way I could retrieve database restore finish-times from SQL Server (since msdb.dbo.restorehistory only stores the restore start time).
|
|
|
privacy | disclaimer | copyright | advertise | about authors | contribute | feedback | giveaways | user groups Some names and products listed are the registered trademarks of their respective owners. Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com |