Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

Using sp_get_query_template to get trace statistics on ad hoc SQL, Expand / Collapse
Posted Monday, February 01, 2010 2:55 PM

Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, April 11, 2014 1:49 PM
Points: 73, Visits: 758
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:

EXEC sp_get_query_template N'SELECT ID FROM dbo.User WHERE UserName = ''Judy''', @TemplateText OUTPUT,@Parameters OUTPUT


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!
Post #857585
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse