简体   繁体   English

Oracle 合并语句未到达 NOT MATCHED 部分

[英]Oracle merge statement not reaching NOT MATCHED part

The following Merge statement is not Inserting (going to NOT MATCHED) the record.以下 Merge 语句未插入(将不匹配)记录。

The query used in the Condition part of the Merge clause sometimes will not return any row. Merge 子句的 Condition 部分中使用的查询有时不会返回任何行。

Is this causing the Insert to not happen?这是否导致插入不发生?

MERGE INTO apps.test_table ab USING
(SELECT batch_id
FROM apps.test_table
WHERE session_name='session_name'
AND status        ='NOT STARTED'
)b ON (ab.batch_id=b.batch_id)
WHEN MATCHED THEN
  UPDATE
  SET STATUS         ='RUNNING',
    tag_receive_time = sysdate,
    sess_start_time  = SYSDATE,
    wflw_start_time  = sysdate
  WHERE batch_id     = b.batch_id 
WHEN NOT MATCHED THEN
  INSERT
    (
      ab.batch_id,
      ab.status,
      ab.workflow_name,
      ab.session_name,
      ab.source_name,
      ab.target_table,
      ab.created_by,
      ab.creation_date,
      ab.snapshot_date,
      ab.tag_receive_time,
      ab.sess_start_time,
      ab.wflw_start_time
    )
    VALUES
    (
      apps.batch_id_seq.nextval,
      'RUNNING',
      '$PMWorkflowName',
      '$PMSessionName',
      'AMPS',
      'SPARES_F_ORDER_SHIPMENT_DTL',
      'Informatica',
      SYSDATE,
      SYSDATE,
      SYSDATE,
      SYSDATE,
      SYSDATE
    )
    ; 

Breakdown you query to see what results are coming..Run and check the query result from below Select query:细分查询以查看即将出现的结果。运行并检查 Select 查询下方的查询结果:

SELECT batch_id FROM apps.test_table WHERE session_name='session_name' AND 
status ='NOT STARTED'

And the column batch_id result of the above query contains in table apps.test_table or not if not then it should Insert into table apps.test_table else your Update statement will work并且上述查询的batch_id列结果是否包含在表apps.test_table中,如果不是,则它应该插入到表apps.test_table ,否则您的Update语句将起作用

Remove WHERE batch_id = b.batch_id from Update as its not needed.Update中删除WHERE batch_id = b.batch_id ,因为它不需要。

You already check matching for Update statement within ON (ab.batch_id=b.batch_id) , then need to remove WHERE batch_id = b.batch_id after the Update statement.您已经在ON (ab.batch_id=b.batch_id)中检查了 Update 语句的匹配,然后需要在 Update 语句之后删除WHERE batch_id = b.batch_id

You can check this out,as sample syntaxes, where neither of the Update statements contain a where clause您可以检查一下,作为示例语法,其中两个 Update 语句都不包含 where 子句

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

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