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

前端 未结 7 2306
被撕碎了的回忆
被撕碎了的回忆 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

    This is how it can be done using CASE:

    DECLARE @myParam INT;
    SET @myParam = 1;
    
    SELECT * 
      FROM MyTable
     WHERE 'T' = CASE @myParam
                 WHEN 1 THEN 
                    CASE WHEN MyColumn IS NULL THEN 'T' END
                 WHEN 2 THEN
                    CASE WHEN MyColumn IS NOT NULL THEN 'T' END
                 WHEN 3 THEN 'T' END;
    

提交回复
热议问题