简体   繁体   English

基于目标和源的Oracle合并

[英]Oracle Merge based on Target and source

I need to execute below in oracle but this is not compatible. 我需要在oracle中执行以下操作,但这不兼容。 Basically based on target and source. 基本上基于目标和来源。 Can any one suggest any alternative using SQL only : 谁能仅使用SQL提出任何替代建议:

merge into x as target using y as Source on target.ID = Source.ID
when not matched by target then insert
when matched then update
when not matched by source and target.ID is not null then
update whatevercolumn = 'isdeleted'

The format for a typical merge statement is something along the lines of: 典型的merge语句的格式大致如下:

merge into target_table tgt
using source_dataset src
  on (<join conditions>)
when matched then
  update set tgt.col1 = src.col1,
             tgt.col2 = src.col2, 
             ...
  where <predicates, if required>
when not matched then
  insert (tgt.col1, tgt.col2, ...)
  values (src.col1, src.col2, ...);

You'll have to amend your logic into this format if you want the merge to work. 如果您希望合并有效,则必须将逻辑修改为这种格式。

If you can't do this, please update your question with sample data from the target and source tables, and the outcome you're expecting, along with the logic behind the update you're trying to do. 如果您无法执行此操作,请使用目标表和源表中的示例数据,期望的结果以及您尝试执行的更新背后的逻辑来更新您的问题。


If you need to update records in your target table that don't appear in your source data as well as records that do appear, then you'll need to write a subquery that does a full outer join between both tables and then use that as your source dataset. 如果您需要更新目标表中未出现在源数据中的记录以及出现在实际数据中的记录,那么您需要编写一个子查询来在两个表之间进行完全外部联接,然后将其用作您的源数据集。

Something like: 就像是:

merge into target_table trg
using (select coalesce(srce.target_join_col, targ.source_join_col) join_col,
              coalesce(srce.other_col1, targ.other_col1) other_col1,
              coalesce(srce.other_col2, targ.other_col2) other_col2,
              ...
       from   target_table targ
              full outer join source_table srce on (targ.target_join_col = srce.source_join_col)) src
  on (trg.target_join_col)
when matched then
  update set trg.other_col1 = src.other_col1,
             trg.other_col2 = src.other_col2,
             ...
  where      trg.other_col1 != src.other_col1
  or         trg.other_col2 != src.other_col2
  ...
when not matched then
  insert (trg.target_join_col, trg.other_col1, trg.other_col2, ...)
  values (src.source_join_col, src.other_col1, src.other_col2, ...);

NB untested, since you didn't provide any test case or data for us to work with. NB未经测试,因为您没有提供任何测试用例或数据供我们使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM