如何用SQL代理进行SQL探查器跟踪
来源:优易学  2011-4-15 16:54:50   【优易学:中国教育考试门户网】   资料下载   IT书店

  SQL 探查器跟踪使用系统存储过程来建立。你可以从一个现有的探查器跟踪中使用SQL命令,并构造你自己的存储过程来创建和启动一个SQL探查器跟踪。你需要使用自己的存储过程来指定一些额外的设置。这些设置包括运行时间、文件大小和跟踪输出文件位置。下面列出了关于如何使用这个存储过程来创建、执行并且关闭探查器跟踪的详细过程。
  创建跟踪定义
  定义用于构造一个探查器跟踪的SQL命令最有效的方法是使用SQL探查器。
  1. 启动SQL探查器并选择File > New Trace。指定你在跟踪中想要的事件、字段和过滤器。
  2. 启动跟踪然后停止它。
  3. 输出定义。点击File > Export > Script Trace Definition > For SQL Server 2005。
  注意,对于SQL Sever 2000 和 2008,请选择适当的输出类型。
  4. 保存跟踪文件。
  创建一个探查器跟踪存储过程
  接着,选择这些输出跟踪定义并且用它来创建一个存储过程。
  1. 使用SSMS来打开上面创建的输出跟踪定义。
  2. 在SSMS中打开另一个查询窗口并粘贴下面的trc_template存储过程代码。
  /*
  use Admin
  go
  */
  CREATE procedure trc_Template @Folder nvarchar(200)
  as
  /*
  Start a 60 minute profiler trace storing the captured output in
  provider folder.
  The folder must exist. A subfolder will be created using the start date
  and time to allow for repeated running of this profile without
  replacing the
  previuos captured trace files.
  On SQL Server 2005, XP_CMDSHELL needs to be enable to create the
  subfolder. You
  might want to disable it when you are done running your scheduled
  trace.
  Sample Command: exec trc_Template @Folder =
  'C:\Output\ProfilerTrace\Template'
  */
  set nocount on
  -- To change the traces duration, modify the following statement
  declare @StopTime datetime ; set @StopTime = dateadd(mi,60,getdate())
  declare @StartDatetime varchar(13) ; set @StartDatetime =
  convert(char(8),getdate(),112) + '_' +
  cast(replace(convert(varchar(5),getdate(),108),':','') as char(4)) --['YYYYMMDD_HHMM']
  declare @rc int
  declare @TraceID int
  declare @TraceFile nvarchar(100)
  declare @MaxFileSize bigint ; set @MaxFileSize = 50 -- The maximum trace file in megabytes
  declare @cmd nvarchar(2000)
  declare @msg nvarchar(200)
  If right(@Folder,1)<>'\' set @Folder = @Folder + '\'
  -- Check if Folder exists
  set @cmd = 'dir ' +@Folder
  exec @rc = master..xp_cmdshell @cmd,no_output
  if (@rc != 0) begin set @msg = 'The specified folder ' + @Folder + '
  does not exist, Please specify an existing drive:\folder '+ cast(@rc as
  varchar(10)) raiserror(@msg,10,1) return(-1)
  end
  --Create new trace file folder
  set @cmd = 'mkdir ' +@Folder+@StartDatetime
  exec @rc = master..xp_cmdshell @cmd,no_output
  if (@rc != 0) begin set @msg = 'Error creating trace folder : ' +
  cast(@rc as varchar(10)) set @msg = @msg + 'SQL Server 2005 or later
  instance require OLE Automation to been enabled' raiserror(@msg,10,1)
  return(-1)
  end
  set @TraceFile = @Folder+@StartDatetime+'\trace'
  exec @rc = sp_trace_create @TraceID output, 2, @TraceFile,
  @MaxFileSize, @StopTime
  if (@rc != 0) begin set @msg = 'Error creating trace : ' + cast(@rc as
  varchar(10)) raiserror(@msg,10,1) return(-1)
  end

[1] [2] 下一页

责任编辑:小草

文章搜索:
 相关文章
热点资讯
热门课程培训