ColdFusion MsSQL HAVING SUM() 带有 NULL 值

发布于 2024-10-10 09:29:13 字数 2828 浏览 5 评论 0原文

我使用 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 条记录,但在 Exception 部分 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 中使用上面的 = 查询?是CFbug吗?

谢谢

I tested this with CF 9 and 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)

and then query in 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

the result is

alt text

then I tried using 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#">

and the result is

alt text
alt text

Edit (start here):
scroll down to debugger I've got this

alt text

that red area indicated that qryHavingSum_4 is executed, with 0 record, but in the Exception section qryHavingSum_4 is undefined (the <cfdump var="#qryHavingSum_4#">)
Edit (end here)

if I change qryHavingSum_4 to

<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>

the result is

alt text

and then I tried to change SUM(B_INT) (without ISNULL) and HAVING SUM(B_INT) (without ISNULL) with other operator > >= < <= <> != and it works.

why I can't use the = with query above in CF? is it CFbugs?

thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七颜 2024-10-17 09:29:13

Management studio 会删除空值(当您运行它时,检查消息选项卡,您将看到以下内容),

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

Management studio strips out the nulls (when you run it check the messages tab and you'll see the following )

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

The coldfusion mssql driver does not.

Using the isnull function is the best approach.

Alternatively you can disable ansi warnings in your cfquery and re-enable them after they run
So you can change query 4 to

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文