我通过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'
当我在日期中加上' - '时,查询会正确执行。
似乎没有针对datetime-values的特殊绑定,所以我想知道为什么会发生这种情况。
会话设置如下:
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
答案 0 :(得分:0)
我没有足够的代表发表评论。如果在开头设置日期值时将“ - ”更改为“/”会发生什么?例如:
$datefrom = "2009/06/01";
$dateto = "2009/07/01";