下面是 不采用拼接sql字符串实现多条件查询的解决方案
第一种写法是 感觉代码有些冗余
if (@adddate is not null) and (@name <> '')
select * from table where adddate = @adddate and name = @name
else if (@adddate is not null) and (@name ='')
select * from table where adddate = @adddate
else if(@adddate is null) and (@name <> '')
select * from table where and name = @name
else if(@adddate is null) and (@name = '')
select * from table
第二种写法是
select * from table where (adddate = @adddate or @adddate is null) and (name = @name or @name = '')
第三种写法是
select * from table where
adddate = case @adddate is null then adddate else @adddate end,
name = case @name when '' then name else @name end
本文转自cnn23711151CTO博客,原文链接:http://blog.51cto.com/cnn237111/495696 ,如需转载请自行联系原作者