1

What is wrong with below code, can some help me to resolve it?

CREATE OR REPLACE TRIGGER TEST_TRI
   AFTER INSERT
   ON TEST1
   FOR EACH ROW WHEN (NEW.COL2 >= '01-MAY-16')
BEGIN
   IF INSERTING
   THEN
        MERGE INTO  TEST_HIST HIST
        USING  TEST1 T1
        ON (T1.NEW.COL1=HIST.COL2)
        WHEN MATCHED THEN
    UPDATE SET 
        HIST.COL5=NEW.COL5
        WHEN NOT MATCHED
        THEN
      INSERT INTO 
           VALUES (:NEW.COL1,:NEW.COL2,:NEW.COL3,:NEW.COL4,:NEW.COL5);                 
   END IF;    
END;
/

Error: Error(4,3):PL/SQL : Sql stmt ignored Error (12,14) : PL/SQL : ORA-00926: MISSING VALUES KEYWORD

Thanks for the info, i have changed code like below and getting new error.

CREATE OR REPLACE TRIGGER test_tri
   after INSERT
   ON test1
   FOR EACH ROW WHEN (NEW.col5 >= '01-MAY-16')
   DECLARE
   PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
   IF INSERTING
   THEN
        MERGE INTO  test_hist hist
        USING  test1 t1
        ON (t1.PACKAGE_ID=hist.PACKAGE_ID)
        WHEN MATCHED THEN
    UPDATE SET 
        hist.col5=t1.col5
        WHEN NOT MATCHED
        THEN
      INSERT (col1,col2, col3, col4, col5) 
           VALUES (:NEW.col1,:NEW.col2,:NEW.col3,:NEW.col4,:NEW.col5);                 
   END IF;  
   COMMIT;
END;
/

While inserting record in test1 table am getting below error ORA-30926: unable to get a stable set of rows in the source table

Bala S
  • 495
  • 1
  • 6
  • 17

1 Answers1

4

You've got your source data all mixed up in your merge statement, I suspect. You only want to consider the rows being inserted, right?

I think your trigger should be something like:

CREATE OR REPLACE TRIGGER test_tri
   after INSERT
   ON test1
   FOR EACH ROW WHEN (NEW.col5 >= '01-MAY-16')
   DECLARE
   PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
   IF INSERTING
   THEN
        MERGE INTO test_hist hist
        USING (select :new.package_id, :new.col1, :new.col2, :new.col3, :new.col4, :new.col5
               from   dual) t1
          ON (t1.PACKAGE_ID=hist.PACKAGE_ID)
        WHEN MATCHED THEN
          UPDATE SET hist.col5=t1.col5
        WHEN NOT MATCHED THEN
          INSERT (col1, col2, col3, col4, col5)
          VALUES (t1.col1, t1.col2, t1.col3, t1.col4, t1.col5);
   END IF;  
   COMMIT;
END;
/

N.B. if :new.col5 is a date column, then change:

FOR EACH ROW WHEN (NEW.col5 >= '01-MAY-16')

to

FOR EACH ROW WHEN (NEW.col5 >= to_date('01/05/2016', 'dd/mm/yyyy'))

Years have 4 digits (I've guessed that you meant 2016, and not 1916!).

Boneist
  • 22,910
  • 1
  • 25
  • 40
  • Thank you so much, perfectly working. This is my first implementation for merge with trigger. – Bala S Nov 20 '15 at 13:38