ORACLE Trigger INSERT IN UPDATE

That's it, I'm just trying to create a trigger that selects an entire record from TABLE EMP and inserts it into TABLE EMP_ARCHIVE when trying to UPDATE (As the name implies, the EMP_ARCHIVE table is a history table for storing changes made to the EMP email table). Both tables have the same fields / columns. The following is the trigger I'm trying to create. I know that there is something wrong, but I could not understand. It throws an error in '(' after the INSERT command. Any help would be appreciated. Forgive me if there is any fundamental error, as I am new to them.

CREATE OR REPLACE TRIGGER Save_EMP_Changes
BEFORE UPDATE ON EMP
FOR EACH ROW
BEGIN
   INSERT INTO EMP_ARCHIVE
   (
      emp_id, emp_name,
      emp_age, emp_sex,
      emp_active
   )
   SELECT 
      :old.emp_id, :old.emp_name,
      :old.emp_age, :old.emp_sex,
      :old.emp_active
   FROM EMP 
   WHERE emp_id = :old.emp_id
END;
+3
source share
2 answers

No need to choose from the table:

CREATE OR REPLACE TRIGGER Save_EMP_Changes
BEFORE UPDATE ON EMP
FOR EACH ROW
BEGIN
   INSERT INTO EMP_ARCHIVE
   (
      emp_id, emp_name,
      emp_age, emp_sex,
      emp_active
   )
   VALUES
   (  :old.emp_id, :old.emp_name,
      :old.emp_age, :old.emp_sex,
      :old.emp_active
   );
END;

Btw: Oracle 11 , FLASHBACK ARCHIVE . - .

+6

, . , - , , . .

CREATE OR REPLACE TRIGGER Save_EMP_Changes
BEFORE UPDATE ON EMP
FOR EACH ROW
BEGIN
   INSERT INTO EMP_ARCHIVE
   (
      emp_id, emp_name,
      emp_age, emp_sex,
      emp_active, revision_cnt
   )
   SELECT 
      :old.emp_id, :old.emp_name,
      :old.emp_age, :old.emp_sex,
      :old.emp_active, nvl(max(revision_cnt)+1, 1)
   FROM EMP 
   WHERE emp_id = :old.emp_id
END;

HTH .

0

Source: https://habr.com/ru/post/1795831/


All Articles