1

这是一个生产 SQL Server 2000 数据库,包含大约 1500 个数据库对象(SP、视图、UDF)。现在,有人告诉我,这些数据库对象中约有 20% 未被应用程序使用(其中 200 个大多是小型构建在多个平台/技术上的),我的任务是识别它们。是否有可以针对数据库运行的脚本来识别这些数据库对象的“上次运行日期/时间”?

有没有其他方法可以识别未使用的数据库对象——除了浏览代码之外?请注意,这是 SQL Server 2000。非常感谢任何帮助。

谢谢

4

1 回答 1

-1

我不得不为我的 SQL2000 服务器编写一些代码。

我将这些插入到表中以进行持续监控

第一段代码将告诉您作业计划运行的时间

SET @SQL = 'SELECT 
         J.[name] AS [JobName]
        , CASE S.[enabled]
            WHEN 1 THEN ''Yes''
            WHEN 0 THEN ''No''
          END AS [IsEnabled]
        , CASE 
            WHEN [freq_type] = 64 THEN ''Start automatically when SQL Server Agent starts''
            WHEN [freq_type] = 128 THEN ''Start whenever the CPUs become idle''
            WHEN [freq_type] IN (4,8,16,32) THEN ''Recurring''
            WHEN [freq_type] = 1 THEN ''One Time''
          END [ScheduleType]
        , CASE [freq_type]
            WHEN 1 THEN ''One Time''
            WHEN 4 THEN ''Daily''
            WHEN 8 THEN ''Weekly''
            WHEN 16 THEN ''Monthly''
            WHEN 32 THEN ''Monthly - Relative to Frequency Interval''
            WHEN 64 THEN ''Start automatically when SQL Server Agent starts''
            WHEN 128 THEN ''Start whenever the CPUs become idle''
          END [Occurrence]
        , CASE [freq_type]
            WHEN 4 THEN ''Occurs every '' + CAST([freq_interval] AS VARCHAR(3)) + '' day(s)''
            WHEN 8 THEN ''Occurs every '' + CAST([freq_recurrence_factor] AS VARCHAR(3)) 
                        + '' week(s) on ''
                        + CASE WHEN [freq_interval] & 1 = 1 THEN ''Sunday'' ELSE '''' END
                        + CASE WHEN [freq_interval] & 2 = 2 THEN '', Monday'' ELSE '''' END
                        + CASE WHEN [freq_interval] & 4 = 4 THEN '', Tuesday'' ELSE '''' END
                        + CASE WHEN [freq_interval] & 8 = 8 THEN '', Wednesday'' ELSE '''' END
                        + CASE WHEN [freq_interval] & 16 = 16 THEN '', Thursday'' ELSE '''' END
                        + CASE WHEN [freq_interval] & 32 = 32 THEN '', Friday'' ELSE '''' END
                        + CASE WHEN [freq_interval] & 64 = 64 THEN '', Saturday'' ELSE '''' END
            WHEN 16 THEN ''Occurs on Day '' + CAST([freq_interval] AS VARCHAR(3)) 
                         + '' of every ''
                         + CAST([freq_recurrence_factor] AS VARCHAR(3)) + '' month(s)''
            WHEN 32 THEN ''Occurs on ''
                         + CASE [freq_relative_interval]
                            WHEN 1 THEN ''First''
                            WHEN 2 THEN ''Second''
                            WHEN 4 THEN ''Third''
                            WHEN 8 THEN ''Fourth''
                            WHEN 16 THEN ''Last''
                           END
                         + '' '' 
                         + CASE [freq_interval]
                            WHEN 1 THEN ''Sunday''
                            WHEN 2 THEN ''Monday''
                            WHEN 3 THEN ''Tuesday''
                            WHEN 4 THEN ''Wednesday''
                            WHEN 5 THEN ''Thursday''
                            WHEN 6 THEN ''Friday''
                            WHEN 7 THEN ''Saturday''
                            WHEN 8 THEN ''Day''
                            WHEN 9 THEN ''Weekday''
                            WHEN 10 THEN ''Weekend day''
                           END
                         + '' of every '' + CAST([freq_recurrence_factor] AS VARCHAR(3)) 
                         + '' month(s)''
          END AS [Recurrence]
        , CASE [freq_subday_type]
            WHEN 1 THEN ''Occurs once at '' 
                        + STUFF(
                     STUFF(RIGHT(''000000'' + CAST([active_start_time] AS VARCHAR(6)), 6)
                                    , 3, 0, '':'')
                                , 6, 0, '':'')
            WHEN 4 THEN ''Occurs every '' 
                        + CAST([freq_subday_interval] AS VARCHAR(3)) + '' Minute(s) between '' 
                        + 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 ''Occurs every '' 
                        + CAST([freq_subday_interval] AS VARCHAR(3)) + '' Hour(s) between '' 
                        + 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],



    FROM '+ @Server +'.[msdb].[dbo].[sysjobschedules] AS S INNER JOIN 
        '+ @Server +'.[msdb].[dbo].[sysjobs] AS J ON S.[job_id] = J.[job_id]
    WHERE S.[enabled] = 1
    ORDER BY J.[Name]'

    EXEC (@SQL)

下一段代码将告诉您上次运行的作业以及是否成功

SET @SQL = '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'' 
      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 
   '+ @Server +'.[msdb].[dbo].[sysjobs] AS [sJOB]
    LEFT JOIN (
                SELECT
                    [job_id]
                    , MAX([next_run_date]) AS [NextRunDate]
                    , MAX([next_run_time]) AS [NextRunTime]
                FROM '+@Server +'.[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]


                FROM '+ @Server +'.[msdb].[dbo].[sysjobhistory]
                WHERE [step_id] = 0
            ) AS [sJOBH]
        ON [sJOB].[job_id] = [sJOBH].[job_id]
       -- AND [sJOBH].[RowNumber] = 1
ORDER BY [JobName]'

--PRINT (@SQL)

EXEC (@SQL)

将 @Server 替换为您的服务器名称或将变量 @Server 设置为您要对其运行脚本的服务器的名称

于 2013-09-23T14:32:24.400 回答