开发者

date ranges in where clause of index view

I have an index view which I run a query against with

WHERE GETDATE() BETWEEN start_date AND end_date

in the WHERE clause.

I suspect that the answer to m开发者_开发问答y question is probably going to be no, but, is it possible to push this into the index view or do something clever to achieve the same effect?

My concern is that what the index view will use from GETDATE() will be whatever GETDATE() returns when the index view is created, which is not what I want. I always want this to be the be current date and time. Also, I'm using SQL Server 2008.


You will not be able to do this within an indexed view.

Think about how SQL Server would have to implement this. Every 3ms (or however often the GETDATE() value can change), SQL Server would have to reassess whether any new rows should be added to the view, or whether any existing rows would need to be removed.

Compare this to the usual examples and usage restrictions of Indexed views, where it's hopefully obvious that SQL server can make inclusion/exclusion decisions during appropriate INSERT/UPDATE statements for the base tables.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜