oracle 中用一个表的字段update另一个表的字段值怎么做
update (
select a.contact_name cc,
b.owner_contact do
from contctsm1 a, device2m1 b
where substr(b.logical_name, 5, 4) = a.station_id
and a.contact_name <>b.owner_contact
) set do=cc
需要更新多条记录 但是我这样写报错 “无法修改与非键值保存表对应列”
------解决方案--------------------update device2m1 t
set t.owner_contact = (select contact_name
from contctsm1 s
where substr(t.logical_name, 5, 4) = s.station_id
and s.contact_name <> t.owner_contact);
------解决方案--------------------
需要注意的有几点
一 如果select contact_name
from contctsm1 s
where substr(t.logical_name, 5, 4) = s.station_id
and s.contact_name <> t.owner_contact
返回值不唯一,按你自己需要添加其他条件.
二 如果不是要更新owner_contact中所有数据,需要增加where条件对需更新数据进行限定.
------解决方案--------------------可以试试这个:
update device2m1 b
set owner_contact =(SELECT contact_name FROM contctsm1 a ,device2m1 b
WHERE substr(b.logical_name, 5, 4) = a.station_id
and a.contact_name <>b.owner_contact)
------解决方案--------------------
如果你能确定对应关系,还想用上面的写法,可以加注释,强制执行。
SQL code
update (
select /*+ BYPASS_UJVC */
a.contact_name cc,
b.owner_contact do
from contctsm1 a, device2m1 b
where substr(b.logical_name, 5, 4) = a.station_id
and a.contact_name <>b.owner_contact
) set do=cc
------解决方案--------------------
SQL code
--喜欢这种写法,直接方便!
update device2m1 t
set t.owner_contact = (select contact_name
from contctsm1 s
where substr(t.logical_name, 5, 4) = s.station_id
and s.contact_name <> t.owner_contact);