有关在SQL的子查询中出现NULL影响计算的问题.

sp_gen="select spg1-spg2+spg3 as spg from ("
sp_gen=sp_gen&"select sum(成品根数)+sum(试验品根数) as spg1 from 成型 where 日期>'"&mon&"-25' and 规格='"&rs("规格")&"') ,("
sp_gen=sp_gen&"select sum(装炉根数) as spg2 from 焙烧 where 装炉时间 between '"&mon&"-25' and '"&date()&"' and 规格='"&rs("规格")&"' and 类型='生坯' and 等级='A' and 等级='S') ,("
sp_gen=sp_gen&"select sum(生坯根) as spg3 from 数据临时表 where 规格='"&rs("规格")&"' and 其它='不能动' ) "

请大家帮我看看, 在FROM后面出来的 spg2 spg3 都是 null, 有没有办法在这里面让他们如果是 null 的话, 自动转为 0 ???

我最好把它们集中到一个SELECT中, 因为不这样, 我得建立非常多的 recordset 对象, 太可怕了. 可能会有数百个.

很急, 谢谢.
---------------------------------------------------------------

用ISNULL(spg2,0)
ISNULL(spg3,0)取代
spg2,spg3
---------------------------------------------------------------

如果是SQL SERVER 的话可以用函数IsNull()

ISNULL
使用指定的替换值替换 NULL。

语法
ISNULL ( check_expression , replacement_value )

参数
check_expression

将被检查是否为 NULL的表达式。check_expression 可以是任何类型的。

replacement_value

在 check_expression 为 NULL时将返回的表达式。replacement_value 必须与 check_expresssion 具有相同的类型。

返回类型
返回与 check_expression 相同的类型。

注释
如果 check_expression 不为 NULL,那么返回该表达式的值;否则返回 replacement_value。

--------------------------------------
● 曾经沧海难为水 ╭○╮
<■> 除却巫山不是云 <□\
√ ¦ ¦↘
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓

---------------------------------------------------------------

ISNULL ( check_expression , replacement_value )

Published At
Categories with Web编程
Tagged with
comments powered by Disqus