oracle 更新语句merge into 的使用方法

 

MERGE(合并)

英文是Oracle上数据库SQL参考查询

Purpose 目的:

Use the MERGE statement to select rows from one or more sources for update or insertion into a table or view. You can specify conditions to determine whether to update or insert into the target table or view.

使用MERGE语句选择行从一个或多个源更新或插入一个表或视图。您可以指定条件来决定是否更新或插入到目标表或视图。

This statement is a convenient way to combine multiple operations. It lets you avoid multiple INSERTUPDATE, and DELETE DML statements.

这句话是一个方便的方式将多个操作。它可以让你避免多个INSERT、UPDATE和DELETE DML语句。

MERGE is a deterministic statement. That is, you cannot update the same row of the target table multiple times in the same MERGE statement.

合并是一个决定性的声明。也就是说,你不能更新相同的目标表行多次在同一MERGE语句。

Note:

Oracle Database does not implement fine-grained access control during MERGE statements. If you are using the fine-grained access control feature on the target table or tables, use equivalent INSERT and UPDATE statements instead of MERGE to avoid error messages and to ensure correct access control.

Oracle数据库在MERGE语句没有实现细粒度的访问控制。如果你使用细粒度访问控制功能在目标表或表,使用等价的INSERT和UPDATE语句而不是合并,以避免错误信息并确保正确的访问控制。

Prerequisites(先决条件)

You must have the INSERT and UPDATE object privileges on the target table and the SELECT object privilege on the source table. To specify the DELETE clause of the merge_update_clause, you must also have the DELETE object privilege on the target table.

你必须有目标表上的插入和更新对象特权和特权在源表的选择对象。merge_update_clause的指定删除子句,你还必须对目标表删除对象特权。

Syntax语法

 

 

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

在两个服务器之间,我做了一个DBLINK,A库获取B库的数据就是用的MERGE INTO 

两个库之间的数据同步的话用起来比较方便 ,相同就更新某些个字段,不相同就插入数据

--获取 JT_CUSTOMER 数据
MERGE INTO JT_CUSTOMER B USING AB01@DBLINK_112 C ON (B.LOGID =C.ZA0100) --从CENTER将表MERGE到BRANCH,同步的依据是两个表的PK
when matched then
UPDATE SET b.shopgrade = c.za0107 , b.turndate = c.za0109
WHEN NOT MATCHED THEN --如果PK值不一至,则将源表中的数据整条插入到目标表中
INSERT (

               LOGID ,
          CUSTOMERNO ,
     shopgrade ,
     REGDATE ,
     TURNDATE ,
     ISTG ,
     TGGRADE ,
     TGTURNDATE ,
     countrynumber
    )
VALUES (
     ZA0100 , --CUSTOMERID
          ZA0102 , --CUSTOMERNO
    za0107 ,--shopgrade
    ZA0108 , --REGDATE
    ZA0109 , --TURNDATE
    ZA0110 , --ISTG
    ZA0112 , --TGGRADE
    ZA0111 , --TGDATE
    za0118
    );
COMMIT;

 

--用来更新速度也比较快。只是不知道在资源消耗上,占用内存上有什么和UPDATE 的差别

merge into jt_customer c
using
(
select ab.za0100 LOGID, af.za0102 USERNAME

from ab01@dblink_112 ab , af01@dblink_112 af
where ab.za0103 = af.za0100
)T
on (c.logid = T.LOGID)
WHEN MATCHED THEN
UPDATE SET C.USERNAME = T.USERNAME
;

posted @ 2018-01-19 15:40  CreamKay  阅读(345)  评论(0)    收藏  举报