如何使用master.dbo.xp_sqlagent_enum_jobs获得上次运行结果
CREATE TABLE #enum_job
(
Job_ID UNIQUEIDENTIFIER,
Last_Run_Date INT,
Last_Run_Time INT,
Next_Run_Date INT,
Next_Run_Time INT,
Next_Run_Schedule_ID INT,
Requested_To_Run INT,
Request_Source INT,
Request_Source_ID VARCHAR(100),
Running INT,
Current_Step INT,
Current_Retry_Attempt INT,
State INT
)
INSERT INTO #enum_job
EXEC master.dbo.xp_sqlagent_enum_jobs 1, garbage
SELECT *
FROM #enum_job
答案 0 :(得分:0)
您的问题有点不清楚,并且您没有说明您拥有哪个版本的MSSQL,但假设您想要查找每个作业的最新作业结果,那么您可以直接查询作业表:
select
j.name as 'Job', jh.run_status as 'Result of last run'
from
msdb.dbo.sysjobs j
join msdb.dbo.sysjobhistory jh
on j.job_id = jh.job_id
where
jh.step_id = 0 and
jh.run_date = (select max(run_date) from msdb.dbo.sysjobhistory where job_id = jh.job_id) and
jh.run_time = (select max(run_time) from msdb.dbo.sysjobhistory where job_id = jh.job_id and run_date = jh.run_date)
有关run_status值的列表,请参阅sysjobhistory
的文档。作业表将日期和时间分别存储为整数,因此如果您进行大量查询,则可能需要编写一个函数来将(run_date, run_time
)对转换为单个datetime
值。通常,从TSQL查询作业信息并不像应该的那样容易,因此您可以考虑使用Smo。