环境Sql Server +sp
问题:
select datediff(day) 这句可以执行
而下面这句不能执行(有时也可以执行)
sub_para为varchar()错误信息是从字符串转换为 datetime 时发生语法错误
select * from T_SUB
where item_local_code=
and datediff(daysub_paragetdate())=
and (sub_del_flag<>)
而且不能执行的时候这个语句不会返回任何记录集
select * from t_sub
where item_local_code=
and isDate(sub_para)=
原因表中创建的索引影响了条件的执行顺序
导致先执行了 datediff(daysub_paragetdate())
下面的测试说明了这个问题
测试表及数据
create table tb(
item_local_code char()
sub_del_flag int
sub_para varchar()
constraint PK_t primary key(sub_paraitem_local_code)
)
insert tb select
union all select a
go
查询语句
select * from (
select * from tb
where item_local_code=
and sub_del_flag<>
and isdate(sub_para)=
) A where datediff(daysub_paragetdate())>
go
删除测试
drop table tb
/*测试结果
item_local_code sub_del_flag sub_para
服务器: 消息 级别 状态 行
从字符串转换为 datetime 时发生语法错误