0
点赞
收藏
分享

微信扫一扫

oracle merge into使用笔记(相当于replace into,也可以用于批量insert或update)


oracle中想要实现replace into的效果,但这是mysql中的语法。
那么怎么办呢?
其实不用担心,oracle中也有对应的语法。

使用

示例:

merge into t_main a
using (
select '111' id,'0.05' newAmount from dual
union all
select '222' id,'0.06' newAmount from dual
) c
on(a.id=c.id)
when matched
then update set a.amount=c.newAmount
when not matched
then insert (a.id,a.amount) values (c.id,c.newAmount)

如果只用到update或insert也是可以的,另外一个when不写即可。

效率比where exists快不少

merge into的效率其实很不错,如果涉及到批量update,一种方案是update … where exists,但是实测那种写法效率慢很多。

资料

用法说明

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

判断源表 S 和目标表 T 是否满足 ON 中的条件,如果满足则用 S 表去更新 T 表,如果不满足,则将 S 表数据插入 T 表中。但是有很多可选项,如下:

普通模式
只 update 或者只 insert
无条件 insert 实现
带 delete 的 update


举报
0 条评论