T-SQL Stored Procedure Creates Trace Tables from Trace Files

T-SQL Stored Procedure Creates Trace Tables from Trace Files

LoadTraceFileToTable is a T-SQL stored procedure for use on Microsoft SQL Server 2005. Like SQL Server Profiler, LoadTraceFileToTable automatically creates a trace table from a trace file. Unlike SQL Server Profiler, LoadTraceFileToTable doesn't needlessly enlarge the master or user database because the table is created after the profiling process and not during it.

Download the Code iconI’ve always preferred loading the data from a trace (.trc) file into a table for analysis. For me, it’s easier to sort and handle trace data when it’s in a table because I can easily run queries. So, I wrote the LoadTraceFileToTable stored procedure. With this stored procedure, I can trace whatever I want, put the results in a trace file, use Load- TraceFileToTable to load the data from the trace file into a trace table, run queries against the table, and dispose of the table without losing the original trace file.

The LoadTraceFileToTable stored procedure isn’t meant to replace but rather complement SQL Server Profiler, which also lets you create trace tables from trace files. However, LoadTraceFileToTable does have one advantage over Profiler. With Profiler, the trace table can reach millions of rows during the profiling process and hence enlarge the master or user database. With LoadTraceFileToTable, the table is created after and not during the profiling process, so the database isn’t needlessly enlarged.

The LoadTraceFileToTable stored procedure, which Listing 1 shows, constructs a dynamic T-SQL statement. Specifically, it constructs a SELECT INTO statement that uses the ::fn_trace_gettable system function. This function transforms a trace file into a table. The LoadTraceFile ToTable stored procedure also adds a bigint identity column to sequence the trace rows, as callout A shows.

  (@traceFileName varchar(200), 
  @newTraceTableName sysname)
  DECLARE @tsqlStmt varchar(400)
  SET @tsqlStmt = 

  'SELECT IDENTITY(bigInt,1,1) AS Row_Number,' +
  '* ' + ' INTO ' + RTRIM(LTRIM(@newTraceTableName)) +
  ' FROM ::fn_trace_gettable ( ' + '''' + 
  @traceFileName + '''' + ',default)'

  PRINT @tsqlStmt
  EXEC (@tsqlStmt)

I wrote the LoadTraceFileToTable stored procedure for use on SQL Server 2005. To use LoadTraceFileToTable, you need to pass in two parameters. The first parameter is the pathname to the target trace file. The second parameter is the name of the table in which you want to put the trace file’s data. The trace table’s name needs to be in the format database.schema.tablename. For example, if the trace file’s pathname is C:\ELI_TRACE.trc and the trace table’s name is AdventureWorks.dbo .trc_04052007, you’d run the command

EXEC LoadTraceFileToTable
@traceFileName = ‘C:\ELI_TRACE.trc’,
@newTraceTableName =

Before you run this command, though, you need to make sure that the following conditions are met. (The stored procedure doesn’t check for these conditions.)

  1. The trace file must exist prior to the stored procedure’s execution.
  2. The trace file parameter must be the full pathname.
  3. The table name parameter must be in the format database. schema.table.
  4. The trace table shouldn’t exist prior to the stored procedure’s execution.
  5. The SELECT INTO statement must be a valid statement in the server (i.e., the SELECT INTO/Bulk Copy option must be enabled).

—Eli Leiba, Senior Application DBA,
Israel Electric Company

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.