zoukankan      html  css  js  c++  java
  • 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/ 

  • 相关阅读:
    Java技术 第九次实验
    JAVA第八次作业
    Java第七次作业--图形用户界面
    Java第六次作业
    Java第五次作业
    《Java技术》第四次作业
    Java技术第三次作业
    《Java技术》第二次作业
    《Java技术》第一次作业
    ActiveQq的代码实现
  • 原文地址:https://www.cnblogs.com/malaikuangren/p/2604091.html
Copyright © 2011-2022 走看看