Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 913
mheptinstall Avatar asked Jul 01 '26 19:07

mheptinstall


1 Answers

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.

like image 110
Martin Smith Avatar answered Jul 04 '26 18:07

Martin Smith