合并查询基于何时与目标oracle SQL不匹配

时间:2016-07-13 05:07:43

标签: sql oracle oracle10g

我想对表格进行合并查询,或者更确切地说是匹配特定条件的表格的子集。所以我正在寻找的是下面的内容 -

merge into table1 as target using table2 as Source on target.col1 = Source.col1
when matched then update
when not matched by target -- row exists in source but not in targer
then insert ;

我理解oracle中不支持when not matched by target。我想知道是否有替代方法在单个合并语句中执行相同的操作。基本上我只想在源表中存在行而不在目标表中时插入。

1 个答案:

答案 0 :(得分:0)

正如其他人指出的那样,BY TARGET可以只针对Oracle删除,并且可以按您期望的那样工作。这里有一个带有两个表的小脚本,显示了此行为及其工作原理。您希望将temp_test中但尚未summary中从temp_testsummary的所有行合并。

create table temp_test 
(
    DT      date,
    Foobar  varchar2(100),
    Step    varchar2(100)
);

create table summary 
(
    DT          date,
    Foobar      varchar2(100),
    Step        varchar2(100),
    OtherFields varchar2(100) NULL
);
/

insert into temp_test values (trunc(sysdate), 'test', '20'); -- should be inserted
insert into temp_test values (trunc(sysdate-1), 'test', '20'); -- already in summary

INSERT into summary (DT, Foobar, Step) VALUES (trunc(sysdate-1), 'test', '20');
select * from temp_test; -- 2 records
select * from summary; -- 1 record


merge into summary TARGET
USING temp_test SOURCE
ON (TARGET.DT = SOURCE.DT and TARGET.Foobar = SOURCE.Foobar and TARGET.Step = SOURCE.Step)
WHEN NOT MATCHED THEN
    INSERT (DT, Foobar, Step) VALUES (SOURCE.DT, SOURCE.Foobar, SOURCE.Step);
-- 1 row updated

select* from gui_po_summary; -- 2 rows

-- try to merge again, nothing should be done
merge into summary TARGET
USING temp_test SOURCE
ON (TARGET.DT = SOURCE.DT and TARGET.Foobar = SOURCE.Foobar and TARGET.Step = SOURCE.Step)
WHEN NOT MATCHED THEN
    INSERT (DT, Foobar, Step) VALUES (SOURCE.DT, SOURCE.Foobar, SOURCE.Step);
-- 0 rows

delete temp_test; -- 2 rows deleted
-- try to merge again, nothing should be done as we want WHEN NOT MATCHED BY target
merge into summary TARGET
USING temp_test SOURCE
ON (TARGET.DT = SOURCE.DT and TARGET.Foobar = SOURCE.Foobar and TARGET.Step = SOURCE.Step)
WHEN NOT MATCHED THEN
    INSERT (DT, Foobar, Step) VALUES (SOURCE.DT, SOURCE.Foobar, SOURCE.Step);
-- 0 rows

还有WHEN NOT MATCHED BY source,它需要对Oracle进行特殊处理,并在https://stackoverflow.com/a/48842932/4040068中得到解答。