MERGE [hint] 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;
代码及说明
1.MERGE INTO test T1
2.USING (SELECT OWNER , OBJECT_NAME , MAX(ID) ID FROM T GROUP BY OWNER, OBJECT_NAME) T
3.ON (T.OWNER = T1.OWNER AND T.OBJECT_NAME = T1.TABLE_NAME)
4.WHEN MATCHED THEN UPDATE SET T1.ID = T.ID
5.WHEN NOT MATCHED THEN INSERT VALUES (T.ID, T.OWNER, T.OBJECT_NAME);
第一行 命名目标表并给别名T1
第二行 using子句提供merge操作的数据源,命名T
第三行 on子句指定合并的条件
第四行 when matched then 子句判定条件符合则对表T1做什么改变(或删除)
第五行 when not matched then 子句判断条件不符合则插入的操作
MERGE INTO 实现插入更新,作用与Kettle 插入更新效果相同。
MERGE INTO test_B T1
USING (SELECT * FROM test_A) T2
ON (T1.T_NAME = T2.T_NAME)
WHEN MATCHED THEN
UPDATE SET T1.T_COMMENT = T2.T_COMMENT, T1.T_KEY_COL = T2.T_KEY_COL
WHEN NOT MATCHED THEN
INSERT
(T_NAME, T_COMMENT, T_KEY_COL)
VALUES
(T2.T_NAME, T2.T_COMMENT, T2.T_KEY_COL);