Results 1 to 5 of 5

Thread: Read an SQL Trace file into a Trace table

  1. #1

    Thread Starter
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Read an SQL Trace file into a Trace table

    I'm using SQL Server 2005 Enterprise Edition. What I need to do is read an unknown number of tracefiles into a database table (I can create the table to load if needed). I know I can use SQL Profiler to do this, open the trace file in profiler then select Save As--> Trace table. What I want to do is something that will automate this process.

    I know there is the function fn_trace_gettable that will read from the first trace in a series to the last, but unfortunately the results are not sequential. The trace stops at a file size of 5 Meg and start a new one with a different name. Since the fn_trace_gettable function relies on the start trace and end trace markers in the files I can not use this.

    I already have a method but that involves opening each file in SQL Profiler and then saving it. I then run an batch process to generate a summary report of the data in the traces.

    Any help ideas would be greatly appreciated.

    Gary
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  2. #2
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: Read an SQL Trace file into a Trace table

    I know this doesn't help with traces you have already done but for new traces why not just set the trace to directly record to a table when you start it ?

    You can also run a Trace to table from code -

    http://msdn.microsoft.com/en-us/libr...4(SQL.90).aspx
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  3. #3

    Thread Starter
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Read an SQL Trace file into a Trace table

    Saving as a trace table on the machine is not an option. The analysis will be done on a local machine and not the servers. So I will be getting the trace files sent to me to analyse and need to do the work locally. I have already thought of writing a utility that will read the contents of a folder and precess the result of the folder scan to a set of tables using fn_trace_gettable. I just want to avoid doing the work if someone already has a solution to this.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  4. #4
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: Read an SQL Trace file into a Trace table

    Right i see what you mean, that makes things a lot more difficult !!

    I don't know any way to do what you want automatically, i suppose you could save the file as XML, which you could then bulk insert into a table.

    The only problem with this is you would have to have the table already set-up with the exact correct columns in it, and you may need a mapping file.

    I don't know if you are going to find an easy way to do this unfortunately!
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  5. #5

    Thread Starter
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Read an SQL Trace file into a Trace table

    I got a start any way this will read the trace files and get the data I want to summarize

    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim textData As String = String.Empty
    3.         Dim dbId As Integer = 0
    4.         Dim duration As Integer = 0
    5.         Dim cpu As Integer = 0
    6.         Dim reads As Integer = 0
    7.         Dim writes As Integer = 0
    8.         Dim sTime As Date
    9.         Dim msg As String = String.Empty
    10.  
    11.         Dim myTraceFile As New Microsoft.SqlServer.Management.Trace.TraceFile
    12.         Dim fbFolder As New FolderBrowserDialog
    13.         fbFolder.ShowDialog()
    14.         Dim fName As New System.IO.DirectoryInfo(fbFolder.SelectedPath.Trim())
    15.         fbFolder.Dispose()
    16.         Dim allFiles As IO.FileInfo() = fName.GetFiles("*.trc")
    17.         For Each f As System.IO.FileInfo In allFiles
    18.             myTraceFile.InitializeAsReader(f.FullName())
    19.             Dim i As Integer = 0
    20.             While myTraceFile.Read()
    21.                 'EventClass  0  String
    22.                 'BinaryData  1      Byte()
    23.                 'DatabaseID  2      Int32
    24.                 'SPID        3      Int32
    25.                 'Duration    4      Int64
    26.                 'StartTime   5      DateTime
    27.                 'Reads       6      Int64
    28.                 'Writes      7      Int64
    29.                 'CPU         8      Int32
    30.                 'TestData    9      String
    31.                 'ObjectId   10      Int32
    32.                 textData = myTraceFile.GetString(9)
    33.                 If textData IsNot Nothing Then
    34.                     dbId = myTraceFile.GetInt32(2)
    35.                     msg = "DBId = " & dbId & System.Environment.NewLine
    36.                     duration = myTraceFile.GetInt64(4)
    37.                     msg &= "Duration: " & duration.ToString() & System.Environment.NewLine
    38.                     cpu = myTraceFile.GetInt32(8)
    39.                     msg &= "CPU: " & cpu.ToString() & System.Environment.NewLine
    40.                     sTime = myTraceFile.GetDateTime(5)
    41.                     msg &= "StartTime: " & sTime & System.Environment.NewLine
    42.                     reads = myTraceFile.GetInt64(6)
    43.                     msg &= "Reads: " & reads.ToString() & System.Environment.NewLine
    44.                     writes = myTraceFile.GetInt64(7)
    45.                     msg &= "Writes: " & writes.ToString() & System.Environment.NewLine
    46.                     msg &= "TextData: " & textData
    47.                     Me.TextBox1.Text = msg
    48.                 End If
    49.                 i += 1
    50.                 If i = 15 Then
    51.                     Exit While
    52.                 End If
    53.                 msg = String.Empty
    54.             End While
    55.             myTraceFile.Close()
    56.         Next
    57.         myTraceFile.Dispose()
    58.  
    59.     End Sub
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width