我有一个表DOMAINS
中的列2点不同的模式ID
,NAME
,CODE
,DESCRIPTION
。
对于NAME
新模式中存在的任何内容,它应使用现有模式ID
而无需任何合并;对于这些新NAME
记录,它应该ID
从旧模式插入。
MERGE INTO DOMAINS A
USING (SELECT ID,NAME,CODE,DESCRIPTION FROM <Old Schema 6.1>.DOMAINS@DB_MIG_61_TO_74) B
ON(A.NAME = B.NAME)
WHEN MATCHED **<do nothing>**
WHEN NOT MATCHED THEN INSERT(A.ID,A.NAME,A.CODE,A.DESCRIPTION)
VALUES(B.ID,B.NAME,B.CODE,B.DESCRIPTION);
我如何解释do nothing
以上查询中的部分?
对于您的情况,无需使用以下部分:
when matched then update ...
(usingwhen matched then update set a.id = a.id
被接受(Oracle不会抛出错误),但没有影响,因此,这种用法是多余的,因为您不想为匹配的情况更改任何内容。)
如果要更改,请添加
when matched then update set a.id = b.id
之前
when not matched then insert...
(例如,Oracle支持
when matched then update
语法。请参见下面的演示)
对于当前情况,继续执行以下操作:
SQL> create table domains( id int, name varchar2(50), code varchar2(50), description varchar2(50));
SQL> insert into domains values(1,'Domain A','D.A.','This is Domain A');
SQL> merge into domains A
using
(select 2 id, 'Domain A' name, 'D.A.' code, 'This is Domain A' description from domains) b
on ( a.name = b.name )
when not matched then insert( a.id, a.name, a.code, a.description )
values( b.id, b.name, b.code, b.description );
SQL> select * from domains;
ID NAME CODE DESCRIPTION
-- -------- ----- ----------------
1 Domain A D.A. This is Domain A
SQL> delete domains;
SQL> insert into domains values(1,'Domain A','D.A.','This is Domain A');
-- we're deleting and inserting the same row again
SQL> merge into domains A
using
(select 2 id, 'Domain B' name, 'D.B.' code, 'This is Domain B' description from domains) b
on ( a.name = b.name )
when not matched then insert( a.id, a.name, a.code, a.description )
values( b.id, b.name, b.code, b.description );
ID NAME CODE DESCRIPTION
-- -------- ----- ----------------
1 Domain A D.A. This is Domain A
2 Domain B D.B. This is Domain B
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句