首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > oracle >

oracle 中用一个表的字段update另一个表的字段值如何做

2012-02-06 
oracle 中用一个表的字段update另一个表的字段值怎么做update (select a.contact_name cc,b.owner_contact

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); 

热点排行