我有一个名为spGetOrders的存储过程,它接受一些参数:@startdate和@enddate.这会查询"订单"表.表中的一列称为"ClosedDate".如果订单尚未关闭,则此列将保留NULL;如果订单尚未关闭,则此列将保留为日期值.我想添加一个@Closed参数,它将获取一些值.在一个简单的世界里,我能做到......
select * from orders o where o.orderdate between @startdate AND @enddate and (if @Closed = 1 then o.ClosedDate IS NULL else o.ClosedDate IS NOT NULL)
显然,这不会起作用..我也在看动态sql这是我最后的手段,但开始看起来像答案..
请帮忙..
试试这个:
select * from orders o where o.orderdate between @startdate AND @enddate and ((@Closed = 1 And o.ClosedDate IS NULL) Or (@Closed = 0 And o.ClosedDate IS NOT NULL))
关于在where子句中混合AND和OR的注意事项要谨慎.这样做时,控制评估顺序的括号非常重要.