多表批量(关联)更新 oracle、mysql和sqlserver

多表批量(关联)更新
把表A中的name1字段设为对应的B表中的name1字段

1.oracle
update test1 a set name=(select name from test2 b where a.no=b.no) where exists(select name from test2 b where a.no=b.no); --防止插入null值
update test1 a set name=(select name from test2 b where a.no=b.no)

根据已有表创建新的表
CREATE TABLE TAB_NEW (PARTITION ....) AS SELECT * FROM TAB_OLD的方式(考虑并行和NOLOGGING)
create table asm_op_log_1 as select * from asm_op_log t; (复制表不存在)
insert into asm_op_log_2 select * from asm_op_log t; (复制表已经存在)
2.mysql
update table1 a ,table2 b set a.tValue =b.tValue where a.id=b.id

3.sqlserver
update a set a.name1 = b.name1, a.name2=b.name2 from 表A a, 表B b where a.id=b.id

insert into asm_op_log_2 select * from asm_op_log t;(复制表已经存在)
select 字段列表 into 复制表名称 from 表 (复制表不存在)


--------------------------------分割线------------------------------------------


插入指定字段的案例:
insert into wx_rc_contacts(id,name,sex,dept,roles,mobile,email)
(select wx_rc_contacts_seq.NEXTVAL,name,sex,dept,roles,mobile,email from wx_rc_contacts2 t
  where  t.name not in(select name from wx_rc_contacts));

猜你喜欢

转载自aaron7524.iteye.com/blog/1453037
今日推荐