sql多条件查询的⼀种简单的⽅法
以后我们做多条件查询,⼀种是排列结合,另⼀种是动态拼接SQL
如:我们要有两个条件,⼀个⽇期@addDate,⼀个是@name
第⼀种写法是
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 <> '')
queryselectorall用法select * from table where and name = @name
else if(@addDate is null) and (@name = '')
select * from table
第⼆种就是动态组成SQL,通过exec来执⾏,我就不写,
昨天我想到⼀种办法
select * from table where (addDate = @addDate or @addDate is null) and (name = @name or @name = '')结果⼀调试,成功,
⼀点想法,有更好⽅法的,请指教!~
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论