当某些事情是真的时,有没有办法忽略CASE中的AND?

时间:2016-11-14 15:10:07

标签: sql sql-server tsql case

我有这个Where条款

Select * From Student_Info si
Inner Join Certifications cc
Inner Join Cert_Earned ce

Where si.grad_date = @grad_date
AND cc.org_no = @org_no

但我需要一个额外的AND,如果结果是值为false,我应该忽略它,我会想要所有证书

AND cc.industrial = CASE WHEN @industrial = 0 THEN Do Nothing
                     Else @industrial

3 个答案:

答案 0 :(得分:8)

这通常表示为:

AND (@industrial = 0 OR ccc.industrial = @industrial)

答案 1 :(得分:1)

听起来你只想添加一个在两个不同条件之间执行OR的谓词

AND (@industrial = 0 or ccc.industrial = @industrial)

答案 2 :(得分:0)

您可以使用CASE函数执行类似的操作:

AND 1 = (CASE WHEN @industrial = 0 THEN 1 
              ELSE CASE WHEN cc.industrial = @industrial THEN 1 END
         END)

或者如果cc.industrial不可为空,那么可能:

AND cc.industrial = CASE WHEN @industrial = 0 THEN cc.industrial
                     Else @industrial END