SQL跟踪:如何使用SQL Server Profiler仅捕获插入,更新,删除语句?

时间:2016-09-16 09:13:08

标签: sql-server-2008 sql-server-profiler

要求:

我只需捕获跟踪中的Insert,Update和Delete语句。为此,我使用了下面的跟踪代码,但不幸的是它捕获了存储过程执行,声明了那个执行中没有DML的语句。

如何仅捕获Insert,Update和Delete语句,我需要做什么。 感谢帮助。

declare @rc int
declare @TraceID int
declare @maxfilesize bigint
set @maxfilesize = 5 

exec @rc = sp_trace_create @TraceID output, 0, N'InsertFileNameHere', @maxfilesize, NULL 
if (@rc != 0) goto error

declare @on bit
set @on = 1
exec sp_trace_setevent @TraceID, 10, 10, @on
exec sp_trace_setevent @TraceID, 10, 6, @on
exec sp_trace_setevent @TraceID, 10, 11, @on
exec sp_trace_setevent @TraceID, 10, 12, @on
exec sp_trace_setevent @TraceID, 10, 13, @on
exec sp_trace_setevent @TraceID, 10, 14, @on
exec sp_trace_setevent @TraceID, 10, 15, @on
exec sp_trace_setevent @TraceID, 10, 17, @on
exec sp_trace_setevent @TraceID, 10, 18, @on
exec sp_trace_setevent @TraceID, 12, 1, @on
exec sp_trace_setevent @TraceID, 12, 11, @on
exec sp_trace_setevent @TraceID, 12, 6, @on
exec sp_trace_setevent @TraceID, 12, 10, @on
exec sp_trace_setevent @TraceID, 12, 12, @on
exec sp_trace_setevent @TraceID, 12, 13, @on
exec sp_trace_setevent @TraceID, 12, 14, @on
exec sp_trace_setevent @TraceID, 12, 15, @on
exec sp_trace_setevent @TraceID, 12, 17, @on
exec sp_trace_setevent @TraceID, 12, 18, @on

declare @intfilter int
declare @bigintfilter bigint

exec sp_trace_setfilter @TraceID, 1, 0, 6, N'%INSERT%'
exec sp_trace_setfilter @TraceID, 1, 1, 6, N'%UPDATE%'
exec sp_trace_setfilter @TraceID, 1, 1, 6, N'%DELETE%'
exec sp_trace_setfilter @TraceID, 1, 0, 1, NULL
exec sp_trace_setfilter @TraceID, 10, 0, 7, N'SQL Server Profiler - 19bdcc9e-99de-4173-99e7-84cd5fc0add4'
exec sp_trace_setfilter @TraceID, 10, 0, 1, NULL

exec sp_trace_setstatus @TraceID, 1


select TraceID=@TraceID
goto finish

error: 
select ErrorCode=@rc

finish: 
go

1 个答案:

答案 0 :(得分:1)

请尝试以下脚本来捕获所需数据:

    declare @rc int
declare @TraceID int
declare @maxfilesize bigint
set @maxfilesize = 5 

exec @rc = sp_trace_create @TraceID output, 0, N'InsertFileNameHere', @maxfilesize, NULL 
if (@rc != 0) goto error

declare @on bit
set @on = 1

exec sp_trace_setevent @TraceID, 10, 1, @on
exec sp_trace_setevent @TraceID, 10, 10, @on
exec sp_trace_setevent @TraceID, 10, 6, @on
exec sp_trace_setevent @TraceID, 10, 11, @on
exec sp_trace_setevent @TraceID, 10, 12, @on
exec sp_trace_setevent @TraceID, 10, 13, @on
exec sp_trace_setevent @TraceID, 10, 14, @on
exec sp_trace_setevent @TraceID, 10, 15, @on
exec sp_trace_setevent @TraceID, 10, 17, @on
exec sp_trace_setevent @TraceID, 10, 18, @on
exec sp_trace_setevent @TraceID, 12, 1, @on
exec sp_trace_setevent @TraceID, 12, 11, @on
exec sp_trace_setevent @TraceID, 12, 6, @on
exec sp_trace_setevent @TraceID, 12, 10, @on
exec sp_trace_setevent @TraceID, 12, 12, @on
exec sp_trace_setevent @TraceID, 12, 13, @on
exec sp_trace_setevent @TraceID, 12, 14, @on
exec sp_trace_setevent @TraceID, 12, 15, @on
exec sp_trace_setevent @TraceID, 12, 17, @on
exec sp_trace_setevent @TraceID, 12, 18, @on
exec sp_trace_setevent @TraceID, 41, 1, @on

declare @intfilter int
declare @bigintfilter bigint

exec sp_trace_setfilter @TraceID, 1, 1, 6, N'%INSERT%'
exec sp_trace_setfilter @TraceID, 1, 1, 6, N'%UPDATE%'
exec sp_trace_setfilter @TraceID, 1, 1, 6, N'%DELETE%'
exec sp_trace_setfilter @TraceID, 1, 0, 1, NULL
exec sp_trace_setfilter @TraceID, 10, 0, 7, N'SQL Server Profiler - 19bdcc9e-99de-4173-99e7-84cd5fc0add4'
exec sp_trace_setfilter @TraceID, 10, 0, 1, NULL

exec sp_trace_setstatus @TraceID, 1


select TraceID=@TraceID
goto finish

error: 
select ErrorCode=@rc

finish: 
go