Oracle存储过程迁移ODPS-01(专有云):支持DML(delete/update/merge)SQL

本文详细介绍了如何使用ODPS SQL进行全量表与增量表的数据同步操作,包括更新、删除以及合并策略。通过具体SQL语句示例,展示了如何实现数据的高效同步与管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

--关系型数据库支持的 delete/update/merge SQL ,在ODPS该如何写

-- 上日全量表
table1(key1 string,key2 string,col1 string,col2 string);
-- 今日增量表
table2(key1 string,key2 string,col1 string,col2 string);
-- 今日增量表(删除)
table3(key1 string,key2 string,col1 string,col2 string);

--1.update(table2 表中的记录的值,更新到table1表中)
insert overwrite table table1
select t1.key1
      ,t1.key2
      ,case when t2.key1 is not null then t2.col1 else t1.col1 end as col1
      ,case when t2.key1 is not null then t2.col2 else t1.col2 end as col2
  from table1 t1
  left outer join table2 t2 on t1.key1=t2.key1 and t1.key2 = t2.key2
;

--2.delete(table2 表中的记录,从table1表中删除)
insert overwrite table table1
select t1.key1
      ,t1.key2
      ,t1.col1
      ,t1.col2
  from table1 t1
  left outer join table2 t2 on t1.key1=t2.key1 and t1.key2 = t2.key2
 where t2.key1 is null
;
--3.merge(没有del)
insert overwrite table table1
select 
  from(
-- 先把上日存在,今日也存在的记录从上日表中排除。剩下的就是今日没有更新的记录
select t1.key1
      ,t1.key2
      ,t1.col1
      ,t1.col2
  from table1 t1
  left outer join table2 t2 on t1.key1=t2.key1 and t1.key2 = t2.key2
 where t2.key1 is null
 union all
-- 再合并上今日增量,就是今天的全量
select t2.key1
      ,t2.key2
      ,t2.col1
      ,t2.col2
  from table2 t2)tt
;

--2.merge(有del)
insert overwrite table table1
select 
  from(
-- 先把上日存在,今日也存在的记录从上日表中排除,再把今日删除的记录排除。剩下的就是今日没有更新的记录
select t1.key1
      ,t1.key2
      ,t1.col1
      ,t1.col2
  from table1 t1
  left outer join table2 t2 on t1.key1=t2.key1 and t1.key2 = t2.key2
  left outer join table3 t3 on t1.key1=t3.key1 and t1.key2 = t3.key2
 where t2.key1 is null or t2.key1 is null
 union all
-- 再合并上今日增量,就是今天的全量
select t2.key1
      ,t2.key2
      ,t2.col1
      ,t2.col2
  from table2 t2)tt
;

-- 暮角 15901445705 update at 20181203 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值