ColdFusion MsSQL HAVING SUM()具有NULL值

时间:2011-01-04 10:01:50

标签: sql-server sql-server-2005 coldfusion having-clause

我用CF 9和MsSQL 2005测试了这个

CREATE TABLE HAVING_SUM
(
    A_VARCHAR VARCHAR(5),
    B_INT INT
)

INSERT INTO HAVING_SUM ( A_VARCHAR, B_INT ) VALUES ( 'AB', 2 )
INSERT INTO HAVING_SUM ( A_VARCHAR, B_INT ) VALUES ( 'AB', 3 )
INSERT INTO HAVING_SUM ( A_VARCHAR, B_INT ) VALUES ( 'AB', 5 )
INSERT INTO HAVING_SUM ( A_VARCHAR, B_INT ) VALUES ( 'CD', 2 )
INSERT INTO HAVING_SUM ( A_VARCHAR, B_INT ) VALUES ( 'CD', 7 )
INSERT INTO HAVING_SUM ( A_VARCHAR, B_INT ) VALUES ( 'CD', 8 )
INSERT INTO HAVING_SUM ( A_VARCHAR, B_INT ) VALUES ( 'CD', NULL)

然后在Ms SQL Server Management Studio Express中查询

SELECT  *
FROM    HAVING_SUM

SELECT  A_VARCHAR,
        SUM(B_INT) AS B_INT
FROM    HAVING_SUM
GROUP BY A_VARCHAR

SELECT  A_VARCHAR,
        SUM(B_INT) AS B_INT
FROM    HAVING_SUM
GROUP BY A_VARCHAR
HAVING SUM(B_INT) = 10

SELECT  A_VARCHAR,
        SUM(B_INT) AS B_INT
FROM    HAVING_SUM
GROUP BY A_VARCHAR
HAVING SUM(B_INT) = 10.5

结果是

alt text

然后我尝试使用cfquery

<cfquery name="qryHavingSum_1">
SELECT  *
FROM    HAVING_SUM
</cfquery>

<cfdump var="#qryHavingSum_1#">

<cfquery name="qryHavingSum_2">
SELECT  A_VARCHAR,
        SUM(B_INT) AS B_INT
FROM    HAVING_SUM
GROUP BY A_VARCHAR
</cfquery>

<cfdump var="#qryHavingSum_2#">

<cfquery name="qryHavingSum_3">
SELECT  A_VARCHAR,
        SUM(B_INT) AS B_INT
FROM    HAVING_SUM
GROUP BY A_VARCHAR
HAVING SUM(B_INT) = 10
</cfquery>

<cfdump var="#qryHavingSum_3#">

<cfquery name="qryHavingSum_4">
SELECT  A_VARCHAR,
        SUM(B_INT) AS B_INT
FROM    HAVING_SUM
GROUP BY A_VARCHAR
HAVING SUM(B_INT) = 10.5
</cfquery>

<cfdump var="#qryHavingSum_4#">

,结果是

alt text alt text

修改(从这里开始): 向下滚动到调试器我有这个

alt text

红色区域表示qryHavingSum_4已执行,0记录,但在异常部分中,qryHavingSum_4未定义(<cfdump var="#qryHavingSum_4#">修改(此处结束)

如果我将qryHavingSum_4更改为

<cfquery name="qryHavingSum_4">
SELECT  A_VARCHAR,
        SUM(ISNULL(B_INT, 0)) AS B_INT
FROM    HAVING_SUM
GROUP BY A_VARCHAR
HAVING SUM(ISNULL(B_INT, 0)) = 10.5
</cfquery>

结果是

alt text

然后我尝试将SUM(B_INT)(没有ISNULL)和HAVING SUM(B_INT)(没有ISNULL)更改为其他运算符> >= < <= <> !=并且它可以正常工作。

为什么我不能在CF中使用=上面的查询?它是CFbugs吗?

谢谢

1 个答案:

答案 0 :(得分:4)

管理工作室删除空值(运行时检查消息选项卡,您将看到以下内容)

Warning: Null value is eliminated by an aggregate or other SET operation.

coldfusion mssql驱动程序没有。

使用isnull函数是最好的方法。

或者,您可以在cfquery中禁用ansi警告,并在运行后重新启用它们 因此,您可以将查询4更改为

SET ANSI_WARNINGS OFF
SELECT  A_VARCHAR,
        SUM(B_INT) AS B_INT
FROM    HAVING_SUM
GROUP BY A_VARCHAR
HAVING SUM(B_INT)=10.5
SET ANSI_WARNINGS ON