Search code examples
sql-serversql-server-2012filtered-index

SQL Server Filtered Index WHERE Column = Column


I was hoping to try use a filtered index on a table in SQL Server 2012 to see if it would improve query execution though when trying to create it I am getting the following error:

Msg 10735, Level 15, State 1, Line 3
Incorrect WHERE clause for filtered index 'IX_SRReferralIn_Filtered' on table 'dbo.SRReferralIn'.

Below is the statement I am using. RowIdentifier and IDOrganisationVisibleTo are the columns in the CLUSTERED PRIMARY KEY

CREATE NONCLUSTERED INDEX IX_SRReferralIn_Filtered
ON dbo.SRReferralIn(RowIdentifier, IDOrganisationVisibleTo)
WHERE IDOrganisationVisibleTo = IDOrganisation;

Is the expression in the WHERE clause not supported?


Solution

  • No this is not supported.

    The grammar only allows comparisons with constants

    <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 | = | <> | != | > | >= | !> | < | <= | !< }
    

    You could create an indexed view with this condition though.