title: Insert/Update to another table using trigger
body: <p>I have 2 tables, tableA and tableB.</p>
  
  <pre><code>tableA:
  id     |     status      |     Flag     |   ResultOfAction   |   ExternalId |  Customer
  1             200               2                 0                332            C1
  2             200               0                 0                333            C1
  </code></pre>
  
  <p>If old.status &lt;> new.status OR Flag = 3 OR ResultofAction = 1, the trigger will populate another table.</p>
  
  <p>e.g</p>
  
  <pre><code>tableB:
  id     |     status      |     Flag     |   ResultOfAction   |   ExternalId | tableAId
  1             200               2                 0                332           1
  </code></pre>
  
  <p>if tableA has new insertion: </p>
  
  <pre><code>tableA:
  id     |     status      |     Flag     |   ResultOfAction   |   ExternalId |  Customer
  1             200               2                 0                332            C1
  2             200               0                 0                333            C1
  3             200               2                 0                334            C1
  </code></pre>
  
  <p>TableB will populate new row automatically.</p>
  
  <pre><code>tableB:
  id     |     status      |     Flag     |   ResultOfAction   |   ExternalId | tableAId
  1             200               2                 0                332           1
  2             200               2                 0                334           3
  </code></pre>
  
  <p>If one of the row in tableA update. e.g. id of 1 to 300, then tableB</p>
  
  <pre><code>tableB:
  id     |     status      |     Flag     |   ResultOfAction   |   ExternalId  | tableAId
  1             300               2                 0                332           1
  2             200               2                 0                334           3
  </code></pre>
  
  <p>Here is my exising Trigger. Anyone can help?</p>
  
  <pre><code>CREATE OR REPLACE TRIGGER TABLEA_TRG AFTER INSERT OR UPDATE ON TABLEA
  REFERENCING OLD AS OLD NEW AS NEW
  FOR EACH ROW WHEN (OLD.STATUS &lt;&gt; NEW.STATUS OR NEW.FLAG = 3 or NEW.RESULTOFACTION = 1)
  BEGIN 
  IF INSERTING THEN
    INSERT INTO TABLEB(TABLEAID, ExternalId, STATUS, Flag)
      SELECT :NEW.ID, :NEW.ExternalId, :NEW.STATUS, :NEW.FLAG FROM DUAL
        WHEN NOT EXISTS (SELECT 1 FROM TABLEB WHERE tableAId = :NEW.id);
  ELSIF UPDATING THEN
    IF :NEW.STATUS &lt;&gt; :OLD.STATUS THEN
      UPDATE TABLEB DWT SET DWT.tableAId = :NEW.id, DWT.ExternalId = :NEW.ExternalId,
        DWT.STATUS = :NEW.STATUS, DWT.Flag = :NEW.Flag;
    END IF;
    MERGE INTO TABLEB D
    USING DUAL ON (D.TABLEAID = :NEW.ID)
    WHEN MATCHED THEN 
    UPDATE SET D.STATUS = :NEW.STATUS
    WHEN NOT MATCHED THEN
    INSERT (D.TABLEAID, D.ExternalId, D.STATUS, D.FLAG) VALUE (:NEW.ID, :NEW.ExternalId, :NEW.STATUS, :NEW.FLAG);
  END IF;
  END;
  /
  </code></pre>
  
  <p>There are errors 
   * Error(7,7): PL/SQL: SQL Statement ignored
   * Error(9,16): PL/SQL: ORA-00933: SQL command not properly ended</p>
  
  <p>This is my first time doing Trigger in Oracle, can someone help me please?</p>