首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >sql 多条件查询的一种简单的方法

sql 多条件查询的一种简单的方法

作者头像
跟着阿笨一起玩NET
发布2018-09-18 14:38:23
2.2K0
发布2018-09-18 14:38:23
举报
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 <> '')
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 = '')
结果一调试,成功,
一点想法,有更好方法的,请指教!~
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2012-04-21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档