日期:2014-05-17  浏览次数:20464 次

依条件查询数据
“状态”,“申请人”,“开始时间”,“结束时间”作为查询条件,且这些条件都可以为空。
按照以往的作法,要判断好多种情况,要写一大堆判断语句。现在想请问有没有其他的更好的方法。谢谢!

状态【 】
申请人【 】
开始时间【 】结束时间【 】

查询[color=#0000FF][/color]


------解决方案--------------------
任何方法都要针对每一个条件写判断语句。
------解决方案--------------------
首先sql使用参数 类似
select * from tabe1 where 
( (condition =@condition and @condition is not null ) or @condition is null)
and ((request =@request and @request is not null ) or @request is null)
and ((datetime >=@starttime and @starttime is not null ) or @starttime is null)
and ((datetime <=@endtime and @endtime is not null ) or @endtime is null)