前言:SQLDiag是SQL Server内建的诊断收集共享程序,
[SQL Server 2005]
默认位置:C:\Program ">Files\Microsoft SQL Server\90\Tools\Binn\SQLDiag.exe
你可以在任何路径下执行SQLDiag因为此路径已被加到path系统变量,执行sqldiag -?或sqldiag /?即可显示参数说明
SQL 2005的sqldiag搭配参数请使用正斜线"/"
SQLdiag
可以收集下列类型的诊断信息:
Windows 效能记录
Windows 事件记录文件
SQL Server Profiler 追踪
SQL Server 封锁信息
SQL Server 组态信息
http://technet.microsoft.com/zh-tw/library/ms162833.aspx
一般使用:
例如:
sqldiag /O C:\temp\sqldiag
(/O 指定输出档案的目录)
最下方会出现
2008/07/01 00:34:42.82 SQLDIAG
Collection started. Press Ctrl+C to stop.表示正在收集中
在此时按下Ctrl+C即可停止收集 收集的信息会储存在指定的C:\temp\sqldiag\目录下,其中数个log_xx.trc档是从C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\LOG\路径下所复制的
进阶收集: 修改并指定xml檔,来收集Performance Counter的信息
[SQL Server 2000]
默认位置:C:\Program Files\Microsoft SQL Server\MSSQL\Binn\SQLDiag.exe
切换目录到此位置后执行sqldiag -?或sqldiag /?即可显示参数说明
SQL 2000的sqldiag搭配参数执行要使用"-"
例如: sqldiag -E -O c:\temp\sqldiag.log
(-O 指定输出信息到指定的档案里)
若要让SQL 2000可以取得SQL Trace(*.trc)资料,可以利用下面[Sample Code]或KB281671文件范例SQL在master数据库新增一个名为trace_blackbox的Stored Procedure
KB281671 INF: 预存程序来建立一个 SQL Server 2000 的 blackbox 追踪
然后在排定时间执行trace_blackbox 1以启动trace_blackbox,执行期间这个Instance所有联机的SQL Command都会记录到C:\Program Files\Microsoft SQL Server\MSSQL\Data\blackbox开头.trc,在必要的时间执行trace_blackbox 0以关闭trace_blackbox,而这些trc档案都将会被sqldiag所复制并更名到指定的位置
[Caution]
若执行SQLDiag当时trace_blackbox还开着,档案将无法复制所以SQLDiag.trc会是空的
[Samlp Code]
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[trace_blackbox]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[trace_blackbox]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE trace_blackbox @on int = 2 AS
/* If no argument is passed to the @on parameter then get the current blackbox trace status.
If @on is zero then stop and delete the blackbox trace.
If @on is one then create and start the blackbox trace.
*/
declare @traceid int, @blackboxstatus int, @dir nvarchar(80)
set @traceid = 0
set @blackboxstatus = 0
set nocount on
SELECT @traceid = traceid FROM :: fn_trace_getinfo(0)
where property = 1 and value = 8
IF @on = 0 and @traceid > 0
begin
select @blackboxstatus = cast(value as int) FROM :: fn_trace_getinfo(0)
where traceid = @traceid and property = 5
IF @blackboxstatus > 0 exec sp_trace_setstatus @traceid,0 --stop blackbox trace
exec sp_trace_setstatus @traceid,2 --delete blackbox trace definition
end
IF @on = 1
begin
IF @traceid < traceid =" 0" blackboxstatus =" 0" traceid =" traceid" property =" 1" value =" 8" blackboxstatus =" cast(value" traceid =" @traceid" property =" 5"> 0 and @blackboxstatus > 0
begin
select @dir = cast(value as nvarchar(80)) FROM :: fn_trace_getinfo(0)
where traceid = @traceid and property = 2
select 'The blackbox trace is running and the trace file is in the following directory.'
select @dir + '.trc'
end
ELSE select 'The blackbox trace is not running.'
set nocount off
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON