SQL Server 에이전트에 등록된 작업 목록 조회

DataBase/MS-SQL|2020. 8. 5. 17:10
반응형

SQL Server 에이전트에 등록된 작업 목록을 조회 하는 SQL 입니다.

SELECT
	A.name,
	case description when '설명이 없습니다.' then '' else description end as description,   
  CASE convert(
    nvarchar(3), 
    [freq_type], 
    3
  ) WHEN 4 THEN N' 매일' WHEN 8 THEN N' 매주 ' + CASE WHEN [freq_interval] & 1 = 1 THEN N'일' ELSE '' END 
	+ CASE WHEN [freq_interval] & 2 = 2 THEN ' 월' ELSE '' END + CASE WHEN [freq_interval] & 4 = 4 THEN ' 화' ELSE '' END + CASE WHEN [freq_interval] & 8 = 8 THEN ' 수' ELSE '' END + CASE WHEN [freq_interval] & 16 = 16 THEN N' 목' ELSE '' END + CASE WHEN [freq_interval] & 32 = 32 THEN N' 금' ELSE '' END + CASE WHEN [freq_interval] & 64 = 64 THEN N' 토' ELSE '' END WHEN 16 THEN N'하루 한번 ' 
	+ CAST(
    [freq_interval] AS VARCHAR(3)
  ) + N' 매달 ' + CAST(
    [freq_recurrence_factor] AS VARCHAR(3)
  ) + N' 매달' WHEN 32 THEN N'발생 ' + CASE [freq_relative_interval] WHEN 1 THEN N'첫째' WHEN 2 THEN N'둘째' WHEN 4 THEN N'셋째' WHEN 8 THEN N'넷째' WHEN 16 THEN N'마지막' END + ' ' + CASE [freq_interval] WHEN 1 THEN N'일' WHEN 2 THEN N'월' WHEN 3 THEN N'화' WHEN 4 THEN N'수' WHEN 5 THEN N'목' WHEN 6 THEN N'금' WHEN 7 THEN N'토' WHEN 8 THEN N'일' WHEN 9 THEN N'평일' WHEN 10 THEN N'주말' END + N' 모든 ' + CAST(
    [freq_recurrence_factor] AS VARCHAR(3)
  ) + N' 매달' END AS [Recurrence], 
  CASE convert(
    nvarchar(3), 
    [freq_subday_type], 
    3
  ) WHEN 1 THEN  STUFF(
    STUFF(
      RIGHT(
        '000000' + CAST(
          [active_start_time] AS VARCHAR(6)
        ), 
        6
      ), 
      3, 
      0, 
      ':'
    ), 
    6, 
    0, 
    ':'
  ) WHEN 2 THEN N'주기적 발생 ' + CAST(
    [freq_subday_interval] AS VARCHAR(3)
  ) + N' 초(s) 간격 ' + STUFF(
    STUFF(
      RIGHT(
        '000000' + CAST(
          [active_start_time] AS VARCHAR(6)
        ), 
        6
      ), 
      3, 
      0, 
      ':'
    ), 
    6, 
    0, 
    ':'
  ) + ' & ' + STUFF(
    STUFF(
      RIGHT(
        '000000' + CAST(
          [active_end_time] AS VARCHAR(6)
        ), 
        6
      ), 
      3, 
      0, 
      ':'
    ), 
    6, 
    0, 
    ':'
  ) WHEN 4 THEN N'주기적 발생 ' + CAST(
    [freq_subday_interval] AS VARCHAR(3)
  ) + N' 분(s) 간격 ' + STUFF(
    STUFF(
      RIGHT(
        '000000' + CAST(
          [active_start_time] AS VARCHAR(6)
        ), 
        6
      ), 
      3, 
      0, 
      ':'
    ), 
    6, 
    0, 
    ':'
  ) + ' & ' + STUFF(
    STUFF(
      RIGHT(
        '000000' + CAST(
          [active_end_time] AS VARCHAR(6)
        ), 
        6
      ), 
      3, 
      0, 
      ':'
    ), 
    6, 
    0, 
    ':'
  ) WHEN 8 THEN N'주기적 발생 ' + CAST(
    [freq_subday_interval] AS VARCHAR(3)
  ) + N' 시간(s) 간격 ' + STUFF(
    STUFF(
      RIGHT(
        '000000' + CAST(
          [active_start_time] AS VARCHAR(6)
        ), 
        6
      ), 
      3, 
      0, 
      ':'
    ), 
    6, 
    0, 
    ':'
  ) + ' & ' + STUFF(
    STUFF(
      RIGHT(
        '000000' + CAST(
          [active_end_time] AS VARCHAR(6)
        ), 
        6
      ), 
      3, 
      0, 
      ':'
    ), 
    6, 
    0, 
    ':'
  ) END [Frequency],  D.step_id, D.step_name, D.command 
FROM msdb.dbo.sysjobs A 
	INNER JOIN msdb.dbo.sysjobschedules B ON A.job_id = B.job_id 
	INNER JOIN msdb.dbo.sysschedules C ON B.schedule_id = C.schedule_id 
	INNER JOIN msdb.dbo.sysjobsteps D ON A.job_id = D.job_id  
ORDER BY A.name

 

실행 결과는 대략 아래와 같이 조회가 됩니다.

 

반응형

댓글()

백업 혹은 복구중인 데이터베이스 진행률 확인

DataBase/MS-SQL|2020. 8. 5. 10:03
반응형

백업 혹은 복구 중인 데이터 베이스의 진행 상태나 진행률을 확인하는 SQL 입니다.

 

경과된 시간 , 남은 시간 , 예상 소요 시간등을 확인 할 수 있습니다.

 

SELECT r.session_id,r.command
,cast(CONVERT(NUMERIC(6,2),r.percent_complete)  as varchar) + ' %' AS [Percent Complete]
,CONVERT(VARCHAR(20),DATEADD(ms,r.estimated_completion_time,GetDate()),20) AS [ETA Completion Time]
,CONVERT(NUMERIC(10,2),r.total_elapsed_time/1000.0/60.0) AS [Elapsed Min]
,CONVERT(NUMERIC(10,2),r.estimated_completion_time/1000.0/60.0) AS [ETA Min]
,CONVERT(NUMERIC(10,2),r.estimated_completion_time/1000.0/60.0/60.0) AS [ETA Hours]
,CONVERT(VARCHAR(1000),(SELECT text FROM sys.dm_exec_sql_text(sql_handle))) AS [SQL] 
FROM sys.dm_exec_requests r WHERE command IN ('RESTORE DATABASE','BACKUP DATABASE')

 

반응형

댓글()