T-SQL Where Clause Case Statement Optimization (optional parameters to StoredProc)

后端 未结 5 964
死守一世寂寞
死守一世寂寞 2021-02-05 11:19

I\'ve been battling this one for a while now. I have a stored proc that takes in 3 parameters that are used to filter. If a specific value is passed in, I want to filter on that

5条回答
  •  清酒与你
    2021-02-05 11:27

    If you pass in a null value when you want everything, then you can write your where clause as

       Where colName = IsNull(@Paramater, ColName)  
    

    This is basically same as your first method... it will work as long as the column itself is not nullable... Null values IN the column will mess it up slightly.

    The only approach to speed it up is to add an index on the column being filtered on in the Where clause. Is there one already? If not, that will result in a dramatic improvement.

提交回复
热议问题