SQL CASE表达式基于多个模式返回集合为null而非null

时间:2012-03-21 20:44:59

标签: sql sql-server

我有一个带参数的任务,有三种结果模式

实施例 参数名称为sizePram(1 =仅为空,2 =仅为非空,3 =为全部或全部)

declare @sizeParam int
set @sizeParam = 1 or 2 or 3

select * from SalesLT.Product where Size =
CASE @sizeParam
    when 1 then is null
    when 2 then is not null
    when 3 then all
end

3 个答案:

答案 0 :(得分:4)

select * 
from SalesLT.Product 
where 
    (@sizeParam = 1 and Size is null) or
    (@sizeParam = 2 and Size is not null) or
    (@sizeParam = 3)

答案 1 :(得分:1)

SELECT *
    FROM SalesLT.Product
    WHERE (@sizeParam = 1 AND Size IS NULL)
       OR (@sizeParam = 2 AND Size IS NOT NULL)
       OR (@sizeParam = 3)

答案 2 :(得分:0)

SELECT *
  FROM SalesLT.Product
 WHERE ( sizeParam <> 1 OR Size IS NULL )
       AND NOT ( sizeParam = 2 AND Size IS NULL );