CASE when子句使用时

时间:2017-09-12 06:19:54

标签: sql-server

declare @custid varchar(50) = null

select * 
from Items 
where ShowItemNlyCusLoggedInWeb = case when 
                                       @custid = not null then 
                                       ShowItemNlyCusLoggedInWeb=0 or isnull

这对我不起作用。

2 个答案:

答案 0 :(得分:0)

declare @custid varchar(50)=null

select * 
from   Items 
where  (ISNULL(ShowItemNlyCusLoggedInWeb,0) = 0 OR @custid IS NULL)

答案 1 :(得分:0)

你可能正在寻找这个

WHERE 1 = CASE WHEN @custid IS NOT NULL AND ShowItemNlyCusLoggedInWeb = 0  THEN 1
               WHEN @custid IS NULL AND ShowItemNlyCusLoggedInWeb IN (0,1)  THEN 1
               ELSE 0
          END