求大师指点下面语句错误原因

update table1 a set a.bal=a.bal+(select b.bal from table2 b where a.no=b.no)预期的期望是刷数据,但是执行完语句之后,金额(bal)字段被改为 null,希望能够得到指点。 数据库版本为DB2 10.1显示全部

update table1 a set a.bal=a.bal+(select b.bal from table2 b where a.no=b.no)


预期的期望是刷数据,但是执行完语句之后,金额(bal)字段被改为 null,希望能够得到指点。 数据库版本为DB2 10.1

收起
参与20

查看其它 2 个回答jmzsz的回答

jmzszjmzsz信息技术经理jzzq

尝试对b.bat 增加 null值判断,应该是 table2中没有对应的数据.

update table1 a set a.bal=a.bal+(select coalesce(b.bal,0) from table2 b where a.no=b.no)

金融其它 · 2016-03-16
  • 两张表之前都没有空值的,
    2016-03-17
  • 刚才试了下,还是不行。 我 写了个程序,我写了个程序,用游标一条条处理就可以了。
    2016-03-17
  • 原因肯定是b 表中没有a 表中对应的记录数或者b 表值为空,只判断是否为空不行的话就再加WHERE条件,用in还是可以一条语句处理的,update table1 a set a.bal=a.bal+(select coalesce(b.bal,0) from table2 b where a.no=b.no) where a.no in (select no from table2)
    2016-03-17
  • 另外 where 语句是可以不要的,只是 coalesce换个位置: update table1 a set a.bal=a.bal+coalesce((select b.bal from table2 b where a.no=b.no),0)
    2016-03-17
  • 才看到你的回复,明天上班我再试试看你的方法。
    2016-03-20
  • 十分感谢你
    2016-03-20

回答者

jmzsz
信息技术经理jzzq

jmzsz 最近回答过的问题

回答状态

  • 发布时间:2016-03-16
  • 关注会员:6 人
  • 回答浏览:855
  • X社区推广