Error Alert Script



To prevent databases from running into serious problems, DBAs typically setup alerts for specific errors; for example, the error “table corrupt” could lead to “database corrupted”. The problem is there are a large number of alerts for the “table corrupt” error message (about 40), and while you probably don’t want to set all 40 of the alerts on your server, you don’t want to miss any of them either. The solution is to simply set this alert job instead.

The job allows you to combine as many specific error messages as you want to monitor in just one single alert. For example, you can combine ” table corrupt” and ” DBCC” to monitor both by using this single job.

It applies to SQL Server 6.5/7.0/2000.


How the Job Works

Basically, the job will look up the SQL Server Error log files for specific error strings that the DBA wants to be notified of whenever they occur (for example : ” table corrupt” and “DBCC”) and then send an e-mail to the DBA composed of the error log lines that comprise the strings.

How to Create the Job


1. Create a text file comprised of the various strings you want to include (“Table corrupt”, for example). Name the file search.txt.

Note: for more information on how to create this file please refer to documentation for Window NT Findstr Utility or Microsoft SQL Server Administrator’s companion book.

2. Create a T-SQL job using this script:

exec xp_sendmail lname, fname

@subject = ' Search Errorlog'

@query = "declare @result varchar(125)

exec @result = xp_cmdshell ' findstr /i /g:c:mssql7logsearch.txt mssql7logerrorlog*.* ' "

Arguments:

c:mssql7logsearch.txt is the path of search.txt file

mssql7logerrorlog*.* is the path of the MS SQL Server error log file

Note: make sure that SQL Mail and SQL Server Agent are running; and that a valid MAPI Profile exists.

3. Schedule to run this job as a recurring job.


Stephanie Nguyen

Get the Free Newsletter!

Subscribe to Cloud Insider for top news, trends & analysis

Latest Articles