sql 服务器代理作业的最新两个状态
The latest two status of sql server agent jobs
我需要进行查询以获取 sql 服务器代理作业的状态。
棘手的部分是,我既想要作业上次失败的时间(如果有的话),又要上次 运行 成功的时间 - 这可能吗?
到目前为止,我只能获得最后一次成功 运行 或最后一次失败 运行 的作业。
SELECT
job.name
,jobh.run_date
,DATEADD(HOUR, (jobh.run_time / 1000000) % 100,
DATEADD(MINUTE, (jobh.run_time / 10000) % 100,
DATEADD(SECOND, (jobh.run_time / 100) % 100,
DATEADD(MILLISECOND, (jobh.run_time % 100) * 10,
cast('00:00:00' as TIME(0)))))) AS 'tidspunkt'
,jobh.run_duration
FROM msdb.dbo.sysjobhistory jobh
JOIN [msdb].[dbo].[sysjobs] job ON jobh.job_id = job.job_id
WHERE jobh.step_id = 0
AND jobh.message LIKE '%failed%'
AND (jobh.run_date = CONVERT(VARCHAR, GETDATE(), 112) OR jobh.run_date = CONVERT(VARCHAR, DATEADD(DAY, -1, GETDATE()), 112) )
AND job.name = 'UpdateCPR'
ORDER BY jobh.run_date DESC, jobh.run_time DESC
只需更改线路...
AND jobh.message LIKE '%failed%'
...关注...
AND (jobh.message LIKE '%failed%' OR jobh.message LIKE '%succeeded%')
您将获得多行。如果您愿意,您可以按 LIKE 结果和 select TOP 1 将其分组,以便在一行中检索它。
编辑
使用它在一行中获取失败和成功信息。
;WITH jobRuns AS (
SELECT
job.job_id,
job.name,
jobh.run_time,
jobh.run_duration,
execStatus
FROM msdb.dbo.sysjobhistory jobh
JOIN msdb.dbo.sysjobs job
ON jobh.job_id = job.job_id
CROSS APPLY(
SELECT execStatus = CASE
WHEN jobh.message LIKE '%failed%' THEN 0
WHEN jobh.message LIKE '%succeeded%' THEN 1 END) ext
WHERE
jobh.step_id = 0
and job.name LIKE '%testjob%'
/*GROUP BY
job.job_id,
job.name,
jobh.run_time,
execStatus*/)
,lastRuns AS (
SELECT TOP 1
jobRuns.job_id,
jobRuns.name,
run_time_failed = failed.run_time,
run_duration_failed = failed.run_duration,
run_time_succeeded = success.run_time,
run_duration_succeeded = success.run_duration
FROM jobRuns
CROSS APPLY(
SELECT TOP 1 run_time, run_duration
FROM jobRuns
WHERE execStatus = 0
ORDER BY run_time DESC) failed
CROSS APPLY(
SELECT TOP 1 run_time, run_duration
FROM jobRuns
WHERE execStatus = 1
ORDER BY run_time DESC) success
)
SELECT *
FROM lastRuns
我需要进行查询以获取 sql 服务器代理作业的状态。
棘手的部分是,我既想要作业上次失败的时间(如果有的话),又要上次 运行 成功的时间 - 这可能吗?
到目前为止,我只能获得最后一次成功 运行 或最后一次失败 运行 的作业。
SELECT
job.name
,jobh.run_date
,DATEADD(HOUR, (jobh.run_time / 1000000) % 100,
DATEADD(MINUTE, (jobh.run_time / 10000) % 100,
DATEADD(SECOND, (jobh.run_time / 100) % 100,
DATEADD(MILLISECOND, (jobh.run_time % 100) * 10,
cast('00:00:00' as TIME(0)))))) AS 'tidspunkt'
,jobh.run_duration
FROM msdb.dbo.sysjobhistory jobh
JOIN [msdb].[dbo].[sysjobs] job ON jobh.job_id = job.job_id
WHERE jobh.step_id = 0
AND jobh.message LIKE '%failed%'
AND (jobh.run_date = CONVERT(VARCHAR, GETDATE(), 112) OR jobh.run_date = CONVERT(VARCHAR, DATEADD(DAY, -1, GETDATE()), 112) )
AND job.name = 'UpdateCPR'
ORDER BY jobh.run_date DESC, jobh.run_time DESC
只需更改线路...
AND jobh.message LIKE '%failed%'
...关注...
AND (jobh.message LIKE '%failed%' OR jobh.message LIKE '%succeeded%')
您将获得多行。如果您愿意,您可以按 LIKE 结果和 select TOP 1 将其分组,以便在一行中检索它。
编辑
使用它在一行中获取失败和成功信息。
;WITH jobRuns AS (
SELECT
job.job_id,
job.name,
jobh.run_time,
jobh.run_duration,
execStatus
FROM msdb.dbo.sysjobhistory jobh
JOIN msdb.dbo.sysjobs job
ON jobh.job_id = job.job_id
CROSS APPLY(
SELECT execStatus = CASE
WHEN jobh.message LIKE '%failed%' THEN 0
WHEN jobh.message LIKE '%succeeded%' THEN 1 END) ext
WHERE
jobh.step_id = 0
and job.name LIKE '%testjob%'
/*GROUP BY
job.job_id,
job.name,
jobh.run_time,
execStatus*/)
,lastRuns AS (
SELECT TOP 1
jobRuns.job_id,
jobRuns.name,
run_time_failed = failed.run_time,
run_duration_failed = failed.run_duration,
run_time_succeeded = success.run_time,
run_duration_succeeded = success.run_duration
FROM jobRuns
CROSS APPLY(
SELECT TOP 1 run_time, run_duration
FROM jobRuns
WHERE execStatus = 0
ORDER BY run_time DESC) failed
CROSS APPLY(
SELECT TOP 1 run_time, run_duration
FROM jobRuns
WHERE execStatus = 1
ORDER BY run_time DESC) success
)
SELECT *
FROM lastRuns