创建对某个表的操作情况的跟踪
--================================================================================================ --创建对某个表的操作情况的跟踪 --注意修改路径 和 @value --================================================================================================ -- Create a Queue declare @rootPath NVARCHAR(200) = 'D:\inout\TraceLog\' declare @path nvarchar(500) declare @rc int declare @TraceID int declare @maxfilesize bigint set @maxfilesize = 20 declare @value nvarchar(4000) --表名 set @value = N'%CommodityBaseRecord%' SET @path = @rootPath --+ CONVERT(VARCHAR(6), GETDATE(), 112) + '\' --年月的目录 + REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR, GETDATE(), 120), '-',''),' ',''),':',''); --select @path --D:\InOut\TraceLog\201709\20170927110926 -- Please replace the text InsertFileNameHere, with an appropriate -- filename prefixed by a path, e.g., c:\MyFolder\MyTrace. The .trc extension -- will be appended to the filename automatically. If you are writing from -- remote server to local drive, please use UNC path and make sure server has -- write access to your network share exec @rc = sp_trace_create @TraceID output, 0, @path, @maxfilesize, NULL if (@rc != 0) goto error -- Client side File and Table cannot be scripted -- Set the events declare @on bit set @on = 1 exec sp_trace_setevent @TraceID, 12, 1, @on exec sp_trace_setevent @TraceID, 12, 9, @on exec sp_trace_setevent @TraceID, 12, 2, @on exec sp_trace_setevent @TraceID, 12, 3, @on exec sp_trace_setevent @TraceID, 12, 6, @on exec sp_trace_setevent @TraceID, 12, 8, @on exec sp_trace_setevent @TraceID, 12, 10, @on exec sp_trace_setevent @TraceID, 12, 11, @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, 22, @on exec sp_trace_setevent @TraceID, 12, 34, @on exec sp_trace_setevent @TraceID, 12, 35, @on --exec sp_trace_setevent @TraceID, 13, 1, @on --exec sp_trace_setevent @TraceID, 13, 9, @on --exec sp_trace_setevent @TraceID, 13, 2, @on --exec sp_trace_setevent @TraceID, 13, 3, @on --exec sp_trace_setevent @TraceID, 13, 6, @on --exec sp_trace_setevent @TraceID, 13, 8, @on --exec sp_trace_setevent @TraceID, 13, 10, @on --exec sp_trace_setevent @TraceID, 13, 11, @on --exec sp_trace_setevent @TraceID, 13, 12, @on --exec sp_trace_setevent @TraceID, 13, 13, @on --exec sp_trace_setevent @TraceID, 13, 14, @on --exec sp_trace_setevent @TraceID, 13, 15, @on --exec sp_trace_setevent @TraceID, 13, 22, @on --exec sp_trace_setevent @TraceID, 13, 34, @on --exec sp_trace_setevent @TraceID, 13, 35, @on -- Set the Filters declare @bigintfilter bigint --set @intfilter = 1055342824 --select object_id('dbo.sp_name') -- exec sp_trace_setfilter @TraceID, 22, 0, 0, @intfilter --22 ObjectID 系统分配的对象 ID。 -- 1 TextData 与跟踪内捕获的事件类相关的文本值。 --sp_trace_setfilter [ @traceid = ] trace_id -- , [ @columnid = ] column_id -- , [ @logical_operator = ] logical_operator -- , [ @comparison_operator = ] comparison_operator -- , [ @value = ] value exec sp_trace_setfilter @TraceID, 1, 0, 6, @value --[ **@logical\_operator** = ] logical_operator -- 指定是否应用 AND (0) 或 OR (1) 运算符。 logical_operator 的数据类型为 int,无默认值。 --[ **@comparison\_operator=** ] comparison_operator --指定要执行的比较的类型。 comparison_operator 的数据类型为 int,无默认值。 下表包含比较运算符及其代表的值。 -- 0 =(等于) 6 LIKE 7 NOT LIKE --[ **@value=** ] value --指定要在其上进行筛选的值。 value 的数据类型必须匹配要筛选的列的数据类型。 例如,如果对数据类型为 int 的“对象 ID”列设置筛选,则 value 的数据类型必须为 int。 如果 value 的数据类型为 nvarchar 或 varbinary,则最大长度可为 8000。 --比较运算符为 LIKE 或 NOT LIKE 时,逻辑运算符可以包括“%”或其他适合 LIKE 运算的筛选器。 --在 SQL Server 2005 及更高版本中,您可以将 value 指定为 NULL 以筛选出其列值为 NULL 的事件。 NULL 只对 0(= 等于)和 1(<> 不等于)运算符才有效。 在这种情况下,这些运算符等同于 Transact-SQL IS NULL 和 IS NOT NULL 运算符。 --若要应用介于列值范围内的筛选器,则必须执行 sp_trace_setfilter 两次:一次用大于或等于 ('>=') 比较运算符,一次用小于或等于 ('<=') 运算符。 -- Set the trace status to start exec sp_trace_setstatus @TraceID, 1 -- display trace id for future references select TraceID=@TraceID goto finish error: select ErrorCode=@rc finish: go --================================================================================================ --查询跟踪到的结果 注意需要更改ID id= @TraceID --================================================================================================ declare @file nvarchar(256) select @file= path from sys.traces where id=@TraceID and status=1 AND [path] IS NOT NULL SELECT * FROM ::fn_trace_gettable(@file, DEFAULT) a --where DatabaseID=7 --and TextData like '%CommodityBaseRecord%' ORDER BY starttime desc select * from sys.traces --================================================================================================ --停止跟踪、关闭跟踪并删除跟踪定义。 注意修改 @TraceID --================================================================================================ SELECT * FROM ::fn_trace_getinfo(default) -- First stop the trace. EXEC sp_trace_setstatus @TraceID, 0 -- Close and then delete its definition from SQL Server. EXEC sp_trace_setstatus @TraceID, 2