日期:2014-05-19  浏览次数:20941 次

ACCESS字段中有空值.用SQL如何解决
如下语句:
INSERT   INTO   table1   (   单位名称,   货物名称,   月份,   数量   )
SELECT   单位名称,   货物名称,   月份,   入库数量   -   出库数量   +   (SELECT   数量   from   table1   where   (table1.单位名称   =   table12.单位名称)   FROM   table12

因为   (SELECT   数量   from   table1   where   (table1.单位名称   =   table12.单位名称)
这句SQL   有可以选出空值.导致了  

入库数量   -   出库数量   +   (SELECT   数量   from   table1   where   (table1.单位名称   =   table12.单位名称)   的值也为空了。

请问如何解决这个问题呢..
用过   (SELECT   nz(数量,0)   from   table1   where   (table1.单位名称   =   table12.单位名称)也不行..ISNULL也不行.返还的还是空值

个位帮帮忙呀   ...数据库为ACCESS

------解决方案--------------------
case when ..then..else..end
------解决方案--------------------
因为 (SELECT 数量 from table1 where (table1.单位名称 = table12.单位名称)
这句SQL 有可以选出空值.导致了 什么意思
什么值是空的??
table1.单位名称,table12.单位名称,数量或者其他什么值会空???

用过 (SELECT nz(数量,0) from table1 where (table1.单位名称 = table12.单位名称)也不行..ISNULL也不行.返还的还是空值
nz(数量,0)是数据表几的指定下 table1.nz(数量,0)/table2.nz(数量,0)
另外table1.单位名称 = table12.单位名称 对应的单位是不是完全统一的名字包括单位名字前后的空格数是否一致 都可能让条件完全为假
------解决方案--------------------
入库数量 - 出库数量 + ISNULL((SELECT iff(isnull(数量),0,数量) from table1 where (table1.单位名称 = table12.单位名称))

不是用在相加后的,要用在可能有空值的字段上。
------解决方案--------------------
先处理一下空的可以用NULL替换一下