sql-server-2008t-sqlindexingnon-clustered-index

Create unique index with filter expression having IS NULL or OR


I am trying to develop a unique index.

CREATE UNIQUE NONCLUSTERED INDEX NCI_NewUnique
ON [NewUnique]([U1])
WHERE (ISNULL([MyField], '') = '') 

My error is

Incorrect WHERE clause for filtered index 'NCI_NewUnique' on table 'NewUnique'.

Here is another attempt; This one I have removed the ISNULL(MyField, '') part. Why cannot this one have an OR?

CREATE UNIQUE NONCLUSTERED INDEX NCI_NewUnique
ON [NewUnique]([U1])
WHERE (
         ([MyId] IS NULL) 
         OR 
         ([MyId] IS NOT NULL AND [MyField] IS NOT NULL)
      )

Error is:

Incorrect syntax near the keyword 'OR'.

Solution

  • I don't know why but according to the documentation functions and or is not allowed.

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