Using gilamonster's examples, I was easily able to capture a serverside trace so that I could group the data and get some meaningful results over a time frame.
Unfortunately her example was using 100% stored procedures. So this script works fine with the stored procedures, but not for the ad hoc SQL, which is about 10-20% of the entries.
I researched sp_get_query_template and can get this to work by typing in my own unicode data like so:
DECLARE @TemplateText NVARCHAR(MAX), @Parameters NVARCHAR(MAX)
EXEC sp_get_query_template N'SELECT ID FROM dbo.User WHERE UserName = ''Judy''', @TemplateText OUTPUT,@Parameters OUTPUT
DECLARE @SQLText NVARCHAR(MAX),@TemplateText NVARCHAR(MAX), @Parameters NVARCHAR(MAX)
SELECT @SQLText = N'SELECT ID FROM dbo.User WHERE UserName = ''Judy'''
EXEC sp_get_query_template @SQLText, @TemplateText OUTPUT,@Parameters OUTPUT
When I place this same code into a cursor and @SQLText is coming directly from the TraceResults table, I get the following error:
Msg 214, Level 16, State 21, Procedure sp_get_query_template, Line 1
Procedure expects parameter '@querytext' of type 'nvarchar(max)'.
I"ve tried changing the field in the table to NVARCHAR, I've tried casting it to an intermediary NVARCHAR, but whatever I seem to be doing, the text returned from the trace and placed into the table doesn't seem to be converting to NVARHCAR proproperly.
I suppose I have two questions then:
1. Is there a better way to be doing this other than cursoring through the table and running this SP line by line? Ideally I would love if it were a function and I could CROSS APPLY it inline, but that doesn't seem to be the case.
2. If the cursor is the way to go, what would be the proper way to convert the data from VARCHAR to NVARCHAR?
Thanks very much!