语法:(其中as可以省略)
MERGE INTO table_name AS table_alias
USING (table|view|sub_query) AS alias
ON (join condition)
WHEN MATCHED THEN
UPDATE SET
col1 = col_val1,
col2 = col2_val --9i 不可以有where条件,10g 可以
WHEN NOT MATCHED THEN
INSERT (column_list)—多个列以逗号分割 //可以不指定列
VALUES (column_values); --9i 不可以有where条件,10g 可以
作用:将源数据(来源于实际的表,视图,子查询)更新或插入到指定的表中(必须实
际存在),依赖于on条件,好处是避免了多个insert 和update操作。Merge是一个
目标性明确的操作符,不允许在一个merge 语句中对相同的行insert 或update 操
作。这个语法仅需要一次全表扫描就完成了全部工作,执行效率要高于INSERT
+UPDATE。例子如下:
[quote]
drop table subs;
create table subs(msid number(9),
ms_type char(1),
areacode number(3)
);
drop table acct;
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);
--Oracle advanced sql by dj mailto:dingjunlove@163.com
commit;
merge into acct a --操作的表
using subs b on (a.msid=b.msid)--使用原始数据来源的表,并且制定条件,条件必须有括号
when matched then
update set a.areacode=b.areacode--当匹配的时候,执行update操作,和直接update的语法
不一样,不需要制定表名
when not matched then--当不匹配的时候,执行insert操作,也不需要制定表名,若指定字段插入,则在insert后用括号标明,不指定是全部插入
insert(msid,bill_month,areacode) values(b.msid,'200702',b.areacode);
[/quote]
MERGE INTO table_name AS table_alias
USING (table|view|sub_query) AS alias
ON (join condition)
WHEN MATCHED THEN
UPDATE SET
col1 = col_val1,
col2 = col2_val --9i 不可以有where条件,10g 可以
WHEN NOT MATCHED THEN
INSERT (column_list)—多个列以逗号分割 //可以不指定列
VALUES (column_values); --9i 不可以有where条件,10g 可以
作用:将源数据(来源于实际的表,视图,子查询)更新或插入到指定的表中(必须实
际存在),依赖于on条件,好处是避免了多个insert 和update操作。Merge是一个
目标性明确的操作符,不允许在一个merge 语句中对相同的行insert 或update 操
作。这个语法仅需要一次全表扫描就完成了全部工作,执行效率要高于INSERT
+UPDATE。例子如下:
[quote]
drop table subs;
create table subs(msid number(9),
ms_type char(1),
areacode number(3)
);
drop table acct;
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);
--Oracle advanced sql by dj mailto:dingjunlove@163.com
commit;
merge into acct a --操作的表
using subs b on (a.msid=b.msid)--使用原始数据来源的表,并且制定条件,条件必须有括号
when matched then
update set a.areacode=b.areacode--当匹配的时候,执行update操作,和直接update的语法
不一样,不需要制定表名
when not matched then--当不匹配的时候,执行insert操作,也不需要制定表名,若指定字段插入,则在insert后用括号标明,不指定是全部插入
insert(msid,bill_month,areacode) values(b.msid,'200702',b.areacode);
[/quote]