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

请教在oracle中怎么批量更新一列记录?待,各位帮帮忙吧…

2012-12-15 
请问在oracle中如何批量更新一列记录?在线等待,各位大虾帮帮忙吧……在oracle数据库中 sys_userdept与sys_de

请问在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 条件要……………………
------其他解决方案--------------------


"set deptid = "后面的SQL必须返回0或1行数据否则报错ORA-01427
如果没有where条件更新的话可以用下面的语句
update sys_userdept
  set deptid = (select max(id) from sys_dept);
-- or
update sys_userdept
  set deptid = (select id from sys_dept where rownum<=1);

[其他解释]
update sys_userdept a
  set deptid = (select id from sys_dept b where a.KEYS=b.KEYS)
  where exists (select 1 from sys_dept b where a.KEYS=b.KEYS) 
-- KEYS 是2个表的连接字段, 使用exists 效率高点
 

[其他解释]
update sys_userdept a
  set deptid = (
      select id from sys_dept b
      where a.col=b.col)

[其他解释]
你等号后面的查询结果,有多条,返回值赋给某行中的一列时。当然就会提示返回多行了。
必须改成一对一对返回结果。,用sys_dept表的主键与sys_userdept中的相关字段进行关联就行了。
[其他解释]
呵呵 学到喽 谢谢大家O(∩_∩)O~
[其他解释]
update sys_userdept a
  set deptid = (select id from sys_dept b 
 
 where a.id=b.id
------------------
这个效率低,100万条需要5分钟;
--------------------------------
merge into sys_userdept su
using sys_dept sd on
(su.col=sd.col)
when matched then
     update
     set su.deptid=sd.id
--------------------------
这个没有试过,下次试下,学习了

热点排行