SQL server 中的作业信息查询

作业信息存储在MSDB中.可以运行以下存储过程,监视作业的执行情况.

可以参考如下SQL:

SELECT 作业的名称 = name,
       对作业的说明 = description,
       计划运行作业的下一个日期 = (SELECT next_run_date
                       FROM   sysjobschedules
                       WHERE  job_id = sysjobs.job_id),
       计划运行作业的时间 = (SELECT next_run_time
                    FROM   sysjobschedules
                    WHERE  job_id = sysjobs.job_id),
       作业的执行状态 = CASE (SELECT   top 1   run_status
                       FROM     sysjobhistory
                       WHERE    job_id = sysjobs.job_id
                       ORDER BY instance_id DESC)
                   WHEN 0 THEN '失败'
                   WHEN 1 THEN '成功'
                   WHEN 2 THEN '重试'
                   WHEN 3 THEN '已取消'
                   WHEN 4 THEN '正在进行中'
                 END,
       作业或步骤开始执行的日期 = (SELECT   top 1   run_date
                       FROM     sysjobhistory
                       WHERE    job_id = sysjobs.job_id
                       ORDER BY instance_id DESC),
       作业或步骤开始的时间 = (SELECT   top 1   run_time
                     FROM     sysjobhistory
                     WHERE    job_id = sysjobs.job_id
                     ORDER BY instance_id DESC),
       执行作业或步骤所花费的时间 = (SELECT   top 1   run_duration
                        FROM     sysjobhistory
                        WHERE    job_id = sysjobs.job_id
                        ORDER BY instance_id DESC)
FROM   sysjobs

 如果监视备份作业的执行计划,可以在描述中输入:"备份" 等字样,查询时候可以用description 描述信息过滤.如下:

(注意:对某些字段尽心了翻译和转换)

SELECT 作业的名称 = name,
       对作业的说明 = description,
       计划运行作业的下一个日期 = (SELECT left(left(next_run_date,4)+'-'+right(next_run_date,4),7)+'-'+right(next_run_date,2)
                       FROM   sysjobschedules
                       WHERE  job_id = sysjobs.job_id),
       计划运行作业的时间 = (SELECT left(left(left('000000',6-len(next_run_time))+ltrim(next_run_time),2)+':'+right(left('000000',6-len(next_run_time))+ltrim(next_run_time),4),5)+':'+right(left('000000',6-len(next_run_time))+ltrim(next_run_time),2)
                    FROM   sysjobschedules
                    WHERE  job_id = sysjobs.job_id),
       作业的执行状态 = CASE (SELECT   top 1   run_status
                       FROM     sysjobhistory
                       WHERE    job_id = sysjobs.job_id
                       ORDER BY instance_id DESC)
                   WHEN 0 THEN '失败'
                   WHEN 1 THEN '成功'
                   WHEN 2 THEN '重试'
                   WHEN 3 THEN '已取消'
                   WHEN 4 THEN '正在进行中'
                 END,
       作业或步骤开始执行的日期 = (SELECT   top 1   left(left(run_date,4)+'-'+right(run_date,4),7)+'-'+right(run_date,2)
                       FROM     sysjobhistory
                       WHERE    job_id = sysjobs.job_id
                       ORDER BY instance_id DESC),
       作业或步骤开始的时间 = (SELECT   top 1   left(left(left('000000',6-len(run_time))+ltrim(run_time),2)+':'+right(left('000000',6-len(run_time))+ltrim(run_time),4),5)+':'+right(left('000000',6-len(run_time))+ltrim(run_time),2)
                     FROM     sysjobhistory
                     WHERE    job_id = sysjobs.job_id
                     ORDER BY instance_id DESC),
       执行作业或步骤所花费的时间 = (SELECT   top 1  left(left(left('000000',6-len(run_duration))+ltrim(run_duration),2)+'小时'+right(left('000000',6-len(run_duration))+ltrim(run_duration),4),5)+'分钟'+right(left('000000',6-len(run_duration))+ltrim(run_duration),2)+'秒'
                        FROM     sysjobhistory
                        WHERE    job_id = sysjobs.job_id
                        ORDER BY instance_id DESC)
FROM   sysjobs
WHERE description LIKE '%备份%'

这样就可以监视到备份数据库计划的执行,前台页面就可以用Ajax来无刷新监视备份情况了.

测试后,SQL server 2005 也使用.

你可能感兴趣的:(sql,Ajax,Date,数据库,server,存储,作业)