简体   繁体   English

SQL CASE表达式返回基于null和not null的多种模式的集合

[英]SQL CASE expression returning sets based on multiple modes for null and not null

i have task which takes a parameter and has three modes of results 我有一个带有参数并具有三种结果模式的任务

Example Parameter name is sizePram (1 = only null , 2 = only not null, 3 = both or all) 示例参数名称为sizePram(1 =仅为null,2 =仅不为null,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
select * 
from SalesLT.Product 
where 
    (@sizeParam = 1 and Size is null) or
    (@sizeParam = 2 and Size is not null) or
    (@sizeParam = 3)
SELECT *
    FROM SalesLT.Product
    WHERE (@sizeParam = 1 AND Size IS NULL)
       OR (@sizeParam = 2 AND Size IS NOT NULL)
       OR (@sizeParam = 3)
SELECT *
  FROM SalesLT.Product
 WHERE ( sizeParam <> 1 OR Size IS NULL )
       AND NOT ( sizeParam = 2 AND Size IS NULL );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM