Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Using sp_get_query_template to get trace statistics on ad hoc SQL,


Using sp_get_query_template to get trace statistics on ad hoc SQL,

Author
Message
ShawnTherrien
ShawnTherrien
SSC Journeyman
SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)

Group: General Forum Members
Points: 75 Visits: 765
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



AND

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!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search