简体   繁体   中英

How can we write trigger to insert distinct values from one table to another table in Oracle?

Say table1, table2 are 2 tables. Whenever there is an insert/update in the table1, the table2 has to update accordingly.

Please help me in this?

Thanks in advance.

You may try this:

-- insert/update command on table1
create or replace trigger TriggerName before insert or update on table1 for each row
    BEGIN
        -- Write inset/update command on table2
    END;
/

You can use the MERGE statement. Try something like:

CREATE OR REPLACE TRIGGER TABLE1_AIU
  AFTER INSERT OR UPDATE ON TABLE1
  FOR EACH ROW
BEGIN
    MERGE INTO TABLE2 t2
      USING (SELECT :NEW.VALUE1 FROM DUAL) n
        ON (t2.VALUE1 = n.VALUE1)
      WHEN NOT FOUND THEN
        INSERT (VALUE1)
          VALUES (n.VALUE1);
END;

Best of luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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