oracle中merge into用法解析

本文详细解析了Oracle SQL中的MERGE语句,这是一种高效的数据同步工具,用于更新或插入目标表中的数据。通过示例展示了如何使用MERGE语句根据源表与目标表之间的条件表达式来更新或插入数据,特别适用于批量数据处理场景。

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

语法:

MERGE INTO [target-table] A USING [source-table sql] B ON([conditional expression] and [...]...)
WHEN MATCHED THEN
	[UPDATE sql]
WHEN NOT MATCHED THEN
	[INSERT sql]

含义:

判断B表和A表是否满足ON中条件,如果满足则用B表去更新A表,如果不满足,则将B表数据插入A表但是有很多可选项

例子:

-- 可重复执行
alter table table1 nologging; 
merge into table1 rc
using 
(
select concat(to_char(sysdate,'yyyyMMddHH24mmss'),rownum)id_col,clumn1,clumn2,clumn3,clumn4

 from table2
  where credit_balance is not null and type<>8 and status = 2 and seq_no is not null)all_record
on (rc.seq_no=all_record.seq_pay_no)
when not matched then 
 
insert /*+ append */
(
  RECHARGE_SEQ_NO ,
  clumn1      ,
  clumn2     ,
  clumn3          ,
  clumn4   ,
  RECHARGE_CHANNEL 
)
 values(
   all_record.id_col,
   all_record.clumn1,
   all_record.clumn2,
   all_record.clumn3,
   all_record.clumn4);
   commit;
alter table table1  logging;

详见:https://blog.youkuaiyun.com/jeryjeryjery/article/details/70047022

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值