不拼接sql字符串实现多条件查询
推荐阅读:
写法1:
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
写法2:
select * from table where (adddate = @adddate or @adddate is null) and (name = @name or @name = '')
写法3:
select * from table where
adddate = case @adddate is null then adddate else @adddate end,
name = case @name when '' then name else @name end