These first five or so posts use DB mail and the stored procedure sp_send_dbmail. I like information sent to me, rather than me looking for it. In this case the weekly Database integrity checks are run and with over ten instances of SQL Server (all 2005). I don't want to spend my time every Monday morning (when I remember) to go look for each intergrity check output file.
Thinking about this, I should probably turn this into a SSIS package. That will be another post.
Each server needs DB mail running, it's easier if there is a common DB mail profile across all servers. This stored procedure should run on each machine and it would be one less thing to change in the code.
The code also uses xp_cmdshell. It's used to get directory listing where the intergrity output file exists. Here is the DOS command I use to get the list: dir "C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\LOG\*Integrity*.txt" /B /TC /O-D
where /B is base formating, /TC show time of creation and /O-D means order by desending date. So this will give us a list of files with the newest file at the top of the list. By dumping this into a temp table and getting the first one we have the file we want. Then we just have to mail it.
Code: /*
created by Dave Ott
This emails the Database Intergrity checks to me.PLace on all DB servers
Note: db mail must be set up and use the profile that is on that server
*/
-- OS command to get file. Change path as needed
DECLARE @cmd NVARCHAR(500)
SET @cmd ='dir "C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\LOG\*Integrity*.txt" /B /TC /O-D'
/*
/B - base format
/TC - Time, Creation
/O - List Files by (D)ate (-) reversed order
*/
SETnocountON
CREATETABLE #list
(
ikey INTIDENTITY(1, 1)
NOTNULL,
filenameVARCHAR(1000)
)
INSERTINTO #list (filename)
EXECxp_cmdshell @cmd
--Most servers have only one file, but keep logic to loop through multiple files
DELETEFROM #list
WHEREikey > 2
DECLARE @file VARCHAR(1000),
@file_attachments VARCHAR(3000)
WHILEEXISTS(SELECT* FROM #list )
BEGIN
SET @file =(SELECTMIN(filename)FROM #list)
SET @file_attachments =CASEWHEN @file_attachments ISNULLTHEN''
This Stored Procedure is the main one I can't live without. Every morning I get an email from our production servers and one from our test and development servers. If there are errors, then I know I have to take care of them before I can work on other stuff that day.
This procedure not only identifies the job failures and errors, it also returns the suspect pages on each server. Also, we log any failures that cause down time to our end users in Share Point. I've included a link to the Share Point page in the email message.
Requirements are that Database mail must be set up and remote servers must be a linked server.
Below the code is a sample email that I get.
USE [master]
GO
CREATEPROC [dbo].[SP_SQLJobFailureEmail]
AS/*
DATE:7/14/06
WHO:Dave Ott
WHY:For easy reporting of errors to the DBA Group
This loops through the failures on these serrvers and reports the errors in an email
Server Other than this one must be linked servers
Mods:
10/10/06 Dave Ott
added server3
change logic to build datetime from run_date and run_time
6/25/07 Dave ott
Added suspect_pages
*/
SETNOCOUNTON
-- list of errors
DECLARE @errors TABLE
(
ServerVARCHAR(20),
Run_date DATETIME,
JobName VARCHAR(2000),
StepNumber TINYINT,
step_name VARCHAR(1000),
MessageVARCHAR(2000),
Ukey INTIDENTITY(1, 1)
)
DECLARE @DaysToReport INT,
@Subject VARCHAR(100),
@Servers VARCHAR(300),
@Message VARCHAR(8000),
@EmailList VARCHAR(100),
@FromDate DATETIME,
@runDate INT,
@runTime INT
-- set defaults
SET @DaysToReport = 1 -- this will only report today and the yesturday
SET @EmailList ='DBAGroup@MyCo.com'--NT group for DBAs
SET @Subject ='SQL Server Job Failure Email:'
SET @Servers =''
SET @Message =''
-- set up date and time for run
SET @FromDate =GETDATE()- @DaysToReport
SET @runDate =(YEAR(@FromDate)* 10000 )+(MONTH(@FromDate)* 100 )
+(DAY(@FromDate))
SET @runTime =DATEPART(hour, @FromDate)* 10000 +DATEPART(Minute, @FromDate)
* 100 +DATEPART(second, @FromDate)
-- server1 (here)
INSERTINTO @errors
SELECTDISTINCT
server,
CONVERT(VARCHAR(10), run_date),
name,
step_id,
step_name,
message
FROMmsdb.dbo.sysjobhistory h
JOINmsdb.dbo.sysjobs j
ON h.job_id = j.job_id
WHERErun_status = 0
AND @FromDate <=CONVERT(DATETIME,LEFT(CONVERT(VARCHAR(10), run_date),