• How to make Sql Trace Without Sql Profiler?


    Sometimes maybe we dont have some sql trace tool . But actually we really have to work around some tracing work in sql server. especially in sql server express .

     As microsoft said.There are nothing tools like sql profiler in sql server express .

    so . we have to work out this problem . when we face it .

    Actually . the backgroud things which done in the Sql profiler tools is based on the Procedure . function etc .

    so . Let us have a look at the stored procedures and functions being responsible for the tracing.

     

    If we want to list all existing traces we should run select query on sys.traces table.

    SELECT * FROM sys.traces

    This should give list of all traces on server with their id, status, path to *.trc log file etc…

    We’re creating trace with sp_trace_create command:

    DECLARE @tid INT
    EXEC sp_trace_create
     @traceid = @tid OUTPUT,
     @options = 2,
     @tracefile = N'c:\trace'
    SELECT @tid -- this will display our new trace id
    GO

    After creating new trace we should set events we want to be traced and columns we want to be written when this event occurs. We can do this with sp_trace_setevent procedure.

    DECLARE @tid INT, @cid INT-- 
    SET @tid = 2     -- our trace id use the trace id you got from sp_trace_create
    SET @cid = 1     -- 1 for ON
    WHILE(@cid < 65) -- there are 65 columns in result,
                     -- let's process them all
          BEGIN
            EXEC sp_trace_setevent @tid, 10, @cid, 1
            EXEC sp_trace_setevent @tid, 12, @cid, 1
            EXEC sp_trace_setevent @tid, 14, @cid, 1
            EXEC sp_trace_setevent @tid, 41, @cid, 1
            EXEC sp_trace_setevent @tid, 43, @cid, 1
            SET @cid += 1
          END
    GO

    Now we can add some filters to trace, for example we want to trace only inserts on table “Users” in “cms” database. This can be done with sp_trace_setfilter procedure.

    DECLARE @tid INT
    SET @tid = 2     -- our trace id
    EXEC sp_trace_setfilter @tid, 1, 1, 6, N'%insert into %Users%'
    EXEC sp_trace_setfilter @tid, 35, 0, 0, N'cms'

    Now we can set our trace in running state with command sp_trace_setstatus. It is very important one.

    DECLARE @tid INT
    SET @tid = 2     -- our trace id
    EXEC sp_trace_setstatus @tid, 1
    GO

    We can use this same function for stopping or deleting trace.

    DECLARE @tid INT
    SET @tid = 2     -- our trace id
    EXEC sp_trace_setstatus @tid, 0 -- stops trace
    GO
    EXEC sp_trace_setstatus @tid, 2 -- deletes trace
    GO

    And at the end, for viewing *.trc file we’re using fn_trace_gettable function:

    select * from ::fn_trace_gettable(N'c:\trace.trc',default)

    I hope this will be helpful for someone.

     

    see also:

    http://msdn.microsoft.com/zh-cn/library/ms186265.aspx

    http://mbielanczuk.com/2012/01/creating-trace-on-sql-express-server/ 

  • 相关阅读:
    财务自由之路--笔记
    Spring Cloud微服务笔记(一)微服务与云概念
    Java8新特性----Stream
    Docker操作笔记(四)使用网络
    Docker操作笔记(三)数据管理
    Docker操作笔记(二)容器
    Docker操作笔记(一)使用镜像
    信息化领域热词分类分析及解释
    假期第二周总结
    假期第一周总结
  • 原文地址:https://www.cnblogs.com/malaikuangren/p/2604091.html
Copyright © 2020-2023  润新知