WHERE IS NULL, IS NOT NULL or NO WHERE clause depending on SQL Server parameter value

前端 未结 7 2307
被撕碎了的回忆
被撕碎了的回忆 2020-12-02 22:57

I have a stored procedure in SQL Server 2000 that performs a search based on parameter values. For one of the parameters passed in, I need a different WHERE cla

7条回答
  •  青春惊慌失措
    2020-12-02 23:30

    WHERE MyColumn = COALESCE(@value,MyColumn) 
    
    • If @value is NULL, it will compare MyColumn to itself, ignoring @value = no where clause.

    • IF @value has a value (NOT NULL) it will compare MyColumn to @value.

    Reference: COALESCE (Transact-SQL).

提交回复
热议问题