When you use a variable in the WHERE clause the optimizer does not use the value of the variable when it optimizes the query (because the code that sets the value of the variable is in the same batch as the code that the query is in). You can try a couple of things1. This is a case where dynamic SQL can actually be a good thing, so try thisDeclare @Date datetimeSet @Date = '2006-11-01 00:00:00.000'exec sp_executesql N'Select Count(col1) from partitionedview1 where col1 = @Date', N'@Date datetime', @Date
2. Put it in a stored procedure, because the query is optimized when the sproc is executed using the value of the sproc parameter, this is different to using a variable, so try thisCREATE PROC myProc @Date datetimeASSelect Count(col1) from partitionedview1 where col1 = @Date
Then when you call the sproc like thisexec myProc '2006-11-01 00:00:00.000'
it should be correctly optimized.If you have a SQL Server Magazine subscription, seehttp://www.sqlmag.com/Articles/ArticleID/42801/pg/1/1.html