【发布时间】:2016-09-17 17:24:13
【问题描述】:
我正在尝试从 SQL Server 数据库中获取计数,它为具有空值的字段提供 0。以下是我正在使用的。
my $sql = q{SELECT count(*) from customer where first_name = ? and last_name = ?};
my @bind_values = ($first_name, $last_name);
my $count = $dbh->selectrow_array($sql, undef, @bind_values);
如果数据库中任一值为空,则返回 0。我知道如果传递的参数是 undef,prepare 会自动生成 is null,但我不知道为什么它不起作用。
所以这是奇怪的观察。当我在 Toda for SQL server 中键入带有值的 SQL 时,它可以工作:
SELECT count(*) from customer where first_name = 'bob' and last_name is null
但是当我尝试相同的查询并在 first_name = bob 和 last_name {null} 的参数中传递值时。它不起作用。
SELECT count(*) from customer where first_name = ? and last_name = ?
【问题讨论】:
-
打开调试看看你是否正确
-
是的,我尝试过调试,但是当我进入 DBI 准备函数时,它需要很长时间并且 eclipse 停止响应。
-
“我知道 prepare 会自动完成
is null” - 不,不会。 -
它不起作用,因为 dbi do not use is null,它使用 last_name= null 我猜
-
使用DBI_TRACE,你应该会看到创建的SQL