SQL 服务器和 sysjobhistory run_status 查询
SQL Server and sysjobhistory run_status query
我在编写查询来执行我想要的操作时遇到问题。在 SQL 服务器中有一个名为 msdb.dbo.sysjobhistory 的系统 table。它包含 SQL 代理作业的历史记录 运行 并在服务器上执行操作。 table 的每一行对应于作业中的一个步骤。 table 中有一列名为 run_status,它获取一个整数值以指示作业完成并写入 table 时的结果。
我通过一些报告和简单的查询跟踪此 table 中的信息。上周我发现了一个有趣的案例,我没有在我的报告中说明这一点。在 table 中,即使每一行都是作业中的一个步骤,也有一个作为总体结果的作业的步骤 0 条目。通常我只查看步骤 0 行并在该行中获取作业的 run_status 并将其用作结果。但是,作业中的某个步骤可能具有 'failure' 状态,但步骤 0 中的整体作业结果具有 'succeeded' 状态。我提出了以下查询来查找这些情况:
SELECT job_id, step_id, step_name, run_date, run_time, run_status, W.parent_status
FROM msdb.dbo.sysjobhistory [H]
CROSS APPLY
( SELECT TOP 1 run_status AS parent_status
FROM msdb.dbo.sysjobhistory
WHERE job_id = H.job_id
AND run_date <= H.run_date
AND step_id = 0
AND (CASE WHEN run_date = H.run_date THEN run_Time ELSE H.run_time END) <= H.run_time
ORDER BY run_date DESC, run_time DESC
) AS [W]
ORDER BY run_date, run_time, step_id
对于一个特定的 job_id:
,此查询将给出类似于以下的结果集
job_id | step_id | step_name | run_date | run_time | run_status | parent_status
-------------------------------------------------------------------------------
A12345 | 0 | (outcome) | 20170112 | 40000 | 1 | 1
A12345 | 1 | step 1 | 20170112 | 43000 | 1 | 1
A12345 | 2 | step 2 | 20170112 | 50000 | 0 | 1
A12345 | 3 | step 3 | 20170112 | 53000 | 1 | 1
在这种情况下,run_status 为 0 表示失败,run_status 为 1 表示成功。
现在我有了这个 table,我想用它来找出一份工作到底发生了什么。我需要做一个 CASE 语句或类似的东西,在必要时将 "override" 步骤 0 的 run_status。因此,如果出现上述情况,则假设我想将 run_status 设为 2 的值。所以现在当我只查询工作结果步骤时,我会得到上面工作的类似信息:
job_id | step_id | step_name | run_date | run_time | run_status
---------------------------------------------------------------
A12345 | 0 | (outcome) | 20170112 | 40000 | 2
我知道我想做什么,只是不确定如何编写 SQL 来完成它。我需要以某种方式将父状态与所有 run_status 结果进行比较,当父状态 = 1 且 run_status = 0 时,然后更改步骤 0 的 run_status。感谢您的帮助.
select
(case when sh.run_status = 1 and tt.job_id is not null then 2 else sh.run_status end) as run_status
, *
from dbo.sysjobhistory sh
outer apply (
select
top (1)
tsh.job_id
from dbo.sysjobhistory tsh
where sh.job_id = tsh.job_id
and sh.step_id <> tsh.step_id
and tsh.run_status = 0
) tt
我在编写查询来执行我想要的操作时遇到问题。在 SQL 服务器中有一个名为 msdb.dbo.sysjobhistory 的系统 table。它包含 SQL 代理作业的历史记录 运行 并在服务器上执行操作。 table 的每一行对应于作业中的一个步骤。 table 中有一列名为 run_status,它获取一个整数值以指示作业完成并写入 table 时的结果。
我通过一些报告和简单的查询跟踪此 table 中的信息。上周我发现了一个有趣的案例,我没有在我的报告中说明这一点。在 table 中,即使每一行都是作业中的一个步骤,也有一个作为总体结果的作业的步骤 0 条目。通常我只查看步骤 0 行并在该行中获取作业的 run_status 并将其用作结果。但是,作业中的某个步骤可能具有 'failure' 状态,但步骤 0 中的整体作业结果具有 'succeeded' 状态。我提出了以下查询来查找这些情况:
SELECT job_id, step_id, step_name, run_date, run_time, run_status, W.parent_status
FROM msdb.dbo.sysjobhistory [H]
CROSS APPLY
( SELECT TOP 1 run_status AS parent_status
FROM msdb.dbo.sysjobhistory
WHERE job_id = H.job_id
AND run_date <= H.run_date
AND step_id = 0
AND (CASE WHEN run_date = H.run_date THEN run_Time ELSE H.run_time END) <= H.run_time
ORDER BY run_date DESC, run_time DESC
) AS [W]
ORDER BY run_date, run_time, step_id
对于一个特定的 job_id:
,此查询将给出类似于以下的结果集job_id | step_id | step_name | run_date | run_time | run_status | parent_status
-------------------------------------------------------------------------------
A12345 | 0 | (outcome) | 20170112 | 40000 | 1 | 1
A12345 | 1 | step 1 | 20170112 | 43000 | 1 | 1
A12345 | 2 | step 2 | 20170112 | 50000 | 0 | 1
A12345 | 3 | step 3 | 20170112 | 53000 | 1 | 1
在这种情况下,run_status 为 0 表示失败,run_status 为 1 表示成功。 现在我有了这个 table,我想用它来找出一份工作到底发生了什么。我需要做一个 CASE 语句或类似的东西,在必要时将 "override" 步骤 0 的 run_status。因此,如果出现上述情况,则假设我想将 run_status 设为 2 的值。所以现在当我只查询工作结果步骤时,我会得到上面工作的类似信息:
job_id | step_id | step_name | run_date | run_time | run_status
---------------------------------------------------------------
A12345 | 0 | (outcome) | 20170112 | 40000 | 2
我知道我想做什么,只是不确定如何编写 SQL 来完成它。我需要以某种方式将父状态与所有 run_status 结果进行比较,当父状态 = 1 且 run_status = 0 时,然后更改步骤 0 的 run_status。感谢您的帮助.
select
(case when sh.run_status = 1 and tt.job_id is not null then 2 else sh.run_status end) as run_status
, *
from dbo.sysjobhistory sh
outer apply (
select
top (1)
tsh.job_id
from dbo.sysjobhistory tsh
where sh.job_id = tsh.job_id
and sh.step_id <> tsh.step_id
and tsh.run_status = 0
) tt