sqlserver中where条件加判断

我想实现如下功能:

where
 case  when (@a = null)
     then 1 = 1
     else @a=a
and b=@b

  但是这样报错,经过翻阅资料找到如下解决方案:

where
(1 = (CASE WHEN @a IS NULL THEN 1 ELSE 0 END)
OR  a=@a )
AND b=@b

测试得知,当1=1时不执行后面的a=@a,反之执行。

原文地址:https://www.cnblogs.com/objectnull/p/9378175.html