Search code examples
oracle-databaseplsqltriggers

PL/SQL Trigger to update another table only upon INSERT of specific record on one table


Referred this answer that update triggers on every insert on tableA.


Scenario is,

In tableA, we have WATERMARK_FLAG column of type CHAR(1) with possible values Y or N

In tableB, we have PROCESSED_FLAG column of type CHAR(1) with possible values Y or N

There is no primary key - foreign key relation between two tables

Primary key columns are same in both tables

PK(col_1, col_3, col_4)


On INSERT of record in tableA with WATERMARK_FLAG as Y update PROCESSED_FLAG with Y in all rows of tableB

How to provide PL/SQL trigger syntax for specific INSERT?


Solution

  • Would be this one

    CREATE OR REPLACE TRIGGER bi_tableA
      BEFORE INSERT ON bi_tableA
      FOR EACH ROW
      WHEN (WATERMARK_FLAG = 'Y')
    DECLARE
    BEGIN
      UPDATE tableB
      SET PROCESSED_FLAG = 'Y';
    END bi_tableA;
    /
    

    However, the performance could decrease. But I assume this is just an exercise.