一个简单的问题

whatdoyousay 2014-11-10 11:51:32
我有两个table,A和B
A中有a,b,c三个字段,b中有d,f,g三个字段,需求如下:
1,用游标的方式实现
2,如果A.a=B.d ,那么A.b=B.f , A.c=B.g
3,如果有其他方式,请简单说明一下思路
...全文
250 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
bw555 2014-11-10
  • 打赏
  • 举报
回复
还可以考虑使用merge into的方式实现
MERGE INTO A
USING B   
ON (A.a = B.d)   
WHEN MATCHED THEN  
UPDATE  
SET A.b=B.f,A.c=B.g;
卖水果的net 2014-11-10
  • 打赏
  • 举报
回复

-- 一次性更新多列数据,别用 cursor ,太慢了
update A  
set (b,c) = (select f,g from B where A.a = B.d)
where exists(select * from where A.a = B.d)

whatdoyousay 2014-11-10
  • 打赏
  • 举报
回复
create or replace procedure updatemobileandphone as declare cursor c1 is select xm,dh,dh2 from usertemp where 1=1; begin for cursor1 in c1 loop update employeeinfo set employeeinfo.mobile=cursor1.dh employeeinfo.phone=cursor1.dh2 where employeeinfo.employeename=cursor1.xm; end loop; end; 就是不知道哪里错了,请指教
卖水果的net 2014-11-10
  • 打赏
  • 举报
回复

-- 括号中的子查询,只写一个表名,不要把两个表名都写进去。
set (b,c) = (select f,g from B where A.a = B.d)

-- 你给改下面的格式了(这个写法不对)
set (b,c) = (select f,g from A,B where A.a = B.d)

whatdoyousay 2014-11-10
  • 打赏
  • 举报
回复
引用 7 楼 wmxcn2000 的回复:
set 这行,最后多了一个 逗号。
还是一样
卖水果的net 2014-11-10
  • 打赏
  • 举报
回复
set 这行,最后多了一个 逗号。
whatdoyousay 2014-11-10
  • 打赏
  • 举报
回复
引用 4 楼 wmxcn2000 的回复:

-- 一次性更新多列数据,别用 cursor ,太慢了
update A  
set (b,c) = (select f,g from B where A.a = B.d)
where exists(select * from where A.a = B.d)

不知道为什么会报错

update employeeinfo  
set (mobile,phone)=(select dh,dh2 from usertemp  where employeeinfo.employeename=usertemp.xm),
where 
exists(select * from employeeinfo e ,usertemp where e.employeename=usertemp.xm);
但是

merge into usertemp
using employeeinfo 
on (usertemp.xm=employeeinfo.employeename)
when matched then
update 
set usertemp.dh= employeeinfo.mobile, usertemp.dh2=employeeinfo.phone;
测试通过,不知道为什么
whatdoyousay 2014-11-10
  • 打赏
  • 举报
回复
引用 3 楼 bw555 的回复:
还可以考虑使用merge into的方式实现
MERGE INTO A
USING B   
ON (A.a = B.d)   
WHEN MATCHED THEN  
UPDATE  
SET A.b=B.f,A.c=B.g;
谢谢,测试通过
卖水果的net 2014-11-10
  • 打赏
  • 举报
回复

-- 一次性更新多列数据,别用 cursor ,太慢了
update A  
set (b,c) = (select f,g from B where A.a = B.d)
where exists(select * from where A.a = B.d)

17,078

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧