Merge into 语句实例

/*Merge into 详细介绍
MERGE语句是Oracle9i新增的语法,用来合并UPDATE和INSERT语句。
通过MERGE语句,根据一张表或子查询的连接条件对另外一张表进行查询,
连接条件匹配上的进行UPDATE,无法匹配的执行INSERT。
这个语法仅需要一次全表扫描就完成了全部工作,执行效率要高于INSERT+UPDATE。 
*/
/*語法:
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;
*/

语法:

MERGE INTO [your table-name] [rename your table here]

USING ( [write your query here] )[rename your query-sql and using just like a table]

ON ([conditional expression here] AND [...]...)

WHEN MATHED THEN [here you can execute some update sql or something else ]

WHEN NOT MATHED THEN [execute something else here ! ]

 实例:

merge into SP_PD_DEFECT T using dm_device D on(T.DEVICE_ID = D.ID)
when matched then update set T.ALL_DISPATCH_LEVEL = D.DISPATCH_LEVEL,T.DEVICE_CODE = D.DEVICE_CODE;

posted @ 2016-07-20 15:35  RyanChan  阅读(455)  评论(0编辑  收藏  举报