下面是 不采用拼接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
总结以上是内存溢出为你收集整理的SQLServer 存储过程中不拼接SQL字符串实现多条件查询全部内容,希望文章能够帮你解决SQLServer 存储过程中不拼接SQL字符串实现多条件查询所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)