1、基本语法
MERGE INTO 表名
USING (子查询)
ON (主键连接)
WHEN MATCHED THEN
更新、删除操作
WHEN NOT MATCHED THEN
更新、删除、插入操作
2、使用实例
merge into hdc_dim.dim_department a
using (
select
t.deptcode as DEPTCODE,
t.deptname as DEPTNAME,
nvl(t.deptclass, '6') as DEPT_CLASS,
t.wardcode as WARDCODE,
t.deptcode_case as DEPTCODE_CASE,
t.deptname_case as DEPTNAME_CASE,
t.deptcode_account as DEPTCODE_ACCOUNT,
t.deptname_account as DEPTNAME_ACCOUNT,
t.deptcode_standard as DEPTCODE_STANDARD,
t.deptname_standard as DEPTNAME_STANDARD,
t.sign_void as SIGN_VOID,
sysdate as CREATE_TIME,
sysdate as UPDATE_TIME
from department t
) b
on (a.deptcode = b.deptcode )
when matched then
update
set a.DEPTNAME = b.DEPTNAME,
a.DEPT_CLASS = b.DEPT_CLASS,
a.WARDCODE = b.WARDCODE,
a.DEPTCODE_CASE = b.DEPTCODE_CASE,
a.DEPTNAME_CASE = b.DEPTNAME_CASE,
a.DEPTCODE_ACCOUNT = b.DEPTCODE_ACCOUNT,
a.DEPTNAME_ACCOUNT = b.DEPTNAME_ACCOUNT,
a.DEPTCODE_STANDARD = b.DEPTCODE_STANDARD,
a.DEPTNAME_STANDARD = b.DEPTNAME_STANDARD,
a.SIGN_VOID = b.SIGN_VOID,
a.UPDATE_TIME = b.UPDATE_TIME
when not matched then
insert
(
DEPTCODE,
DEPTNAME,
DEPT_CLASS,
WARDCODE,
DEPTCODE_CASE,
DEPTNAME_CASE,
DEPTCODE_ACCOUNT,
DEPTNAME_ACCOUNT,
DEPTCODE_STANDARD,
DEPTNAME_STANDARD,
SIGN_VOID,
CREATE_TIME,
UPDATE_TIME)
values
(
b.DEPTCODE,
b.DEPTNAME,
b.DEPT_CLASS,
b.WARDCODE,
b.DEPTCODE_CASE,
b.DEPTNAME_CASE,
b.DEPTCODE_ACCOUNT,
b.DEPTNAME_ACCOUNT,
b.DEPTCODE_STANDARD,
b.DEPTNAME_STANDARD,
b.SIGN_VOID,
b.CREATE_TIME,
b.UPDATE_TIME)