| 首页 | 技术文章 | 软件下载 | 博客 | 论坛 | 精品教程 | 黑客动画 | 视频资源 | 在线服务 | 黑客游戏 | 

您现在的位置: 中国X黑客小组 >> 技术文章 >> 编程技术 >> 网络编程 >> 文章正文 用户登录 新用户注册
  索引对查询条件顺序的影响          【字体:
索引对查询条件顺序的影响
作者:不详    文章来源:CnXHacker.Net    点击数:    更新时间:2007-7-27    

环境:Sql Server2000 +sp4

问题:
select datediff(day,'20040910','20040920') --这句可以执行

--而下面这句不能执行(有时也可以执行)
--sub_para为varchar(8),错误信息是:从字符串转换为 datetime 时发生语法错误。
select * from T_SUB
where item_local_code='03004'
and datediff(day,sub_para,getdate())=29
and (sub_del_flag<>1)

--而且不能执行的时候,这个语句不会返回任何记录集
select * from t_sub
where item_local_code='03004'
and isDate(sub_para)=0

-------------------------------------------------------------------------
--原因,表中创建的索引影响了条件的执行顺序
--导致先执行了 datediff(day,sub_para,getdate())

--下面的测试说明了这个问题
--测试表及数据
create table tb(
item_local_code char(5),
sub_del_flag int,
sub_para varchar(10),
constraint PK_t primary key(sub_para,item_local_code)
)
insert tb select '03004',1,'2003-1-1'
union all select '03005',1,'2003a1-1'
go

--查询语句
select * from (
select * from tb
where item_local_code='03004'
and sub_del_flag<>0
and isdate(sub_para)=1
) A where datediff(day,sub_para,getdate())>29
go

--删除测试
drop table tb

/*--测试结果

item_local_code sub_del_flag sub_para
--------------- ------------ ----------
03004 1 2003-1-1

服务器: 消息 241,级别 16,状态 1,行 3
从字符串转换为 datetime 时发生语法错误。

--*/

 

文章录入:IceRiver    责任编辑:IceRiver 
  • 上一篇文章:

  • 下一篇文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
    中文搜索引擎流量百度占74.8
    搜索引擎频频成被告 搜索结果
    分析称苹果推视窗版浏览器意
    搜索引擎技巧让您成为黑客
    搜索引擎龙虎斗!谷歌盲测PK
    黑客教你如何来入侵搜索引擎
    计算机IT隐私的偷窥者--搜索
    施乐开发新型搜索引擎 比Goo
    百度:Google是最强的搜索引
    硅谷小公司都梦想超越全球头
      网友评论:(只显示最新5条。评论内容只代表网友观点,与本站立场无关!)
    Powered by ICE RIVER - STUDIO
    » CnXHacker.CoM   © CopyRight 2002-2006, CnXHacker.CoM™, Inc. All Rights Reserved.