Click here to Skip to main content
15,891,136 members
Articles / Database Development / SQL Server

Use SQL Parameters to Overcome Ad Hoc Performance Issues

Rate me:
Please Sign up or sign in to vote.
4.84/5 (58 votes)
12 Jan 20079 min read 219.5K   407   109  
Defining (rather loosely) ad hoc queries as SQL commands built as strings by an SQL client application and submitted to SQL Server.
��/****************************************************/

/* Created by: SQL Server Profiler 2005             */

/* Date: 12/30/2006  10:01:02 AM         */

/****************************************************/





-- Create a Queue

declare @rc int

declare @TraceID int

declare @maxfilesize bigint

set @maxfilesize = 5 



-- Please replace the text InsertFileNameHere, with an appropriate

-- filename prefixed by a path, e.g., c:\MyFolder\MyTrace. The .trc extension

-- will be appended to the filename automatically. If you are writing from

-- remote server to local drive, please use UNC path and make sure server has

-- write access to your network share



exec @rc = sp_trace_create @TraceID output, 0, N'C:\ProcedureCache', @maxfilesize, NULL 

if (@rc != 0) goto error



-- Client side File and Table cannot be scripted



-- Set the events

declare @on bit

set @on = 1

exec sp_trace_setevent @TraceID, 38, 1, @on

exec sp_trace_setevent @TraceID, 38, 21, @on

exec sp_trace_setevent @TraceID, 38, 10, @on

exec sp_trace_setevent @TraceID, 38, 35, @on

exec sp_trace_setevent @TraceID, 38, 12, @on

exec sp_trace_setevent @TraceID, 35, 1, @on

exec sp_trace_setevent @TraceID, 35, 10, @on

exec sp_trace_setevent @TraceID, 35, 35, @on

exec sp_trace_setevent @TraceID, 35, 12, @on





-- Set the Filters

declare @intfilter int

declare @bigintfilter bigint



exec sp_trace_setfilter @TraceID, 35, 1, 6, N'AdventureWorks'

-- Set the trace status to start

exec sp_trace_setstatus @TraceID, 1



-- display trace id for future references

select TraceID=@TraceID

goto finish



error: 

select ErrorCode=@rc



finish: 

go





exec sp_trace_setstatus 3

          , 0

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer STG Utah
United States United States
Mark is a jock turned geek who has been programming since early 2000 where he stumbled on excel macros and dug down to discover VBA. Since 2001 he has been working for MaxPreps.com where he can be a geek who writes web applications for jocks. He has been using C# and ASP.Net since 2002.

Check out Mark's blog: www.developMENTALmadness.com

View Mark Miller's profile on LinkedIn

Comments and Discussions