sql存储过程多条件查询三种方法

发布时间:2019-12-03编辑:脚本学堂
有关sql server存储过程中不拼接sql字符串,实现多条件查询的几种方法,需要的朋友参考下。

不拼接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