Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Optimization with a date condition
Message
 
 
To
22/03/2011 17:21:31
General information
Forum:
Microsoft SQL Server
Category:
Scripting
Environment versions
SQL Server:
SQL Server 2008
Application:
Web
Miscellaneous
Thread ID:
01504587
Message ID:
01504590
Views:
34
>If I have a script like this:
>
>
>SELECT MyTable.MyField FROM MyDatabase.dbo.MyTable WHERE MyTable.AddDate>'2011/03/22 17:08:26'
>
>
>...it executes instantly. The result is zero record.
>
>But, if I parameterize it such as:
>
>
>DECLARE @Date datetime
>
>SET @Date='2011/03/22 17:08:26'
>
>SELECT MyTable.MyField FROM MyDatabase.dbo.MyTable WHERE MyTable.AddDate>@Date
>
>
>...it takes a while. And, the result is still zero record.
>
>I tried to make the declaration char and nvarchar but that did not help.
>
>There is something I am missing here.

Try adding

option (recompile) to the query.

http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/a2030652-800b-48dd-8857-5e6e60658a6e

http://social.msdn.microsoft.com/forums/en-us/transactsql/thread/CFA976DB-2D12-4D80-B4B2-CEFFB80B7F6C
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform