日期:2014-05-17  浏览次数:20890 次

请问在oracle中如何批量更新一列记录?在线等待,各位大虾帮帮忙吧……
在oracle数据库中 sys_userdept与sys_dept两个表,想把sys_userdept.id的值更新为sys_dept.id(整列数据)

update  sys_userdept
   set deptid = (select id from sys_dept)


报错:ORA-01427: 单行子查询返回多个行


请帮帮修改一下语句!~


简而言之:查询一个表的一列值,更新到另一个表的一列值
------最佳解决方案--------------------

--批量更新
--1.使用merge into:高效
merge into sys_userdept su
using sys_dept sd on
(su.col=sd.col)
when matched then
     update
     set su.deptid=sd.id
--2.,一般的更新,col为两表的连接属性
update sys_userdept su
  set deptid = (
      select id from sys_dept sd
      where su.col=sd.col)

------其他解决方案--------------------
引用:
SQL code

--批量更新
--1.使用merge into:高效
merge into sys_userdept su
using sys_dept sd on
(su.col=sd.col)
when matched then
     update
     set su.deptid=sd.id
--2.,一般的更新,col为两表的连接属性
update sys……

+1
------其他解决方案--------------------
update sys_userdept a
  set deptid = (select id from sys_dept b where a.KEYS=b.KEYS)

KEYS是主键
------其他解决方案--------------------
看到书中也有这样写的,可能更新的会保险一些吧~~

update sys_userdept a
  set deptid = (select id from sys_dept b where a.KEYS=b.KEYS)
  where exists (select id from sys_dept b where a.KEYS=b.KEYS)
------其他解决方案--------------------
--做一下连接查询就可以了
update sys_userdept a
  set deptid = (select id from sys_dept b 

 where a.id=b.id
 )

------其他解决方案--------------------
顶起
引用:
SQL code

--批量更新
--1.使用merge into:高效
merge into sys_userdept su
using sys_dept sd on
(su.col=sd.col)
when matched then
     update
     set su.deptid=sd.id
--2.,一般的更新,col为两表的连接属性
update sys_userdept……

------其他解决方案--------------------
update sys_userdept a
  set deptid = (select id from (select id from sys_dept group by id) b
where a.id=b.id)
where exists
(select 1 from (select id from sys_dept group by id) b
where a.id=b.id 
)
应该是sys_dept表中存在多条记录与sys_userdept 对应
------其他解决方案--------------------
select id from sys_dept  是不是多条记录了, 更新数据集不是这样写的
where 条件要……………………
------其他解决方案--------------------