oracle merge into 优化,ORACLE 10g 的 merge into用法

在Oracle 10g之前,merge语句支持匹配更新和不匹配插入2种简单的用法,在10g中Oracle对merge语句做了增强,增加了条件选项和DELETE操作。下面我通过一个demo来简单介绍一下10g中merge的增强和10g前merge的用法。参考Oracle 的SQL Reference 下面我在一下环境中做一个测试看看

bc7fdfc03a181785f939cc7c3cabcadc.gif

创建表subs  和 acctcreate table subs(

msid     number(9),

ms_type  char(1),

areacode number(3)

);

create table acct(

msid       number(9),

bill_month number(6),

areacode   number(3),

fee        number(8,2) default 0.00

);

插入数据

insert into subs values(905310001,0,531);

insert into subs values(905320001,1,532);

insert into subs values(905330001,2,533);

commit

语法--  语法

merge [into [schema .] table [t_alias]

using [schema .] { table | view | subquery } [t_alias]

on ( condition )

when matched then

merge_update_clause

when not matched then

merge_insert_clause;

测试---  matched:更新    not matched:插入  两者可以同步执行也可以只要一个条件

merge into acct a

using subs b

on (a.msid = b.msid)

when matched then

update set a.areacode = 22

when not matched then

insert (msid, bill_month, areacode) values (b.msid, '200702', b.areacode);

commit

增强条件查询操作merge into acct a

using subs b

on (a.msid = b.msid)

when matched then

update set a.areacode = 22 where b.ms_type = 0

when not matched then

insert (msid, bill_month, areacode) values (b.msid, '200702', b.areacode) where b.ms_type = 0;

commit

增强删除操作merge into acct a

using subs b

on (a.msid = b.msid)

when matched then

update set a.areacode = 22

delete where (b.ms_type != 0);

commit

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值