Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Parameterized TOP n
Message
From
29/08/2001 14:10:48
 
 
To
29/08/2001 13:28:59
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
00550093
Message ID:
00550471
Views:
16
>>Can you use dynamic SQL in your query? This way you can parameterize Top.
>
>I considered it, but am a little shy to use dynamic SQP because of the traffic that this SPROC will get (on the order of hundreds of hits per second). Wouldn't dynamic SQL require that the statement be compiled on every hit? That would be unacceptable...

PMFJI,
You can use a "parameterized" sp_executesql string that could be compiled only once: (this example from the BOL):
DECLARE @IntVariable INT
DECLARE @SQLString NVARCHAR(500)
DECLARE @ParmDefinition NVARCHAR(500)  

/* Build the SQL string once. */
SET @SQLString =  N'SELECT * FROM pubs.dbo.employee WHERE job_lvl = @level'

/* Specify the parameter format once. */
SET @ParmDefinition = N'@level tinyint'

/* Execute the string with the first parameter value. */
SET @IntVariable = 35
EXECUTE sp_executesql @SQLString, @ParmDefinition, @level = @IntVariable

/* Execute the same string with the second parameter value. */
SET @IntVariable = 32
EXECUTE sp_executesql @SQLString, @ParmDefinition,
                      @level = @IntVariable
The BOL states that the query optimizer "should" use the execution plan from the first call, whatever that means. I guess testing should give you a definitive answer.

There's another bit about reusing execution plans in the BOL topic "Using sp_executesql".
HTH
Previous
Reply
Map
View

Click here to load this message in the networking platform