一、语义
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 TEMP_CD_CNTR_QUERY A USING CONTAINER B ON (A.CNTR_NUM=B.EQMT_NUM and B.State=‘Active‘) WHEN MATCHED THEN UPDATE SET A.CNTR_NUM_CHECK=CASE WHEN B.CHECK_DIGIT IS NULL OR B.CHECK_DIGIT=‘‘ THEN A.CNTR_NUM ELSE A.CNTR_NUM||B.CHECK_DIGIT END , A.CNTR_TYPE=B.oocl_Cde; --例子二: MERGE INTO TEMP_CD_CNTR_QUERY A USING (SELECT DISTINCT t.TRCKG_GRP_REF_CDE,t.CURRENT_CNTR_NUM,t.CONSLD_PACKG_REF_CDE FROM CGO_PACKAGE t) B ON (A.TRCKG_GRP_REF_CDE=B.TRCKG_GRP_REF_CDE and A.cntr_num = B.CURRENT_CNTR_NUM AND B.CONSLD_PACKG_REF_CDE IS NOT NULL) WHEN MATCHED THEN UPDATE SET A.IS_PATIAL= B.CONSLD_PACKG_REF_CDE;
时间: 2024-10-28 09:17:26