我有一个带有dateTime列的表,我想在其中放置一个过滤的索引。该指数将每周重建一次。每次重建时,我希望它包含两天及更新的行,基于此列。我可以创建这样的过滤索引吗?我尝试过各种方法,但语法错误。
例如,索引创建的以下Where子句不起作用:
WHERE (ReadTime > DateAdd(dd,-2,GetDate()))
答案 0 :(得分:5)
您无法直接引用getdate()
来执行此操作。你需要动态SQL。
CREATE INDEX
语法只允许与常量进行比较。
CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX index_name
ON <object> ( column [ ASC | DESC ] [ ,...n ] )
[ INCLUDE ( column_name [ ,...n ] ) ]
[ WHERE <filter_predicate> ]
... /*Irrelevant grammar removed*/
<filter_predicate> ::=
<conjunct> [ AND <conjunct> ]
<conjunct> ::=
<disjunct> | <comparison>
<disjunct> ::=
column_name IN (constant ,...n)
<comparison> ::=
column_name <comparison_op> constant
<comparison_op> ::=
{ IS | IS NOT | = | <> | != | > | >= | !> | < | <= | !< }