Create a tracking operation of a table of

- ================================================ ================================================ 
- create a tracking operation of a table of 
- pay attention to modify the path and @value 
- ============================== ================================================== ================ 
- the Create A Queue 
DECLARE  @rootPath  nVARCHAR ( 200 is ) =  ' D: \ INOUT \ TraceLog \ '  
DECLARE  @path  nvarchar ( 500 )
 DECLARE  @rc  int 
DECLARE  @traceid  int 
DECLARE  @maxfilesize  bigint 
the 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 
September  @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 --select object_id = 1,055,342,824 ( 'dbo.sp_name') 
- Exec sp_trace_setfilter @traceid, 22 is, 0, 0, @intfilter 
- 22 is ObjectID system assigned object ID. 
- the associated text value 1 TextData event class captured within track.

--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   
-   Specifies whether AND (0) or OR (1) operator. logical_operator data type int, with no default value.

- [** @ Comparison \ _operator = **] comparison_operator 
- specifies the type of comparison to be performed. comparison_operator data type int, with no default value. The following table contains value comparison operators and their representatives. 
- 0 = (equals) 6 LIKE 7 NOT LIKE

- [@ value = ** **] value 
- Specifies the value to be screened thereon. value data type must match the data type of the column you want to filter. For example, if the column is set to the data type "Object ID" int screening, the value of the data type to be an int. If the value is nvarchar or varbinary, the maximum length of 8000.

- When the comparison operators LIKE or NOT LIKE, logical operators may include a "%", or other suitable filters LIKE operation.

- In SQL Server 2005 and later versions, you can specify NULL value to filter out events that the column value is NULL. NULL is effective only 0 (= equal) and 1 (<> not equal) operator. In this case, the operator is equivalent to Transact-SQL IS NULL and IS NOT NULL operator.

- To apply a filter interposed between the column values range sp_trace_setfilter must be performed twice: once with greater than or equal to ( "> =") comparison, with less than or equal to one ( '<=') operator .


-- 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


- ================================================ ================================================ 
- queries to track the results of attention to the need to change the above mentioned id = @traceid ID 
- =================================== ================================================== =========== 
DECLARE  @file  nvarchar ( 256 ) 
 SELECT  @file = path from sys.traces WHERE ID = @traceid  and Status = . 1  the aND  [ path ]  the IS  the NOT  NULL 
the SELECT  *  the fROM :: the fn_trace_gettable (@file, DEFAULT) a
--where DatabaseID=7
--and TextData like '%CommodityBaseRecord%'
ORDER BY starttime desc



the SELECT  *  from sys.traces 
 - ========================================== ================================================== ==== 
- stop tracking, tracing and delete closed trace definition. Pay attention to modify @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

 

Guess you like

Origin www.cnblogs.com/davidhou/p/11792228.html