Skip to main content

About sp_readerrorlog

In this article let us understand the system stored procedure Sp_readerrorlog in detail. Sp_readerrorlog is a system stored procedure which can be used to read the SQL Server error logs from within T-SQL.

Syntax of sp_readerrorlog

Parameter 1 : Non zero integer value
Parameter 2 : File name
Parameter 3 : Line number in the file
Parameter 4 : Search string

1. Code snippet to display the content of the current event log
Execute sp_readerrorlog

2. Code snippet to display the content of eventlog.1
EXEC sp_readerrorlog 1, 'C:\Program Files\Microsoft SQL Server\MSSQL$Velias\log\ERRORLOG.1'

Or

EXEC sp_readerrorlog 1

3. Code snippet to search a string --Would search the second row of the specified text file for the number 2000.
EXEC sp_readerrorlog 1, 'C:\SendToTextFile.txt',2, '2000'

Yep you got it right ... this system stored proc can be used not only on log file but on any file (as shown in the above code snippet).

Script to import the SQL Error Logs, format the results and then allow querying using TSQL: http://www.replicationanswers.com/Downloads/ErrorLogs.txt

Comments

Aljo said…
Hi Vadivel,

Nice blog Vadivel.

The code given in the below link throws an error.

http://www.replicationanswers.com/Downloads/ErrorLogs.txt

Error description:
Msg 8114, Level 16, State 5, Procedure sp_readerrorlog, Line 0
Error converting data type varchar to int.

The problem is with the passed parameter to the sp_readerrorlog procedure, The second parameter should be an integer.Correct me if am wrong.

Thanks,

Aljo
Vadivel said…
Aljo, as you would be knowing it is an undocumented feature. I wrote this post based on SQL Server 2000 where it should work. (I don't have it now to confirm it :) )

As you have rightly pointed out in SQL Server 2005 the 2nd param got to be INT only.



Anonymous said…
i deployed one customized script in our environment on different instances, but this extended stored procedure can spike the CPU on servers where sql server 2005 is installed, if you will kill the job executing this procedure, it will not kill that actually and need to restart the database engine service. So becarefull on sql server 2005.

Popular posts from this blog

Script table as - ALTER TO is greyed out - SQL SERVER

One of my office colleague recently asked me why we are not able to generate ALTER Table script from SSMS. If we right click on the table and choose "Script Table As"  ALTER To option would be disabled or Greyed out. Is it a bug? No it isn't a bug. ALTER To is there to be used for generating modified script of Stored Procedure, Functions, Views, Triggers etc., and NOT for Tables. For generating ALTER Table script there is an work around. Right click on the table, choose "Modify" and enter into the design mode. Make what ever changes you want to make and WITHOUT saving it right click anywhere on the top half of the window (above Column properties) and choose "Generate Change Script". Please be advised that SQL Server would drop actually create a new table with modifications, move the data from the old table into it and then drop the old table. Sounds simple but assume you have a very large table for which you want to do this! Then it woul

AWS fatal error: An error occurred (400) when calling the HeadObject operation: Bad Request

While using AWS and trying to copy a file from a S3 bucket to my EC2 instance ended up with this error message. Command Used: aws s3 cp s3://mybucketname/myfilename.html /var/www/html/ Error: fatal error: An error occurred (400) when calling the HeadObject operation: Bad Request The error goes off if we add the region information to the command statement. I am using Asia Pacific (Mumbai) so used ap-south-1 as the region name. Modified Command: aws s3 cp s3://mybucketname/myfilename.html /var/www/html/ --region ap-south-1

[Non Tech] Want to know the recipe for Omelette :)

Fed up with Bread - Jam and Curd Rice, today i wanted to eat Omelette. Interesting part is I wanted to cook it myself :) So in the first picture you see all the items which are needed for preparing an Omelette. When I had a closer look at the eggs I see that almost all the eggs are broken. But believe me when I bought it couple of days back it was in perfect condition! I was wondering whether the eggs have become rotten or pretty old to consume! I tried taking an egg and break it but couldn't break it at all :) Since I have kept in the freezer all the eggs have frozen and looked like a iron ball :) After trying for few minutes of trying i removed the shell of the egg and then kept that iron ball :) into a bowl and placed it within Oven. I heated it for 1 minute and checked. It melted only to a limit. So i just set it for another 2 minutes and checked it later. It has melted but the part of the egg white has become a Omelette :( I didn't leave it there. I took the bowl out of