RealWorldDevelopers
9/19/2016 - 11:19 PM

Show all Jobs on a SQL Server

Show all Jobs on a SQL Server

CREATE    TABLE    #   xp_results(  
  job_id uniqueidentifier   NOT    NULL   ,  
  last_run_date   INT    NOT    NULL   ,  
  last_run_time   INT    NOT    NULL   ,  
  next_run_date   INT    NOT    NULL   ,  
  next_run_time   INT    NOT    NULL   ,  
  next_run_schedule_id   INT    NOT    NULL   ,  
  requested_to_run   INT    NOT    NULL   ,   -- bool  
  request_source   INT    NOT    NULL   ,  
  request_source_id sysname   COLLATE   database_default   NULL   ,  
  running   INT    NOT    NULL   ,   -- bool  
  current_step   INT    NOT    NULL   ,  
  current_retry_attempt   INT    NOT    NULL   ,  
  job_state   INT    NOT    NULL   )  

 INSERT    #   xp_results   EXEC   master.dbo.xp_sqlagent_enum_jobs @is_sysadmin   =   1, @job_owner   =    ''  

 SELECT       CASE    WHEN   b.[enabled]   =   0   THEN    'N'    ELSE    'Y'   End   AS   job_enabled,  
   b.   NAME   ,  
   b.[description],  
      CASE    WHEN   a.running   =   0   THEN    'N'    ELSE    'Y'   End   AS   job_running,  
   a.current_step,  
      CASE   a.job_state  
       WHEN   0   THEN    'Not idle or suspended'  
       WHEN   1   THEN    'Executing'  
       WHEN   2   THEN    'Waiting'  
       WHEN   3   THEN    'Between Retries'  
       WHEN   4   THEN    'Idle'  
       WHEN   5   THEN    'Suspended'  
       WHEN   6   THEN    'WaitingForStepToFinish'  
       WHEN   7   THEN    'PerformingCompletionActions'  
   End   AS   job_state,  
   a.last_run_date,  
   a.last_run_time,  
   a.next_run_date,  
   a.next_run_time,  
   a.job_id  
 FROM    #   xp_results a   LEFT    OUTER    JOIN  
  msdb.dbo.sysjobs b   ON   a.job_id   =   b.job_id  
 --where running = 1  

 DROP    TABLE    #   xp_results  


 --job states  
 --0 = Not idle or suspended,  
 --1 = Executing,  
 --2 = Waiting For Thread,  
 --3 = Between Retries,  
 --4 = Idle,  
 --5 = Suspended,  
 --6 = WaitingForStepToFinish,  
 --7 = PerformingCompletionActions