SQL SERVER 中如何用腳本管理做業

 在SQL SERVER中用腳本管理做業,在絕大部分場景下,腳本都比UI界面管理做業要高效、簡潔。打個簡單的比方,若是你要查看做業的運行時長,若是用UI界面查看,100個做業,你就得在歷史記錄裏面至少查看一百次甚至更多,還要記錄、統計做業各個步驟的執行時間。而用腳本,一個查詢就OK了。這篇文章分享一些我在數據庫管理過程當中積累的一些經常使用腳本 若有不足或須要完善的地方,也請多多指教。sql

 

1:業務場景:你想了解一下全部做業的Schedule信息,方便你做出調整或分析。例如做業的執行頻率;例如你想查詢那些做業是一小時執行一次的,那些是間隔幾分鐘執行一次的,使用下面腳原本查看吧。數據庫

DECLARE @ManyTimes TABLE
(
    freq_relative_interval INT,
    times  NVARCHAR(12)
)
INSERT INTO @ManyTimes
SELECT 1  ,N'第一個' UNION ALL
SELECT 2  ,N'第二個' UNION ALL
SELECT 4  ,N'第三個' UNION ALL
SELECT 8  ,N'第四個' UNION ALL
SELECT 16 ,N'最後一個'
 
DECLARE @WeekDays TABLE
(
    freq_interval    INT,
    weekdays        NVARCHAR(120)
 
)
 
INSERT INTO @WeekDays
SELECT 1    ,N'星期日               '  UNION ALL
SELECT 2    ,N'星期一               '  UNION ALL
SELECT 4    ,N'星期二               '  UNION ALL
SELECT 8    ,N'星期三               '  UNION ALL
SELECT 16   ,N'星期四               '  UNION ALL
SELECT 32   ,N'星期五               '  UNION ALL
SELECT 64   ,N'星期六               '  UNION ALL
SELECT 3    ,N'星期日,一            '  UNION ALL
SELECT 5    ,N'星期日,二            '  UNION ALL
SELECT 9    ,N'星期日,三            '  UNION ALL
SELECT 17   ,N'星期日,四            '  UNION ALL
SELECT 33   ,N'星期日,五            '  UNION ALL
SELECT 65   ,N'星期日,六            '  UNION ALL
SELECT 6    ,N'星期一,二            '  UNION ALL
SELECT 10   ,N'星期一,三            '  UNION ALL
SELECT 18   ,N'星期一,四            '  UNION ALL
SELECT 34   ,N'星期一,五            '  UNION ALL
SELECT 66   ,N'星期一,六            '  UNION ALL
SELECT 12   ,N'星期二,三            '  UNION ALL
SELECT 20   ,N'星期二,四            '  UNION ALL
SELECT 36   ,N'星期二,五            '  UNION ALL
SELECT 68   ,N'星期二,六            '  UNION ALL
SELECT 24   ,N'星期三,四            '  UNION ALL
SELECT 40   ,N'星期三,五            '  UNION ALL
SELECT 72   ,N'星期三,六            '  UNION ALL
SELECT 48   ,N'星期四,五            '  UNION ALL
SELECT 80   ,N'星期四,六            '  UNION ALL
SELECT 96   ,N'星期五,六            '  UNION ALL
SELECT 7    ,N'星期日,一,二         '  UNION ALL
SELECT 11   ,N'星期日,一,三         '  UNION ALL
SELECT 19   ,N'星期日,一,四         '  UNION ALL
SELECT 35   ,N'星期日,一,五         '  UNION ALL
SELECT 67   ,N'星期日,一,六         '  UNION ALL
SELECT 13   ,N'星期日,二,三         '  UNION ALL
SELECT 21   ,N'星期日,二,四         '  UNION ALL
SELECT 37   ,N'星期日,二,五         '  UNION ALL
SELECT 69   ,N'星期日,二,六         '  UNION ALL
SELECT 25   ,N'星期日,三,四         '  UNION ALL
SELECT 41   ,N'星期日,三,五         '  UNION ALL
SELECT 73   ,N'星期日,三,六         '  UNION ALL
SELECT 49   ,N'星期日,四,五         '  UNION ALL
SELECT 81   ,N'星期日,四,六         '  UNION ALL
SELECT 97   ,N'星期日,五,六         '  UNION ALL
SELECT 14   ,N'星期一,二,三         '  UNION ALL
SELECT 22   ,N'星期一,二,四         '  UNION ALL
SELECT 38   ,N'星期一,二,五         '  UNION ALL
SELECT 70   ,N'星期一,二,六         '  UNION ALL
SELECT 26   ,N'星期一,三,四         '  UNION ALL
SELECT 42   ,N'星期一,三,五         '  UNION ALL
SELECT 74   ,N'星期一,三,六         '  UNION ALL
SELECT 50   ,N'星期一,四,五         '  UNION ALL
SELECT 82   ,N'星期一,四,六         '  UNION ALL
SELECT 98   ,N'星期一,五,六         '  UNION ALL
SELECT 28   ,N'星期二,三,四         '  UNION ALL
SELECT 44   ,N'星期二,三,五         '  UNION ALL
SELECT 76   ,N'星期二,三,六         '  UNION ALL
SELECT 52   ,N'星期二,四,五         '  UNION ALL
SELECT 84   ,N'星期二,四,六         '  UNION ALL
SELECT 100  ,N'星期二,五,六         '  UNION ALL
SELECT 56   ,N'星期三,四,五         '  UNION ALL
SELECT 88   ,N'星期三,四,六         '  UNION ALL
SELECT 104  ,N'星期三,五,六         '  UNION ALL
SELECT 112  ,N'星期四,五,六         '  UNION ALL
SELECT 15   ,N'星期日,一,二,三      '  UNION ALL
SELECT 23   ,N'星期日,一,二,四      '  UNION ALL
SELECT 39   ,N'星期日,一,二,五      '  UNION ALL
SELECT 71   ,N'星期日,一,二,六      '  UNION ALL
SELECT 27   ,N'星期日,一,三,四      '  UNION ALL
SELECT 43   ,N'星期日,一,三,五      '  UNION ALL
SELECT 75   ,N'星期日,一,三,六      '  UNION ALL
SELECT 51   ,N'星期日,一,四,五      '  UNION ALL
SELECT 83   ,N'星期日,一,四,六      '  UNION ALL
SELECT 99   ,N'星期日,一,五,六      '  UNION ALL
SELECT 29   ,N'星期日,二,三,四      '  UNION ALL
SELECT 45   ,N'星期日,二,三,五      '  UNION ALL
SELECT 77   ,N'星期日,二,三,六      '  UNION ALL
SELECT 53   ,N'星期日,二,四,五      '  UNION ALL
SELECT 85   ,N'星期日,二,四,六      '  UNION ALL
SELECT 101  ,N'星期日,二,五,六      '  UNION ALL
SELECT 57   ,N'星期日,三,四,五      '  UNION ALL
SELECT 89   ,N'星期日,三,四,六      '  UNION ALL
SELECT 105  ,N'星期日,三,五,六      '  UNION ALL
SELECT 113  ,N'星期日,四,五,六      '  UNION ALL
SELECT 30   ,N'星期一,二,三,四      '  UNION ALL
SELECT 46   ,N'星期一,二,三,五      '  UNION ALL
SELECT 78   ,N'星期一,二,三,六      '  UNION ALL
SELECT 54   ,N'星期一,二,四,五      '  UNION ALL
SELECT 86   ,N'星期一,二,四,六      '  UNION ALL
SELECT 102  ,N'星期一,二,五,六      '  UNION ALL
SELECT 58   ,N'星期一,三,四,五      '  UNION ALL
SELECT 90   ,N'星期一,三,四,六      '  UNION ALL
SELECT 106  ,N'星期一,三,五,六      '  UNION ALL
SELECT 114  ,N'星期一,四,五,六      '  UNION ALL
SELECT 60   ,N'星期二,三,四,五      '  UNION ALL
SELECT 92   ,N'星期二,三,四,六      '  UNION ALL
SELECT 108  ,N'星期二,三,五,六      '  UNION ALL
SELECT 116  ,N'星期二,四,五,六      '  UNION ALL
SELECT 120  ,N'星期三,四,五,六      '  UNION ALL
SELECT 31   ,N'星期日,一,二,三,四   '  UNION ALL
SELECT 47   ,N'星期日,一,二,三,五   '  UNION ALL
SELECT 79   ,N'星期日,一,二,三,六   '  UNION ALL
SELECT 55   ,N'星期日,一,二,四,五   '  UNION ALL
SELECT 87   ,N'星期日,一,二,四,六   '  UNION ALL
SELECT 103  ,N'星期日,一,二,五,六   '  UNION ALL
SELECT 59   ,N'星期日,一,三,四,五   '  UNION ALL
SELECT 91   ,N'星期日,一,三,四,六   '  UNION ALL
SELECT 107  ,N'星期日,一,三,五,六   '  UNION ALL
SELECT 115  ,N'星期日,一,四,五,六   '  UNION ALL
SELECT 61   ,N'星期日,二,三,四,五   '  UNION ALL
SELECT 93   ,N'星期日,二,三,四,六   '  UNION ALL
SELECT 109  ,N'星期日,二,三,五,六   '  UNION ALL
SELECT 117  ,N'星期日,二,四,五,六   '  UNION ALL
SELECT 121  ,N'星期日,三,四,五,六   '  UNION ALL
SELECT 62   ,N'星期一,二,三,四,五   '  UNION ALL
SELECT 94   ,N'星期一,二,三,四,六   '  UNION ALL
SELECT 110  ,N'星期一,二,三,五,六   '  UNION ALL
SELECT 118  ,N'星期一,二,四,五,六   '  UNION ALL
SELECT 122  ,N'星期一,三,四,五,六   '  UNION ALL
SELECT 124  ,N'星期二,三,四,五,六   '  UNION ALL
SELECT 63   ,N'星期日,一,二,三,四,五'  UNION ALL
SELECT 95   ,N'星期日,一,二,三,四,六'  UNION ALL
SELECT 111  ,N'星期日,一,二,三,五,六'  UNION ALL
SELECT 119  ,N'星期日,一,二,四,五,六'  UNION ALL
SELECT 123  ,N'星期日,一,三,四,五,六'  UNION ALL
SELECT 125  ,N'星期日,二,三,四,五,六'  UNION ALL
SELECT 126  ,N'星期一,二,三,四,五,六'  UNION ALL
SELECT 127  ,N'星期日,一,二,三,四,五,六';      
 
SELECT  d.job_id                                    AS job_id    ,
        j.name                                        AS job_name ,
        CASE WHEN j.enabled =1  THEN N'啓用' 
             ELSE N'禁用' END                        AS job_staus,
        CASE WHEN freq_type=1    THEN N'運行一次'
             WHEN freq_type=4    THEN N'天天執行'
             WHEN freq_type=8    THEN N'每週執行'
             WHEN freq_type=16    THEN N'每個月執行'
             WHEN freq_type=32    THEN N'每個月執行'
             WHEN freq_type=64    THEN N'代理服務啓動時運行'
             WHEN freq_type=128 THEN N'在計算機空閒時運行'
        END                                            AS  freq_type,
        CASE WHEN freq_type=1    THEN  N'選項無心義'
             WHEN freq_type=4    THEN 
                (
                 CASE WHEN freq_subday_type=4     
                      THEN  N'每隔' +  CONVERT(NVARCHAR(4),freq_subday_interval) +N'分鐘執行一次' 
                 WHEN freq_subday_type=8 
                      THEN  N'每隔' +  CONVERT(NVARCHAR(4),freq_subday_interval) +N'小時執行一次'
                 ELSE         N'天天執行'+ CONVERT(NVARCHAR(4),freq_interval)  + N'' 
                 END
                 )
             WHEN freq_type=8    THEN 
                ( SELECT w.weekdays FROM   @WeekDays w WHERE w.freq_interval= s.freq_interval
                 )
                                    
             WHEN freq_type=16  THEN  N'每個月' +  CONVERT(NVARCHAR(4),freq_interval)     + N'號執行'
             WHEN freq_type=32  THEN
                (CASE WHEN s.freq_relative_interval = 0 THEN   N'每個月星期' + CONVERT(NVARCHAR(4),freq_interval)  + N'執行'
                      WHEN s.freq_relative_interval = 1 THEN 
                          ( CASE WHEN s.freq_interval =8  THEN   N'每個月第一天執行' 
                                   ELSE N'每個月' + (SELECT times FROM @ManyTimes 
                                                  WHERE freq_relative_interval=s.freq_relative_interval)
                                      + N'星期'
                                      + CONVERT(NVARCHAR(2),s.freq_interval -1) 
                          END
                           )
                      WHEN s.freq_relative_interval = 2 THEN 
                            (CASE WHEN s.freq_interval =8  THEN   N'每個月次日執行'
                                    ELSE N'每個月'+ (SELECT times FROM @ManyTimes 
                                                   WHERE freq_relative_interval=s.freq_relative_interval)
                                       + N'星期'
                                       + CONVERT(NVARCHAR(2),s.freq_interval -1) 
                             END
                             )
                      WHEN s.freq_relative_interval = 4 THEN 
                            (CASE WHEN s.freq_interval =8  THEN   N'每個月第三天執行'
                                 ELSE N'每個月' + (SELECT times FROM @ManyTimes
                                                 WHERE freq_relative_interval=s.freq_relative_interval)
                                                 
                                    + N'星期'
                                 + CONVERT(NVARCHAR(2),s.freq_interval -1) 
                            END
                             )
                      WHEN s.freq_relative_interval = 8 THEN 
                             (CASE WHEN   s.freq_interval =8  THEN N'每個月第四天執行'
                                  ELSE N'每個月' + (SELECT times FROM @ManyTimes 
                                                 WHERE freq_relative_interval=s.freq_relative_interval)
                                    + N'星期'
                              + CONVERT(NVARCHAR(2),s.freq_interval -1) 
                            END
                             )
                      WHEN s.freq_relative_interval =16 THEN  
                            (CASE WHEN   s.freq_interval =8  THEN   N'每個月最後一天執行'
                                 ELSE N'每個月' + (SELECT times FROM @ManyTimes 
                                                 WHERE freq_relative_interval=s.freq_relative_interval)
                                    + N'星期'
                              + CONVERT(NVARCHAR(2),s.freq_interval -1) 
                             END
                             )
                END )
        END AS freq_relative_interval,
        CASE WHEN freq_subday_type =1 THEN N'指定時間點執行一次' 
             WHEN freq_subday_type =2 THEN N'每隔:' + CAST(freq_subday_interval AS VARCHAR(2)) + N'秒執行一次'
             WHEN freq_subday_type =4 THEN N'每隔:' + CAST(freq_subday_interval AS VARCHAR(2)) + N'分執行一次'
             WHEN freq_subday_type =8 THEN N'每隔:' + CAST(freq_subday_interval AS VARCHAR(2)) + N'小時執行一次'
        END AS freq_subday_type, 
        CASE WHEN freq_subday_type =1 THEN N'開始時間點:' 
                    + CAST(active_start_time / 10000   AS VARCHAR(2)) + N''
                    + CAST(active_start_time%10000/100 AS VARCHAR(2)) + N''
             WHEN freq_subday_type =2 THEN N'開始時間點:' 
                    + CAST(active_start_time / 10000 AS VARCHAR(2)) + N''
                    + CAST(active_start_time%10000/100 AS VARCHAR(2)) + N''
             WHEN freq_subday_type =4 THEN N'開始時間點:' 
                    + CAST(active_start_time / 10000 AS VARCHAR(2))   + N''
                    + CAST(active_start_time%10000/100 AS VARCHAR(2)) + N''
             WHEN freq_subday_type =8 THEN N'開始時間點:'
                    + CAST(active_start_time / 10000 AS VARCHAR(2))   + N''
                    + CAST(active_start_time%10000/100 AS VARCHAR(2)) + N''
        END AS job_start_time, 
        CASE WHEN freq_subday_type =1 THEN N'結束時間點:' 
                    + CAST(active_end_time / 10000   AS VARCHAR(2)) + N''
                    + CAST(active_end_time%10000/100 AS VARCHAR(2)) + N''
             WHEN freq_subday_type =2 THEN N'結束時間點:' 
                    + CAST(active_end_time / 10000 AS VARCHAR(2)) + N''
                    + CAST(active_end_time%10000/100 AS VARCHAR(2)) + N''
             WHEN freq_subday_type =4 THEN N'結束時間點:' 
                    + CAST(active_end_time / 10000 AS VARCHAR(2))   + N''
                    + CAST(active_end_time%10000/100 AS VARCHAR(2)) + N''
             WHEN freq_subday_type =8 THEN N'結束時間點:'
                    + CAST(active_end_time / 10000 AS VARCHAR(2))   + N''
                    + CAST(active_end_time%10000/100 AS VARCHAR(2)) + N''
        END AS job_end_time, 
        freq_type,
        freq_interval,     
        freq_subday_type,
        freq_subday_interval,
        active_start_date,
        active_start_time
FROM msdb.dbo.sysschedules s
INNER JOIN msdb.dbo.sysjobschedules d ON s.schedule_id=d.schedule_id
INNER JOIN msdb.dbo.sysjobs j ON d.job_id = j.job_id
ORDER BY j.name

以下測試案例,很是的清晰明瞭,一目瞭然。session

clipboard

2:業務場景:你想查看這個數據庫實例有多少做業。測試

SELECT j.job_id                                         AS JOB_ID            
      ,j.name                                         AS JOB_NAME          
      ,CASE WHEN [enabled] =1 THEN 'Enabled'
                              ELSE 'Disabled'  END   AS JOB_ENABLED    
      ,l.name                                         AS JOB_OWNER   
      ,j.category_id                                 AS JOB_CATEGORY_ID
      ,c.name                                         AS JOB_CATEGORY_NAME
      ,[description]                                 AS JOB_DESCRIPTION   
      ,date_created                                     AS DATE_CREATED      
      ,date_modified                                 AS DATE_MODIFIED
FROM msdb.dbo.sysjobs j
INNER JOIN msdb.dbo.syscategories c ON j.category_id = c.category_id
INNER JOIN sys.syslogins l ON l.sid = j.owner_sid
ORDER BY j.name

在這個SQL上能夠引伸出跟多的SQL,例如查詢某段時間內建立的做業等等spa

3:業務場景: 你想查看、統計哪些做業被禁止了。操作系統

--查看實例下被禁用的做業
SELECT j.job_id                                         AS JOB_ID            
      ,j.name                                           AS JOB_NAME          
      ,CASE WHEN [enabled] =1 THEN 'Enabled'
                              ELSE 'Disabled'  END      AS JOB_ENABLED    
      ,l.name                                           AS JOB_OWNER   
      ,j.category_id                                    AS JOB_CATEGORY_ID
      ,c.name                                           AS JOB_CATEGORY_NAME
      ,[description]                                    AS JOB_DESCRIPTION   
      ,date_created                                     AS DATE_CREATED      
      ,date_modified                                    AS DATE_MODIFIED
FROM msdb.dbo.sysjobs j
INNER JOIN msdb.dbo.syscategories c ON j.category_id = c.category_id
INNER JOIN sys.syslogins l ON l.sid = j.owner_sid
WHERE j.enabled =0
ORDER BY j.name

4: 業務場景: 你想查看有那些帳號在運行那些做業,例如某個同事離職了,可是他的那個NT帳號在運行一些做業,你須要修改做業的Owner,不然當系統管理員禁用這個NT帳號後,這些做業就會出錯。代理

--查看某個Owner的做業
SELECT j.job_id                                         AS JOB_ID            
      ,j.name                                           AS JOB_NAME          
      ,CASE WHEN [enabled] =1 THEN 'Enabled'
                              ELSE 'Disabled'  END      AS JOB_ENABLED    
      ,l.name                                           AS JOB_OWNER   
      ,j.category_id                                    AS JOB_CATEGORY_ID
      ,c.name                                           AS JOB_CATEGORY_NAME
      ,[description]                                    AS JOB_DESCRIPTION   
      ,date_created                                     AS DATE_CREATED      
      ,date_modified                                    AS DATE_MODIFIED
FROM msdb.dbo.sysjobs j
INNER JOIN msdb.dbo.syscategories c ON j.category_id = c.category_id
INNER JOIN sys.syslogins l ON l.sid = j.owner_sid
WHERE l.name='Domain\UserName'
ORDER BY j.name

5:業務場景:你須要查看做業的具體步驟,例如做業執行的某個存儲過程,或者有個同事想了解做業的運行狀況,可是他不記得做業的名稱,只知道這個做業執行了某個存儲過程,那麼就能夠在下面SQL的基礎上進行擴展。code

SELECT j.job_id                                      AS JOB_ID            
      ,j.name                                        AS JOB_NAME
      ,s.step_id                                     AS Step_Id          
      ,s.command                                     AS Command
      ,CASE WHEN [enabled] =1 THEN 'Enabled'
                              ELSE 'Disabled'  END   AS JOB_ENABLED       
      ,j.category_id                                 AS JOB_CATEGORY_ID
      ,c.name                                        AS JOB_CATEGORY_NAME
      ,[description]                                 AS JOB_DESCRIPTION   
      ,date_created                                  AS DATE_CREATED      
      ,date_modified                                 AS DATE_MODIFIED
FROM msdb.dbo.sysjobs j
INNER JOIN msdb.dbo.syscategories c ON j.category_id = c.category_id
INNER JOIN msdb.dbo.sysjobsteps s ON j.job_id = s.job_id
ORDER BY j.name, s.step_id

--查詢那個做業執行了某個存儲過程(由於有時候,忘記了做業名稱,可是知道那個做業調用了某個存儲過程,此時開發人員找你幫忙找到對應的做業,就能夠經過下面SQL查找)blog

SELECT j.job_id                                      AS JOB_ID            
      ,j.name                                        AS JOB_NAME
      ,s.step_id                                     AS Step_Id          
      ,s.command                                     AS Command
      ,CASE WHEN [enabled] =1 THEN 'Enabled'
                              ELSE 'Disabled'  END   AS JOB_ENABLED       
      ,j.category_id                                 AS JOB_CATEGORY_ID
      ,c.name                                        AS JOB_CATEGORY_NAME
      ,[description]                                 AS JOB_DESCRIPTION   
      ,date_created                                  AS DATE_CREATED      
      ,date_modified                                 AS DATE_MODIFIED
FROM msdb.dbo.sysjobs j
INNER JOIN msdb.dbo.syscategories c ON j.category_id = c.category_id
INNER JOIN msdb.dbo.sysjobsteps s ON j.job_id = s.job_id
WHERE s.command LIKE '%procedure_name%'

6:業務場景:數據庫遷移時,你想把屬於這個數據庫的做業也遷移走,可是那些做業是在這些數據庫上面運行呢? 能夠經過下面SQL來查找。ip

---查看屬於某個數據庫的做業(根據做業腳本執行的數據庫斷定)

 

SELECT j.job_id                                      AS JOB_ID            
      ,j.name                                        AS JOB_NAME          
      ,CASE WHEN [enabled] =1 THEN 'Enabled'
                              ELSE 'Disabled' END    AS JOB_ENABLED       
      ,j.category_id                                 AS JOB_CATEGORY_ID
      ,c.name                                        AS JOB_CATEGORY_NAME
      ,[description]                                 AS JOB_DESCRIPTION   
      ,date_created                                  AS DATE_CREATED      
      ,date_modified                                 AS DATE_MODIFIED
FROM msdb.dbo.sysjobs j
INNER JOIN msdb.dbo.syscategories c ON j.category_id = c.category_id
WHERE   job_id IN( SELECT job_id
                  FROM    msdb.dbo.sysjobsteps
                  WHERE   database_name = 'YourSQLDba' )
 ORDER BY j.name

 

7:業務場景:須要查看那些做業類型爲「操做系統(CmdExec)"的做業。

--查看做業類型爲「操做系統(CmdExec)"的做業

SELECT j.job_id                                       AS JOB_ID            
      ,j.name                                         AS JOB_NAME          
      ,CASE WHEN [enabled] =1 THEN 'Enabled'
                              ELSE 'Disabled' END     AS JOB_ENABLED       
      ,j.category_id                                  AS JOB_CATEGORY_ID
      ,c.name                                         AS JOB_CATEGORY_NAME
      ,description                                    AS JOB_DESCRIPTION   
      ,date_created                                   AS DATE_CREATED      
      ,date_modified                                  AS DATE_MODIFIED
FROM msdb.dbo.sysjobs j
INNER JOIN msdb.dbo.syscategories c ON j.category_id = c.category_id
 WHERE EXISTS
    (SELECT 1 FROM msdb.dbo.sysjobsteps s WHERE
             j.job_id = s.job_id AND s.subsystem='CmdExec')

8: 業務場景: 須要查看今天或某個時間段內出錯的做業。

--查詢那些做業在今天出錯(若是要查詢歷史出錯做業,去掉查詢時間條件便可)

 

SELECT j.name                AS JOB_NAME        
      ,j.description         AS JOB_Description 
      ,j.date_created        AS Date_Created    
      ,j.date_modified       AS Date_Modified
FROM  msdb.dbo.sysjobs j
WHERE enabled = 1
      AND EXISTS (
        SELECT  1
        FROM    Msdb.dbo.sysjobhistory h
        WHERE   run_status = 0
            AND j.job_id = h.job_id
              AND run_date = CAST(CONVERT(VARCHAR(8), GETDATE(), 112) AS INT) )

 

---查看某個或全部做業的出錯的歷史記錄信息

SELECT j.name                AS job_name
     , h.step_id             AS step_id
     , h.step_name           AS job_step_name
     , h.sql_severity        AS sql_severity
     , h.[message]           AS error_message
     , h.run_date            AS run_date
     , h.run_time            AS run_time
     , h.run_duration        AS run_duration 
FROM msdb.dbo.sysjobhistory h
LEFT JOIN msdb.dbo.sysjobs j ON h.job_id =j.job_id 
WHERE run_status=0
--AND j.job_name =''
ORDER BY j.name, h.run_date, h.run_time, h.step_id

9: 業務場景: 查看做業的執行時間信息。

--查詢做業的運行時間,檢查做業是否正常運行或存在問題。

SELECT  j.name                        AS Job_Name        ,
        h.step_id                     AS Step_Id         ,
        h.step_name                   AS Step_Name       ,
        h.message                     AS Message         ,
        h.run_date                    AS Run_Date        ,
        h.run_time                    AS Run_Time        ,
        msdb.dbo.agent_datetime(h.run_date, h.run_time) 
                                    AS 'RunDateTime' ,
        CAST(run_duration / 10000 AS VARCHAR(2)) + N'小時'
        + CAST(( run_duration - run_duration / 10000 * 10000 ) / 100 AS VARCHAR(2)) + N'分鐘'
        + SUBSTRING(CAST(run_duration AS VARCHAR(10)),
                            LEN(CAST(run_duration AS VARCHAR(10))) - 1, 2)  + N''
        AS run_duration
FROM    msdb.dbo.sysjobhistory h
        LEFT JOIN msdb.dbo.sysjobs j ON h.job_id = j.job_id
ORDER BY Job_Name, h.Step_Id
SELECT  j.name                        AS Job_Name        ,
        h.step_id                     AS Step_Id         ,
        h.step_name                   AS Step_Name       ,
        h.message                     AS Message         ,
        h.run_date                    AS Run_Date        ,
        h.run_time                    AS Run_Time        ,
        msdb.dbo.agent_datetime(h.run_date, h.run_time) 
                                    AS 'RunDateTime' ,
        CAST(run_duration / 10000 AS VARCHAR(2)) + N'小時'
        + CAST(( run_duration - run_duration / 10000 * 10000 ) / 100 AS VARCHAR(2)) + N'分鐘'
        + SUBSTRING(CAST(run_duration AS VARCHAR(10)),
                            LEN(CAST(run_duration AS VARCHAR(10))) - 1, 2)  + N''
        AS run_duration
FROM    msdb.dbo.sysjobhistory h
        LEFT JOIN msdb.dbo.sysjobs j ON h.job_id = j.job_id
WHERE   Run_Date = CAST(CONVERT(VARCHAR(8), GETDATE(), 112) AS INT)  --今天執行的做業
    AND h.run_status = 1    --執行成功
ORDER BY h.run_duration DESC

10: 業務場景: 查詢做業運行時間超過某個閥值的全部做業。例如做業執行時間超過一分鐘的做業

查詢今天執行時間大於一分鐘的做業

SELECT  j.name                        AS Job_Name        ,
        h.step_id                     AS Step_Id         ,
        h.step_name                   AS Step_Name       ,
        h.message                     AS Message         ,
        h.run_date                    AS Run_Date        ,
        h.run_time                    AS Run_Time        ,
        msdb.dbo.agent_datetime(h.run_date, h.run_time) 
                                    AS 'RunDateTime' ,
        CAST(run_duration / 10000 AS VARCHAR(2)) + N'小時'
        + CAST(( run_duration - run_duration / 10000 * 10000 ) / 100 AS VARCHAR(2)) + N'分鐘'
        + SUBSTRING(CAST(run_duration AS VARCHAR(10)),
                            LEN(CAST(run_duration AS VARCHAR(10))) - 1, 2)  + N''
        AS run_duration
FROM    msdb.dbo.sysjobhistory h
        LEFT JOIN msdb.dbo.sysjobs j ON h.job_id = j.job_id
WHERE   Run_Date = CAST(CONVERT(VARCHAR(8), GETDATE(), 112) AS INT)  --今天執行的做業
    AND h.run_status = 1    --執行成功
    AND h.run_duration > 100 --大於一分鐘的做業
ORDER BY Job_Name, h.Step_Id

11:業務場景,查看正在執行的做業。

SELECT job.job_id,
       job.name  ,
       sa.run_requested_date,
       sa.start_execution_date
FROM   msdb.dbo.sysjobs_view job
       INNER JOIN msdb.dbo.sysjobactivity sa ON job.job_id = sa.job_id 
        -- get only the current session
       INNER JOIN msdb.dbo.syssessions s ON s.session_id = sa.session_id
       INNER JOIN ( SELECT MAX(agent_start_date) AS max_agent_start_date
                    FROM   msdb.dbo.syssessions
                  ) session_max ON s.agent_start_date = session_max.max_agent_start_date
WHERE  sa.run_requested_date IS NOT NULL
       AND sa.stop_execution_date IS NULL; 

12:業務場景,查看某個做業的詳細信息。

EXEC msdb.dbo.sp_help_job @Job_name = 'YourSQLDba_LogBackups' 
相關文章
相關標籤/搜索