Tuesday, October 14, 2014
Monday, September 08, 2014
proc sp_alert_failed_jobs
/*
exec sp_alert_failed_jobs @pastdays = -3 , @exludejob ='''exportbill_dts'',''daily_update_erptables'''
*/
alter proc sp_alert_failed_jobs
(
@pastdays int = -1,
@exludejob varchar(1000) =''
)
as
begin
--declare @pastdays int = -1
--select @exludejob
if(object_id('msdb..__tmp_failed_jobs')>1)
drop table __tmp_failed_jobs
create table __tmp_failed_jobs (name varchar(1000), run_date int, run_time int, step_id int, step_name varchar(1000), message ntext)
declare @sql nvarchar(max)
set @sql = 'select j.name,h.run_date,h.run_time,h.step_id, h.step_name, h.message
From msdb..sysjobhistory h
inner join msdb..sysjobs j on h.job_id = j.job_id
where h.run_status = 0
and h.run_date >= convert(int, CONVERT(varchar, dateadd(dd,'+convert(varchar,@pastdays)+',GETDATE()),112))
and j.name not in ('+@exludejob+')
order by h.run_date desc, h.step_id asc'
--print @sql
insert into __tmp_failed_jobs exec sp_executesql @sql
--select * From __tmp_failed_jobs
if(select COUNT(*) from __tmp_failed_jobs) >0
begin
declare @subject nvarchar(1000), @recipientslist nvarchar(1000)
set @subject = 'Failed Jobs - SQL ALERT for '+@@SERVERNAME+' : '+Convert(nvarchar(100),getdate(),101)
set @recipientslist = 'patrickalexander@hotmail.com;ACheckDB@acheckamerica.com;kcinty@acheckamerica.com;qkong@acheckamerica.com'
DECLARE @tableHTML NVARCHAR(MAX) , @subjectstring nvarchar(200);
SET @tableHTML =
N'
N'' +
' ;
--print @tableHTML
EXEC msdb.dbo.sp_send_dbmail
--@profile_name = 'default',
@recipients =@recipientslist,
@body = @tableHTML,
@body_format = 'HTML' ,
@subject = @subject;
end
end
exec sp_alert_failed_jobs @pastdays = -3 , @exludejob ='''exportbill_dts'',''daily_update_erptables'''
*/
alter proc sp_alert_failed_jobs
(
@pastdays int = -1,
@exludejob varchar(1000) =''
)
as
begin
--declare @pastdays int = -1
--select @exludejob
if(object_id('msdb..__tmp_failed_jobs')>1)
drop table __tmp_failed_jobs
create table __tmp_failed_jobs (name varchar(1000), run_date int, run_time int, step_id int, step_name varchar(1000), message ntext)
declare @sql nvarchar(max)
set @sql = 'select j.name,h.run_date,h.run_time,h.step_id, h.step_name, h.message
From msdb..sysjobhistory h
inner join msdb..sysjobs j on h.job_id = j.job_id
where h.run_status = 0
and h.run_date >= convert(int, CONVERT(varchar, dateadd(dd,'+convert(varchar,@pastdays)+',GETDATE()),112))
and j.name not in ('+@exludejob+')
order by h.run_date desc, h.step_id asc'
--print @sql
insert into __tmp_failed_jobs exec sp_executesql @sql
--select * From __tmp_failed_jobs
if(select COUNT(*) from __tmp_failed_jobs) >0
begin
declare @subject nvarchar(1000), @recipientslist nvarchar(1000)
set @subject = 'Failed Jobs - SQL ALERT for '+@@SERVERNAME+' : '+Convert(nvarchar(100),getdate(),101)
set @recipientslist = 'patrickalexander@hotmail.com;ACheckDB@acheckamerica.com;kcinty@acheckamerica.com;qkong@acheckamerica.com'
DECLARE @tableHTML NVARCHAR(MAX) , @subjectstring nvarchar(200);
SET @tableHTML =
N'
Failed jobs on '+@@SERVERNAME+'
' +N'
Job Name | Date | Time | ' +StepID | ' +StepName | ' +Message | <
---|
--print @tableHTML
EXEC msdb.dbo.sp_send_dbmail
--@profile_name = 'default',
@recipients =@recipientslist,
@body = @tableHTML,
@body_format = 'HTML' ,
@subject = @subject;
end
end
Subscribe to:
Posts (Atom)