Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.79 KB | None | 0 0
  1. USE msdb
  2. GO
  3. BEGIN TRANSACTION
  4.     BEGIN TRY
  5.         DECLARE @collection_set_id_15 int
  6.         DECLARE @collection_set_uid_16 uniqueidentifier
  7.         EXEC [msdb].[dbo].[sp_syscollector_create_collection_set]   @name = N'Stored Procedure Usage - "ElamedDB" database',
  8.                                                                     @collection_mode = 0, -- cached mode
  9.                                                                     @description = N'Collects information about stored procedures use.',
  10.                                                                     @logging_level = 1,
  11.                                                                     @days_until_expiration = 30,-- set to the number of days you need to keep the history
  12.                                                                     @schedule_name = N'CollectorSchedule_Every_30min', -- collector upload schedule
  13.                                                                     @collection_set_id = @collection_set_id_15 OUTPUT,
  14.                                                                     @collection_set_uid = @collection_set_uid_16 OUTPUT
  15.  
  16.         SELECT
  17.             @collection_set_id_15,
  18.             @collection_set_uid_16
  19.  
  20.         DECLARE @collector_type_uid_19 uniqueidentifier
  21.         SELECT
  22.             @collector_type_uid_19 = collector_type_uid
  23.         FROM [msdb].[dbo].[syscollector_collector_types]
  24.         WHERE Name = N'Generic T-SQL Query Collector Type';
  25.  
  26.         DECLARE @collection_item_id_20 int
  27.         EXEC [msdb].[dbo].[sp_syscollector_create_collection_item]  @name = N'Stored Procedure Usage Statistics',
  28.                                                                     @parameters = N'<ns:TSQLQueryCollector xmlns:ns="DataCollectorType"><Query><Value>
  29. SELECT  d.object_id ,
  30.        d.database_id AS [db_id],
  31.        db_name(d.database_id) AS [db_name],
  32.        OBJECT_NAME(object_id, database_id) AS [sp_name],
  33.        d.last_execution_time,
  34.        d.execution_count
  35. FROM sys.dm_exec_procedure_stats AS d
  36. WHERE d.database_id = DB_ID(''ElamedDB'') </Value>
  37. <OutputTable>sp_usage_stats</OutputTable></Query>
  38. <Databases><Database>_DemoDB</Database></Databases></ns:TSQLQueryCollector>',
  39.                                                                     @collection_item_id = @collection_item_id_20 OUTPUT,
  40.                                                                     @frequency = 5,
  41.                                                                     @collection_set_id = @collection_set_id_15,
  42.                                                                     @collector_type_uid = @collector_type_uid_19
  43.         SELECT
  44.             @collection_item_id_20
  45.  
  46.     COMMIT TRANSACTION;
  47. END TRY
  48. BEGIN CATCH
  49.     ROLLBACK TRANSACTION;
  50.     DECLARE @ErrorMessage nvarchar(4000);
  51.     DECLARE @ErrorSeverity int;
  52.     DECLARE @ErrorState int;
  53.     DECLARE @ErrorNumber int;
  54.     DECLARE @ErrorLine int;
  55.     DECLARE @ErrorProcedure nvarchar(200);
  56.     SELECT
  57.         @ErrorLine = ERROR_LINE(),
  58.         @ErrorSeverity = ERROR_SEVERITY(),
  59.         @ErrorState = ERROR_STATE(),
  60.         @ErrorNumber = ERROR_NUMBER(),
  61.         @ErrorMessage = ERROR_MESSAGE(),
  62.         @ErrorProcedure = ISNULL(ERROR_PROCEDURE(), '-');
  63.     RAISERROR (14684, @ErrorSeverity, 1, @ErrorNumber, @ErrorSeverity,
  64.     @ErrorState, @ErrorProcedure, @ErrorLine, @ErrorMessage);
  65.  
  66. END CATCH;
  67.  
  68. GO
  69. USE msdb;
  70. GO
  71. DECLARE @collection_set int
  72.  
  73. SELECT
  74.     @collection_set = collection_set_id
  75. FROM dbo.syscollector_collection_sets
  76. WHERE Name = 'Stored Procedure Usage - "ElamedDB" database'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement