将收集项添加到收集组中 (Transact-SQL)

可以使用随数据收集器一起提供的存储过程将收集项添加到现有收集组中。

使用 SQL Server Management Studio 中的查询编辑器执行以下步骤。

将收集项添加到收集组中

  1. 通过运行 sp_syscollector_stop_collection_set 存储过程停止要向其中添加收集项的收集组。 例如,若要停止名为“Test Collection Set”的收集组,请运行下列语句:

    USE msdb
    DECLARE @csid int
    SELECT @csid = collection_set_id
    FROM syscollector_collection_sets
    WHERE name = 'Test Collection Set'
    SELECT @csid
    EXEC dbo.sp_syscollector_stop_collection_set @collection_set_id = @csid
    
    注意注意

    还可以通过使用 SQL Server Management Studio 中的对象资源管理器来停止收集组。 有关详细信息,请参阅启动或停止收集组

  2. 声明要向其中添加收集项的收集组。 下面的代码提供了一个演示如何声明收集组 ID 的示例。

    DECLARE @collection_set_id_1 int
    SELECT @collection_set_id_1 = collection_set_id FROM [msdb].[dbo].[syscollector_collection_sets]
    WHERE name = N'Test Collection Set'; -- name of collection set
    
  3. 声明收集器类型。 下面的代码提供了一个演示如何声明一般 T-SQL 查询收集器类型的示例。

    DECLARE @collector_type_uid_1 uniqueidentifier
    SELECT @collector_type_uid_1 = collector_type_uid FROM [msdb].[dbo].[syscollector_collector_types] 
       WHERE name = N'Generic T-SQL Query Collector Type';
    

    可以运行下面的代码以获取已安装收集器类型的列表:

    USE msdb
    SELECT * from syscollector_collector_types
    GO
    
  4. 运行 sp_syscollector_create_collection_item 存储过程来创建收集项。 必须声明收集项的架构,以便它映射到所需收集器类型要求的架构。 下面的示例使用一般 T-SQL 查询输入架构。

    DECLARE @collection_item_id int;
    EXEC [msdb].[dbo].[sp_syscollector_create_collection_item] 
    @name=N'OS Wait Stats', --name of collection item
    @parameters=N'
    <ns:TSQLQueryCollector xmlns:ns="DataCollectorType">
     <Query>
      <Value>select * from sys.dm_os_wait_stats</Value>
      <OutputTable>os_wait_stats</OutputTable>
    </Query>
    </ns:TSQLQueryCollector>',
    @collection_item_id = @collection_item_id OUTPUT,
    @frequency = 60,
    @collection_set_id = @collection_set_id_1, --- Provides the collection set ID number
    @collector_type_uid = @collector_type_uid_1 -- Provides the collector type UID
    SELECT @collection_item_id   
    
  5. 在启动已更新的收集组之前,运行以下查询来验证新的收集项是否已创建:

    USE msdb
    SELECT * from syscollector_collection_sets
    SELECT * from syscollector_collection_items
    GO
    

    收集组和它们的收集项都显示在**“结果”**选项卡中。

请参阅

任务

创建使用一般 T-SQL 查询收集器类型的自定义收集组 (Transact-SQL)

参考

数据收集器存储过程 (Transact-SQL)