使用CASE作查询条件后,怎么速度变得很慢,应怎样解决?

fs_hzj 2007-05-07 11:57:42
Select * from vwOutHouseDsp
where iDocStatus<=100 and dDocDate>=@tdBDate and dDocDate<=@tdEDate
and (cDocType='销售发货' or cDocType='销售退货')
and WhSysCode=case when @tcWhSysCode='' then WhSysCode else @tcWhSysCode End
and ISysCode=case when @tcISysCode='' then ISysCode else @tcISysCode End
and IBSysCode=case when @tcIBSysCode='' then IBSysCode else @tcIBSysCode

以上这句在WHERE上使用CASE后查速度就变得很慢了,如果把
and WhSysCode=case when @tcWhSysCode='' then WhSysCode else @tcWhSysCode End
and ISysCode=case when @tcISysCode='' then ISysCode else @tcISysCode End
and IBSysCode=case when @tcIBSysCode='' then IBSysCode else @tcIBSysCode
这几句去掉,速度就正常了,怎么会这样呢?
另外,如果不用CASE,这种情况的条件查询怎样解决比较好呢?
...全文
328 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoxiao8310 2007-05-08
  • 打赏
  • 举报
回复
好象写的不正确啊,case那些应该放在select后面吧
hrb2008 2007-05-08
  • 打赏
  • 举报
回复
把条件
@tcWhSysCode,@tcISysCode,@tcIBSysCode 默认设成null
create procedure protest
@tcWhSysCode int =null,
@tcISysCode int =null,
@tcIBSysCode int =null
as
Select * from vwOutHouseDsp
where iDocStatus<=100 and dDocDate>=@tdBDate and dDocDate<=@tdEDate
and (cDocType='销售发货' or cDocType='销售退货')
and WhSysCode=isnull(@tcWhSysCode,WhSysCode)
and ISysCode=isnull(@tcISysCode,ISysCode)
and IBSysCode=isnull(@tcIBSysCode,IBSysCode)
wwwwb 2007-05-08
  • 打赏
  • 举报
回复
用IIF试试
WhSysCode=iif(@tcWhSysCode='',WhSysCode,@tcWhSysCode)
and ISysCode=iif(@tcISysCode='',ISysCode,@tcISysCode)
and IBSysCode=iif(@tcIBSysCode='',IBSysCode,@tcIBSysCode )
stou 2007-05-08
  • 打赏
  • 举报
回复
语句会出错的。

34,837

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧