zoukankan      html  css  js  c++  java
  • T-SQL语句查看作业等信息

    因服务器需要迁移,需要将现有JOB迁移至新服务器,待服务器调整完毕,则重新迁移到原服务器,所以在做迁移之前希望将现有JOB进行备份,不至于乱了执行时间。
    1.查看所有作业列表
    USE master
    SELECT 
        [sJOB].[job_id] AS [JobID]
        , [sJOB].[name] AS [JobName]
        , [sDBP].[name] AS [JobOwner]
        , [sCAT].[name] AS [JobCategory]
        , [sJOB].[description] AS [JobDescription]
        , CASE [sJOB].[enabled]
            WHEN 1 THEN 'Yes'
            WHEN 0 THEN 'No'
          END AS [IsEnabled]
        , [sJOB].[date_created] AS [JobCreatedOn]
        , [sJOB].[date_modified] AS [JobLastModifiedOn]
        , [sSVR].[name] AS [OriginatingServerName]
        , [sJSTP].[step_id] AS [JobStartStepNo]
        , [sJSTP].[step_name] AS [JobStartStepName]
        , CASE
            WHEN [sSCH].[schedule_uid] IS NULL THEN 'No'
            ELSE 'Yes'
          END AS [IsScheduled]
        , [sSCH].[schedule_uid] AS [JobScheduleID]
        , [sSCH].[name] AS [JobScheduleName]
        , CASE [sJOB].[delete_level]
            WHEN 0 THEN 'Never'
            WHEN 1 THEN 'On Success'
            WHEN 2 THEN 'On Failure'
            WHEN 3 THEN 'On Completion'
          END AS [JobDeletionCriterion]
    FROM
        [msdb].[dbo].[sysjobs] AS [sJOB]
        LEFT JOIN [msdb].[sys].[servers] AS [sSVR]
            ON [sJOB].[originating_server_id] = [sSVR].[server_id]
        LEFT JOIN [msdb].[dbo].[syscategories] AS [sCAT]
            ON [sJOB].[category_id] = [sCAT].[category_id]
        LEFT JOIN [msdb].[dbo].[sysjobsteps] AS [sJSTP]
            ON [sJOB].[job_id] = [sJSTP].[job_id]
            AND [sJOB].[start_step_id] = [sJSTP].[step_id]
        LEFT JOIN [msdb].[sys].[database_principals] AS [sDBP]
            ON [sJOB].[owner_sid] = [sDBP].[sid]
        LEFT JOIN [msdb].[dbo].[sysjobschedules] AS [sJOBSCH]
            ON [sJOB].[job_id] = [sJOBSCH].[job_id]
        LEFT JOIN [msdb].[dbo].[sysschedules] AS [sSCH]
            ON [sJOBSCH].[schedule_id] = [sSCH].[schedule_id]
    ORDER BY [JobName]
    
    2.查看最近一次作业执行情况
    USE master
    SELECT 
        [sJOB].[job_id] AS [JobID]
        , [sJOB].[name] AS [JobName]
        , CASE 
            WHEN [sJOBH].[run_date] IS NULL OR [sJOBH].[run_time] IS NULL THEN NULL
            ELSE CAST(
                    CAST([sJOBH].[run_date] AS CHAR(8))
                    + ' ' 
                    + STUFF(
                        STUFF(RIGHT('000000' + CAST([sJOBH].[run_time] AS VARCHAR(6)),  6)
                            , 3, 0, ':')
                        , 6, 0, ':')
                    AS DATETIME)
          END AS [LastRunDateTime]
        , CASE [sJOBH].[run_status]
            WHEN 0 THEN 'Failed'
            WHEN 1 THEN 'Succeeded'
            WHEN 2 THEN 'Retry'
            WHEN 3 THEN 'Canceled'
            WHEN 4 THEN 'Running' -- In Progress
          END AS [LastRunStatus]
        , STUFF(
                STUFF(RIGHT('000000' + CAST([sJOBH].[run_duration] AS VARCHAR(6)),  6)
                    , 3, 0, ':')
                , 6, 0, ':') 
            AS [LastRunDuration (HH:MM:SS)]
        , [sJOBH].[message] AS [LastRunStatusMessage]
        , CASE [sJOBSCH].[NextRunDate]
            WHEN 0 THEN NULL
            ELSE CAST(
                    CAST([sJOBSCH].[NextRunDate] AS CHAR(8))
                    + ' ' 
                    + STUFF(
                        STUFF(RIGHT('000000' + CAST([sJOBSCH].[NextRunTime] AS VARCHAR(6)),  6)
                            , 3, 0, ':')
                        , 6, 0, ':')
                    AS DATETIME)
          END AS [NextRunDateTime]
    FROM 
        [msdb].[dbo].[sysjobs] AS [sJOB]
        LEFT JOIN (
                    SELECT
                        [job_id]
                        , MIN([next_run_date]) AS [NextRunDate]
                        , MIN([next_run_time]) AS [NextRunTime]
                    FROM [msdb].[dbo].[sysjobschedules]
                    GROUP BY [job_id]
                ) AS [sJOBSCH]
            ON [sJOB].[job_id] = [sJOBSCH].[job_id]
        LEFT JOIN (
                    SELECT 
                        [job_id]
                        , [run_date]
                        , [run_time]
                        , [run_status]
                        , [run_duration]
                        , [message]
                        , ROW_NUMBER() OVER (
                                                PARTITION BY [job_id] 
                                                ORDER BY [run_date] DESC, [run_time] DESC
                          ) AS RowNumber
                    FROM [msdb].[dbo].[sysjobhistory]
                    WHERE [step_id] = 0
                ) AS [sJOBH]
            ON [sJOB].[job_id] = [sJOBH].[job_id]
            AND [sJOBH].[RowNumber] = 1
    ORDER BY [JobName]
    
    3.查看作业步骤
    在msdb库中执行
    sp_help_jobstep 'AF168757-4F01-44A6-AC59-4DDAED6F8FED' (作业ID)

    4.查看所有作业
    在msdb库中执行
    sp_help_job --无参数  

    5.查看作业时间间隔等
    在msdb库中执行
    sp_help_jobschedule 'AF168757-4F01-44A6-AC59-4DDAED6F8FED'(作业ID) 
    

      

    来自CSDN:http://bbs.csdn.net/topics/390393172
  • 相关阅读:
    Java实验四
    Java第八周学习总结
    Java实验三
    输入输出练习
    第七周
    20145113 实验二 Java面向对象程序设计
    2020-2021-1 20209306 《linux内核原理与分析》第九周作业
    2020-2021-1 20209306 《linux内核原理与分析》第八周作业
    2020-2021-1 20209306 《linux内核原理与分析》第七周作业
    2020-2021-1 20209306 《linux内核原理与分析》第六周作业
  • 原文地址:https://www.cnblogs.com/weschen/p/6264763.html
Copyright © 2011-2022 走看看