我正在通过 odbc 查询 mssql 2008 数据库并发现了一个奇怪的行为。
使用以下代码...
$datefrom = "2009-06-01";
$dateto = "2009-07-01";
$clientno = "01";
$sth = $db->prepare("select count(*) from tbl WHERE v_valid_from <= ? and valid_from <= ? and (v_invalid_from >= ?) and (valid_to >= ? or valid_to is null) and clientno = ?");
$sth->execute(array($datefrom, $datefrom, $dateto, $dateto, $clientno));
该查询检索 80 行,但预期为 1000 行。
查看分析器,服务器执行以下语句:
select count(*) from tbl
WHERE v_valid_from <= '20090601 00:00:00.000' and valid_from <= '20090601 00:00:00.000' and
(v_invalid_from >= '20090701 00:00:00.000') and (valid_to >= '20090701 00:00:00.000' or valid_to is null) and clientno = '01'
当我在日期中输入“-”时,查询会正确执行。
日期时间值似乎没有特殊绑定,所以我想知道为什么会发生这种情况。
会话设置如下:
set quoted_identifier on
set arithabort off
set numeric_roundabort off
set ansi_warnings on
set ansi_padding on
set ansi_nulls on
set concat_null_yields_null on
set cursor_close_on_commit off
set implicit_transactions off
set language English
set dateformat ymd
set datefirst 1
set transaction isolation level read committed