Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dump an output from SQL Server Profiler 2008 to a CSV-like file

I am debugging stored procedures, and right now I am interested in what ran in what order and which which parameters as opposed to how fast things ran and what may sneak in between and cause a slowdown.

So, I captured a couple of minutes worth of trace. The problem is that there is too much stuff, and I need to narrow it down. If I do File -> Save As, I get the following options:

  • Trace File...
  • Trace Template...
  • Trace Table...
  • Trace XML File...
  • Trace XML File for Replay...

Now, these are decent options, but what I really want is a tabular format, such as CSV. I think that commas in SP trace would probably mess up the CSV format. I would gladly use something else, such as || as a delimiter.

Once I do have the tabular format, I can filter it down using grep, etc. and then easily process it with Python to see the exact things I want. I started parsing the XML file with a script, but found myself spending too much time on the trace XML file format (have not used lxml library before).

So ... is there an easier way? Can I at least copy it to Excel somehow?

like image 372
Hamish Grubijan Avatar asked Nov 23 '10 15:11

Hamish Grubijan


2 Answers

If you save it into a trace table; you can get the data in a table in SQL Server which will let you manipulate it to your hearts content; including dumping it out to CSV if still required. The text data column is fully represented in the table.


If you choose SaveTrace Table. You will be prompted for the name of the table and the database. Lets say you call it ProfilerTemp in the database scratch.

Enter those; you can query the table using

select * from scratch.dbo.ProfilerTemp 

You will see everything in the trace window in the table. If you didnt filter down to just stored procedures and want just them in the select

Select textdata from [Scratch].[dbo].[ProfilerTemp]    where eventclass = 10    And textdata like 'exec %'    and not cast(TextData as nvarchar(max))= 'exec sp_reset_connection' 

This filters out non procedure calls and any connection resets you may have. You may need to add more filters depending on what you are trying to do.

If you want this out as a text file; choose query - results to file and run the query. This will prompt for the file name and give you the parameter text as a text file.

like image 135
u07ch Avatar answered Sep 20 '22 08:09

u07ch


TL;DR: Copy into a text editor, manually prep, then paste into Excel.

I have very little experience with SQL Server, so I don't know if this will work for others, but it did for me:

  • Select desired rows in SQL Server Profiler. Ctrl + C to copy.
  • Paste into a plain text editor that can do regular expression search and replace (e.g. Notepad++ in my case).
  • Regex replace (N'(''')?[^']*?)\r\n(([^']*?)\r\n)?(([^']*?)\r\n)? with $1 $4 $6
    • This clears all newlines from SQL scripts.
    • Keep doing "Replace All" until no more results are found.
  • Regex replace (Batch(Starting|Completed)[^\\]*?)\r\n with $1
    • This clears newlines from more SQL stuff. Again, keep replacing until no results.
  • Regex replace \r\nset with set
    • This clears all newlines from Audit Login scripts
  • You may need to do more replacing, you get the idea.
  • Paste into excel using the "Text Import Wizard." Use tabs as the deliminator.
  • Sort by the first column and remove any unhelpful rows (e.g. "Audit Login" in my case). You may also need to manually move some data over a column (e.g. "EntityFramework" data in my case)
like image 21
Aaron Campbell Avatar answered Sep 20 '22 08:09

Aaron Campbell