原文:
发现SQL Server惊天大秘密!!
--
set statistics xml on
CREATE TABLE T_TEST(ID INT IDENTITY PRIMARY KEY ,CreateTime DATE)
INSERT INTO T_TEST SELECT ' 2011-11-04 '
UNION ALL SELECT ' 2011-11-04 '
UNION ALL SELECT ' 2011-11-04 '
UNION ALL SELECT ' 2011-11-05 '
UNION ALL SELECT ' 2011-11-05 '
UNION ALL SELECT ' 2011-11-05 '
SELECT * FROM T_TEST
SELECT COUNT ( * ) FROM T_TEST
WHERE CreateTime >= ' 2011-11-04 00:00:00 ' AND CreateTime <= ' 2011-11-04 23:59:59 '
-- 等价于(返回3行,这个没有问题)
SELECT COUNT ( * ) FROM T_TEST
WHERE CreateTime = ' 2011-11-04 '
SELECT COUNT ( * ) FROM T_TEST
WHERE CreateTime >= ' 2011-11-04 00:00:00 ' AND CreateTime < ' 2011-11-04 23:59:59 '
-- 等价于(返回0行,因为where条件无意义,这个也没有问题)
SELECT COUNT ( * ) FROM T_TEST
WHERE CreateTime >= ' 2011-11-04 ' AND CreateTime < ' 2011-11-04 '
-- 传入参数为datetime类型(它为什么能返回3行?!!!!这是为什么!!)
SP_executesql N '
select COUNT(*)
from T_TEST
where CreateTime>=@BeginTime and CreateTime<@EndTime
' ,N ' @BeginTime datetime,@EndTime datetime ' ,
@BeginTime = ' 2011-11-04 00:00:00 ' ,
@EndTime = ' 2011-11-04 23:59:59 '
-- 修改了传参类型为date类型(返回0行)
SP_executesql N '
select COUNT(*)
from T_TEST
where CreateTime>=@BeginTime and CreateTime<@EndTime
' ,N ' @BeginTime date,@EndTime date ' ,
@BeginTime = ' 2011-11-04 00:00:00 ' ,
@EndTime = ' 2011-11-04 23:59:59 '
CREATE TABLE T_TEST(ID INT IDENTITY PRIMARY KEY ,CreateTime DATE)
INSERT INTO T_TEST SELECT ' 2011-11-04 '
UNION ALL SELECT ' 2011-11-04 '
UNION ALL SELECT ' 2011-11-04 '
UNION ALL SELECT ' 2011-11-05 '
UNION ALL SELECT ' 2011-11-05 '
UNION ALL SELECT ' 2011-11-05 '
SELECT * FROM T_TEST
SELECT COUNT ( * ) FROM T_TEST
WHERE CreateTime >= ' 2011-11-04 00:00:00 ' AND CreateTime <= ' 2011-11-04 23:59:59 '
-- 等价于(返回3行,这个没有问题)
SELECT COUNT ( * ) FROM T_TEST
WHERE CreateTime = ' 2011-11-04 '
SELECT COUNT ( * ) FROM T_TEST
WHERE CreateTime >= ' 2011-11-04 00:00:00 ' AND CreateTime < ' 2011-11-04 23:59:59 '
-- 等价于(返回0行,因为where条件无意义,这个也没有问题)
SELECT COUNT ( * ) FROM T_TEST
WHERE CreateTime >= ' 2011-11-04 ' AND CreateTime < ' 2011-11-04 '
-- 传入参数为datetime类型(它为什么能返回3行?!!!!这是为什么!!)
SP_executesql N '
select COUNT(*)
from T_TEST
where CreateTime>=@BeginTime and CreateTime<@EndTime
' ,N ' @BeginTime datetime,@EndTime datetime ' ,
@BeginTime = ' 2011-11-04 00:00:00 ' ,
@EndTime = ' 2011-11-04 23:59:59 '
-- 修改了传参类型为date类型(返回0行)
SP_executesql N '
select COUNT(*)
from T_TEST
where CreateTime>=@BeginTime and CreateTime<@EndTime
' ,N ' @BeginTime date,@EndTime date ' ,
@BeginTime = ' 2011-11-04 00:00:00 ' ,
@EndTime = ' 2011-11-04 23:59:59 '