asked on
ASKER
SELECT h.server, j.name, h.step_name, h.message, h.run_date, h.run_time, h.run_duration
FROM msdb..sysjobs j
INNER JOIN msdb..sysjobhistory h ON j.job_id=h.job_id
WHERE h.run_status=0 -- Failed
ORDER BY h.run_date, h.run_time
ASKER
;WITH jobListCTE as
(
SELECT j.name as job_name,
msdb.dbo.agent_datetime(run_date, run_time) AS run_datetime,
RIGHT('000000' + CONVERT(varchar(6), run_duration), 6) AS run_duration,
message
FROM msdb..sysjobhistory h
INNER JOIN msdb..sysjobs j ON h.job_id = j.job_id
WHERE h.step_name = '(Job outcome)' and h.run_status = 1 /* run status: 1=successs, 0= failed */
)
,CTE2 as
(
SELECT job_name as [JobStep],
run_datetime as [StartDateTime],
SUBSTRING(run_duration, 1, 2) + ':' +
SUBSTRING(run_duration, 3, 2) + ':' +
SUBSTRING(run_duration, 5, 2) as [Duration],
message
,ROW_NUMBER()over(partition by job_name order by run_datetime desc) RN
FROM jobListCTE
)
SELECT * FROM CTE2 T1
WHERE RN = 1
order by [JobStep]
this script don't run well on my side so I change it to:You'll need to run it in the msdb context but I saw that you added the reference to msdb to the table name and that's also fine.
ASKER
is there anyway to integrate the job steps and the failure message of each steps of it if it is failed ?JobHistory table has that information but your query is only filtering by succeeded job executions: WHERE h.step_name = '(Job outcome)' and h.run_status = 1 /* run status: 1=successs, 0= failed */
ASKER
but I can also filter out only failed record by changing run_status to 0, right?Right and Agree :)
if I remove run_status as the condition then it return all, agree?
ASKER
ASKER
;WITH jobListCTE as
(
SELECT j.name as job_name,
msdb.dbo.agent_datetime(run_date, run_time) AS run_datetime,
RIGHT('000000' + CONVERT(varchar(6), run_duration), 6) AS run_duration,
message
FROM msdb..sysjobhistory h
INNER JOIN msdb..sysjobs j ON h.job_id = j.job_id
WHERE h.step_name = '(Job outcome)' and h.run_status = 1 /* run status: 1=successs, 0= failed */
)
,CTE2 as
(
SELECT job_name as [JobStep],
run_datetime as [StartDateTime],
SUBSTRING(run_duration, 1, 2) + ':' +
SUBSTRING(run_duration, 3, 2) + ':' +
SUBSTRING(run_duration, 5, 2) as [Duration],
message
,ROW_NUMBER()over(partition by job_name order by run_datetime desc) RN
FROM jobListCTE
)
ASKER
ASKER
ASKER
nono. what I need is the information on who execute the job and not the job owner information.I couldn't find that information nowhere. Will try to dig on this and back to you.
ASKER
ASKER
ASKER
ASKER
ASKER
ASKER
CTE2 as
(
SELECT job_name as [JobStep],
run_datetime as [StartDateTime],
SUBSTRING(run_duration, 1, 2) + ':' +
SUBSTRING(run_duration, 3, 2) + ':' +
SUBSTRING(run_duration, 5, 2) as [Duration],
message,
[Job description]
,ROW_NUMBER()over(partition by job_name order by run_datetime desc) RN
FROM jobListCTE
)
CTE2 as
(
SELECT job_name as [JobStep],
run_datetime as [StartDateTime],
SUBSTRING(run_duration, 1, 2) + ':' +
SUBSTRING(run_duration, 3, 2) + ':' +
SUBSTRING(run_duration, 5, 2) as [Duration],
message,
[Job description]
,ROW_NUMBER()over(partition by job_name order by run_datetime desc) RN
FROM jobListCTE
where run_datetime > DATEADD(week, GETDATE(), -2) -- Last 2 weeks
)
Msg 8116, Level 16, State 1, Line 1
Argument data type datetime is invalid for argument 2 of dateadd function.
ASKER
,CTE2 as
(
SELECT job_name as [JobStep],
run_datetime as [StartDateTime],
SUBSTRING(run_duration, 1, 2) + ':' +
SUBSTRING(run_duration, 3, 2) + ':' +
SUBSTRING(run_duration, 5, 2) as [Duration],
message,
[Job description]
,ROW_NUMBER()over(partition by job_name order by run_datetime desc) RN
FROM jobListCTE
where DATEDIFF(month, run_datetime, GETDATE()) <= 2 -- Last 2 months
)
,CTE2 as
(
SELECT job_name as [JobStep],
run_datetime as [StartDateTime],
SUBSTRING(run_duration, 1, 2) + ':' +
SUBSTRING(run_duration, 3, 2) + ':' +
SUBSTRING(run_duration, 5, 2) as [Duration],
message,
[Job description]
,ROW_NUMBER()over(partition by job_name order by run_datetime desc) RN
FROM jobListCTE
where DATEDIFF(day, run_datetime, GETDATE()) <= 2
)
ASKER
ASKER
from time to time I run this it doesn't show up, it just say error.Check which columns are you retrieving from.
ASKER
ASKER
ASKER
ASKER
but one thing, can we show both the result from condition with WHERE h.step_name = '(Job outcome)' and without that?Sure. Just remove that condition so will bring all.
ASKER
ASKER
ASKER
ASKER
ASKER
ASKER
Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.
TRUSTED BY
Open in new window