ColdFusion MsSQL HAVING SUM() 带有 NULL 值
我使用 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
结果是
然后我尝试使用 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#">
,结果是
编辑(从这里开始): 向下滚动到调试器我得到了这个
红色区域表明 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>
结果是
然后我尝试使用其他运算符 > 更改
就可以了。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
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
Edit (start here):
scroll down to debugger I've got this
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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Management studio 会删除空值(当您运行它时,检查消息选项卡,您将看到以下内容),
但 Coldfusion mssql 驱动程序不会。
使用 isnull 函数是最好的方法。
或者,您可以在 cfquery 中禁用 ansi 警告,并在运行后重新启用它们
所以你可以将查询 4 更改为
Management studio strips out the nulls (when you run it check the messages tab and you'll see the following )
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