Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Find Failed Jobs Expand / Collapse
Author
Message
Posted Monday, December 10, 2012 6:30 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Yesterday @ 12:12 PM
Points: 320, Visits: 1,916
Hi all,

Does anyone have any scripts to find a list of all the failed jobs in the last 10 hours.

I know I can find all failed jobs from sys.jobhistory.....but how can modify the script to get failed jobs for the past 10 hours? Please advise.

Thanks,
SueTons.
Post #1394844
Posted Monday, December 10, 2012 9:58 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, May 21, 2013 2:30 AM
Points: 24, Visits: 143
Hi Sue,

Please find the below steps to find the job failed in sql sever 2005
-- Variable Declarations
DECLARE @PreviousDate datetime
DECLARE @Year VARCHAR(4)
DECLARE @Month VARCHAR(2)
DECLARE @MonthPre VARCHAR(2)
DECLARE @Day VARCHAR(2)
DECLARE @DayPre VARCHAR(2)
DECLARE @FinalDate INT

-- Initialize Variables
SET @PreviousDate = DATEADD(dd, -7, GETDATE()) -- Last 7 days
SET @Year = DATEPART(yyyy, @PreviousDate)
SELECT @MonthPre = CONVERT(VARCHAR(2), DATEPART(mm, @PreviousDate))
SELECT @Month = RIGHT(CONVERT(VARCHAR, (@MonthPre + 1000000000)),2)
SELECT @DayPre = CONVERT(VARCHAR(2), DATEPART(dd, @PreviousDate))
SELECT @Day = RIGHT(CONVERT(VARCHAR, (@DayPre + 1000000000)),2)
SET @FinalDate = CAST(@Year + @Month + @Day AS INT)

-- Final Logic
SELECT j.[name],
s.step_name,
h.step_id,
h.step_name,
h.run_date,
h.run_time,
h.sql_severity,
h.message,
h.server
FROM msdb.dbo.sysjobhistory h
INNER JOIN msdb.dbo.sysjobs j
ON h.job_id = j.job_id
INNER JOIN msdb.dbo.sysjobsteps s
ON j.job_id = s.job_id
AND h.step_id = s.step_id
WHERE h.run_status = 0 -- Failure
AND h.run_date > @FinalDate
ORDER BY h.instance_id DESC

Please let me know further concerns

Regards
Vivek
Post #1394881
Posted Wednesday, December 12, 2012 2:00 PM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, June 13, 2013 5:09 PM
Points: 254, Visits: 1,032
I use this script to check for failed SQL Agent jobs. You can easily modify it for 10 hours or whatever time interval you need to audit.

It's nothing fancy but it gets the job done.

-- Audit Query - Failed Jobs Report

DECLARE @PreviousDate varchar(8)
DECLARE @FinalDate varchar(8)

-- Initialize Variables
SET @PreviousDate = (CONVERT(varchar(8), DATEADD(dd, -1, GETDATE()), 112)); -- Yesterday
SELECT @PreviousDate AS [Start Date]
SET @FinalDate = (CONVERT(varchar(8), DATEADD(dd, +1, GETDATE()), 112)); -- Tomorrow
SELECT @FinalDate AS [End Date]

-- Identify failed jobs within last day
SELECT *
FROM msdb.dbo.sysjobhistory h
INNER JOIN msdb.dbo.sysjobs j
ON h.job_id = j.job_id
INNER JOIN msdb.dbo.sysjobsteps s
ON j.job_id = s.job_id
INNER JOIN (SELECT DISTINCT job_id FROM msdb..sysjobhistory WHERE run_status <> 1) a
ON h.job_id = a.job_id
WHERE h.run_status = 0 -- Failure
AND h.run_date >= @PreviousDate AND h.run_date < @FinalDate

Post #1395882
Posted Wednesday, December 12, 2012 2:12 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Yesterday @ 12:12 PM
Points: 320, Visits: 1,916
Thanks to both of you. Appreciate it.

SueTons.
Post #1395889
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse