【问题标题】:How to Efficiently Document SQL Server Agent Jobs如何有效地记录 SQL Server 代理作业
【发布时间】:2018-02-15 11:14:01
【问题描述】:

我的团队几乎是全新的,我们继承了几个自动化工作,我们几乎没有或没有文档(工作描述中也没有)。我们有数百个在 SQL Server 代理和各种服务器上运行的自动化作业,所以我想开始记录一些基本信息(这些作业什么时候安排?,它们通常需要多长时间?,它们多久失败一次?,是执行时间的长度是静态的还是增长的?如果是SSIS包,它在哪里,它有什么作用?,如果是报告,是否可以迁移到SSRS调度程序?)。

我们在该地区有一所大学询问我们是否可以聘用任何实习生。尽管我们有大量工作,但我们无法将敏感数据的访问权限授予非员工。我正在考虑将数据库恢复到另一台服务器并截断敏感数据表,从而使系统数据库保持完整,以便记录计划任务。我不确定这是一个好主意,因为我最终可能会花费与完成实际任务所需的时间一样多的时间来“保护”数据。有没有人对这类问题有任何创造性的解决方案?

可能是这样的:

http://bradsruminations.blogspot.com/2011/04/documenting-your-sql-agent-jobs.html http://www.elsasoft.org/forum/topic.asp?TOPIC_ID=164

我想听听其他已经克服这些挑战的人的反馈。感谢您的帮助!!!

【问题讨论】:

    标签: sql-server scheduled-tasks database-administration sql-agent-job


    【解决方案1】:

    我在以下博客的帮助下想出了记录工作信息的脚本。我做了一些修改,我正在应用过滤器来获取有关特定工作的详细信息。

    https://mikesdatawork.wordpress.com/2015/06/30/convert-last_run_date-and-last_run_time-in-sysjobsteps/#comment-2720

    http://bradsruminations.blogspot.com/2011/04/documenting-your-sql-agent-jobs.html

    DECLARE @job_name sysname = 'YourJobNameHere'
    SELECT 'Job Detailed Information' AS TypeofData
    ;WITH job_details AS
    (
    SELECT j.name
    , j.job_id
    , jb.step_id
    , jb.step_name
    ,jb.subsystem
    , jb.command
    ,jb.additional_parameters 
    --, 'last run literal' = dateadd(millisecond, jb.last_run_time,convert(datetime,cast(nullif(jb.last_run_date,0) as nvarchar(10))))
    , 'last run day' = datename(dw, dateadd(millisecond, jb.last_run_time,convert(datetime,cast(nullif(jb.last_run_date,0) as nvarchar(10)))))
    , 'last run date' = convert(char, dateadd(millisecond, jb.last_run_time,convert(datetime,cast(nullif(jb.last_run_date,0) as nvarchar(10)))), 9)
    FROM dbo.sysjobs as j 
    INNER JOIN dbo.sysjobsteps as jb
    ON j.job_id = jb.job_id
    WHERE j.name = @job_name
    )
    SELECT * FROM job_details order by step_id ;
    SELECT 'Schedule Detailed Information' AS TypeofData;
    WITH job_schedules AS
    (
    select schedule_id
          ,sysschedules.name as schedule_name
          ,SchedDesc=TimeOfDay+Frequency+EffDtRange   
    from dbo.sysschedules 
    cross apply 
      --Translate the dates and times into DATETIME values
      --And translate the times into HH:MM:SSam (or HH:MMam) strings
      (select StDate=convert(datetime
                            ,convert(varchar(8),active_start_date))
             ,EnDate=convert(datetime
                            ,convert(varchar(8),active_end_date))
             ,StTime=convert(datetime
                            ,stuff(stuff(right(1000000+active_start_time
                                              ,6)
                                        ,3,0,N':')
                                  ,6,0,N':'))
             ,EnTime=convert(datetime
                            ,stuff(stuff(right(1000000+active_end_time
                                              ,6)
                                        ,3,0,N':')
                                  ,6,0,N':'))
      ) F_DtTm
    cross apply
      --Translate the times into appropriate HH:MM:SSam or HH:MMam char formats
      (select replace(replace(replace(substring(lower(convert(varchar(30),StTime,109))
                                               ,13,14)
                                     ,N':000',N'')
                             ,N':00a',N'a')
                     ,N':00p',N'p')
             ,replace(replace(replace(substring(lower(convert(varchar(30),EnTime,109))
                                               ,13,14)
                                     ,N':000',N'')
                             ,N':00a',N'a')
                     ,N':00p',N'p')
      ) F_Tms(StTimeString,EnTimeString)
    cross apply 
      --What Time of Day? Single Time or Range of Times/Intervals
      (select case 
                when freq_subday_type=0
                then N''
                else case 
                       when freq_subday_type=1
                       then N'At '
                       else N'Every '
                           +convert(nvarchar(10),freq_subday_interval)
                           +' '
                           +case freq_subday_type
                              when 2 then N'Second'
                              when 4 then N'Minute'
                              when 8 then N'Hour'
                            end
                           +case 
                              when freq_subday_interval=1 then N'' else N's' end
                           +N' From '
                     end
                    +StTimeString
                    +case
                       when freq_subday_type=1
                       then N''
                       else N' to '+EnTimeString
                     end
                    +N' '
              end
      ) F_Tm(TimeOfDay)
    cross apply
      --Translate Frequency  
      (select case freq_type
                when 1
                then N'One Time Only'
                when 4
                then N'Every '
                    +case freq_interval 
                       when 1
                       then N'Day'
                       else convert(nvarchar(10),freq_interval)+N' Days'
                     end
                when 8
                then N'Every '
                    +case freq_recurrence_factor
                       when 1
                       then N''
                       else convert(nvarchar(10),freq_recurrence_factor)+N' Weeks on '
                     end
                    +stuff(case when freq_interval& 1<>0 then N', Sunday' else N'' end
                          +case when freq_interval& 2<>0 then N', Monday' else N'' end
                          +case when freq_interval& 4<>0 then N', Tuesday' else N'' end
                          +case when freq_interval& 8<>0 then N', Wednesday' else N'' end
                          +case when freq_interval&16<>0 then N', Thursday' else N'' end
                          +case when freq_interval&32<>0 then N', Friday' else N'' end
                          +case when freq_interval&64<>0 then N', Saturday' else N'' end
                          ,1,2,N'')
                when 16
                then N'Every '
                    +case freq_recurrence_factor 
                       when 1
                       then N'Month '
                       else convert(nvarchar(10),freq_recurrence_factor)+N' Months '
                     end
                    +N'on the '
                    +convert(nvarchar(10),freq_interval)
                    +case 
                       when freq_interval in (1,21,31)
                       then N'st'
                       when freq_interval in (2,22)
                       then N'nd'
                       when freq_interval in (3,23)
                       then N'rd'
                       else N'th'
                     end
                    +N' of the Month'
                when 32
                then N'Every '
                    +case freq_recurrence_factor 
                       when 1
                       then N'Month '
                       else convert(nvarchar(10),freq_recurrence_factor)+N' Months '
                     end
                    +N'on the '
                    +case freq_relative_interval 
                       when  1 then N'1st '
                       when  2 then N'2nd '
                       when  4 then N'3rd '
                       when  8 then N'4th '
                       when 16 then N'Last '
                     end
                    +case freq_interval 
                       when  1 then N'Sunday'
                       when  2 then N'Monday'
                       when  3 then N'Tuesday'
                       when  4 then N'Wednesday'
                       when  5 then N'Thursday'
                       when  6 then N'Friday'
                       when  7 then N'Saturday'
                       when  8 then N'Day'
                       when  9 then N'Weekday'
                       when 10 then N'Weekend Day'
                     end
                    +N' of the Month'
                when 64
                then N'When SQL Server Agent Starts'
                when 128
                then N'Whenever the CPUs become Idle'
                else N'Unknown'
              end
      ) F_Frq(Frequency)
    cross apply
      --When is it effective?
      (select N' (Effective '+convert(nvarchar(11),StDate,100)
             +case  
                when EnDate='99991231'
                then N''
                else N' thru '+convert(nvarchar(11),EnDate,100)
              end
             +N')'           
      ) F_Eff(EffDtRange)
    WHERE schedule_id IN (SELECT s.schedule_id 
    FROM dbo.sysjobschedules as s
    INNER JOIN dbo.sysjobs as j 
    on j.job_id = s.job_id 
    WHERE j.name = @job_name)
    )
    SELECT * FROM job_schedules;
    

    【讨论】:

      【解决方案2】:

      所有这些信息都由 MSSQL 存储在表中,您可以查询这些系统表以检索信息以下适用于 SQL 2012 及更高版本,您可以使用下面的列表查询作业、计划等。现在 MSDN 将在这里做你的朋友。

      • dbo.sysalerts:每个警报包含一行。
      • dbo.syscategories: 包含 SQL Server 使用的类别 Management Studio 用于组织作业、警报和操作员。
      • dbo.sysdownloadlist: 保存所有下载指令的队列 目标服务器。
      • dbo.sysjobactivity: 包含有关当前 SQL Server 的信息 代理工作活动和状态。
      • dbo.sysjobhistory: 包含有关执行的信息 SQL Server 代理的计划作业。
      • dbo.sysjobs: 存储每个计划作业的信息 由 SQL Server 代理执行。
      • dbo.sysjobschedules: 包含要执行的作业的计划信息 由 SQL Server 代理执行
      • dbo.sysjobservers: 存储一个关联或关系 具有一台或多台目标服务器的特定作业。
      • dbo.sysjobsteps: 包含要执行的作业中每个步骤的信息 由 SQL Server 代理执行。
      • dbo.sysjobstepslogs:包含有关作业步骤日志的信息。
      • dbo.sysnotifications:每个通知包含一行。
      • dbo.sysoperators:每个 SQL Server 代理运算符包含一行。
      • dbo.sysproxyes: 包含有关 SQL Server 代理代理的信息 帐户。
      • dbo.sysproxylogin: 记录与哪些 SQL Server 登录相关联 每个 SQL Server 代理代理帐户。
      • dbo.sysproxysubsystem: 记录 SQL Server 代理子系统是哪个 由每个代理帐户使用。
      • dbo.sysschedules: 包含有关 SQL Server 代理作业的信息 时间表。
      • dbo.syssessions: 包含每个 SQL 的 SQL Server 代理开始日期 服务器代理会话。每次 SQL Server 都会创建一个会话 代理服务启动。
      • dbo.syssubsystems: 包含有关所有可用 SQL Server 的信息 代理代理子系统。
      • dbo.systargetservergroupmembers: 记录哪些目标服务器是 目前已加入此多服务器组。
      • dbo.systargetservergroups: 记录哪些目标服务器组是 目前已加入此多服务器环境。
      • dbo.systargetservers: 记录当前有哪些目标服务器 已加入此多服务器操作域。
      • dbo.systaskids: 包含之前创建的任务的映射 当前版本的 SQL Server 到 Management Studio 作业 版本。

      大警告:如果该作业运行 SQL 脚本,这不会告诉您该作业会做什么。您将需要查看作业解密脚本,然后稍后添加描述。这就是为什么你一开始就勤奋并注重细节的原因。

      【讨论】:

        【解决方案3】:

        我自己从未使用过这个工具,但我听说过一些关于 Dataedo 的好消息。 https://dataedo.com/

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-09-19
          • 1970-01-01
          • 2010-12-07
          • 2017-11-11
          • 1970-01-01
          相关资源
          最近更新 更多