• Sean Pearce (7/16/2014)


    SELECT ID, Code, Value1

    FROM Table1 T1

    WHERE ISNULL(T1.[Code], 'NULL') IN (@Param)

    One thing about the code above. It is always going to produce a table scan, because it is wrapping a function around the [Code] column. If the table is a large one this may run slowly. If the table has a covering index on the Code column, a faster performing solution would be to join the table variable to the table. This would return everything but the rows where the Code value is null. You could use a UNION ALL to get the NULLS like this.

    UNION ALL

    SELECT ID, Code, Value1

    From Table T1

    Where 'NULL' in (@param) and T1.Code is null

    Again, this code runs faster if a covering index on the code column exists, and if you are trying to pull a few rows out of a big table. The more rows your @Param will pull, the more likely that the optimizer will choose to do a table scan anyway, but it's important to know that functions wrapped around columns are not "SARGABLE." If you want to know more, just search on that word. It will pay off for you in the future.

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills