db2如何实现用一张表的某个字段更新另一张表的相应字段。

如题所述

update A a set a.dui_status=( select b.dui_status from B b where b.dui_date=a.dui_date and b.acct_no=a.acct_no )
where a.dui_status= 条件 and 其它限制条件.
这样就只改自己想改的东西,你上面写的是改所有,肯定有没有匹配的记录,注意你不是改所有。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-01-22
db2 "update a set a.字段=(select 字段 from b where 你的条件) where 你的条件"追问

不行啊。update A a set a.dui_status=( select b.dui_status from B b, A a where b.dui_date=a.dui_date and b.acct_no=a.acct_no );报错:Error Code: -811

追答

写错了

update A a set a.dui_status=( select b.dui_status from B b where b.dui_date=a.dui_date and b.acct_no=a.acct_no )

不需要在里面还声明一次表a

追问

还是不行,亲。报同样的错误: [Error Code: -811, SQL State: 21000] DB2 SQL Error: SQLCODE=-811, SQLSTATE=21000, SQLERRMC=null, DRIVER=3.50.152

追答

我知道为什么了,你这个子查询语句查询出来的结果不是唯一的,待修改的列匹配上了不止一个结果,所以导致报错。

追问

对啊,就是这个错误。怎么搞呢,写个存储过程或function行吗?

追答

那就只能用函数或存储过程实现了

追问

函数该如何写啊?我写了个函数,但是执行结果是空的。求指教了啊

本回答被网友采纳
第2个回答  2015-01-21
1.B中有数据 具体条件自己加 UPDATE B SET
相似回答