从case语句where子句获取是空记录

时间:2011-08-11 08:00:24

标签: sql sql-server-2008

我想获取分类IS NULL记录WHEN'S',我无法通过以下case语句获取它

 USE AdventureWorks2008R2;
 GO
 DECLARE @Cat VARCHAR(5)
 SET @Cat='S' 
 SELECT   ProductNumber,ProductName where Category =
  CASE @Cat
     WHEN 'R' THEN 'Road'
     WHEN 'M' THEN 'Mountain'
     WHEN 'T' THEN 'Touring'
     WHEN 'S' THEN 'NULL'
     ELSE 'Not for sale'
  END,
  Name
 FROM Production.Product
 ORDER BY ProductNumber;
 GO

我想要获得的是以下查询

  DECLARE @Cat VARCHAR(5)
  SET @Cat='S' 
  SELECT   ProductNumber,ProductName where Category is null

这里@Cat是我的输入参数,来自我的GUI

1 个答案:

答案 0 :(得分:1)

删除代码中的WHEN 'S' THEN 'NULL'并添加此谓词:

OR (Category is null and @Cat = 'S')