Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Generic Trigger to log changes

mail8mzSep 17 2021 — edited Sep 17 2021

Hello,
I am trying to use trigger to log the change of tables. However, it seems I have to hard code the column name and column value. Is there any generic solution regardless of the table structure?

Table structure:

CREATE table base_table ( id number(10), nm varchar2(10) );
CREATE table log_table ( id number(10), nm varchar2(10), logTs timestamp(6));

Table specific trigger works fine:

CREATE OR REPLACE TRIGGER trigger_spfc
  BEFORE UPDATE ON base_table
  FOR EACH ROW
BEGIN
    INSERT INTO log_table( id, nm, logTs)
      VALUES( :old.id, :old.nm, systimestamp);
 END;

I tried to make a more generic trigger using primary key column(eg, id) only. So that I only need hard code the primary key column, but not all columns in the table.


CREATE OR REPLACE TRIGGER trigger_generic
  BEFORE UPDATE ON base_table
  FOR EACH ROW
BEGIN
    INSERT INTO log_table( id, nm, histTS)
    SELECT id, nm, systimestamp from base_table 
    WHERE ID = :OLD.id;
 END;

Now I have "ORA-04091, table is mutating" error.
Any suggestion is welcome, thanks!

This post has been answered by Jan Gorkow on Sep 17 2021
Jump to Answer

Comments

Post Details

Added on Sep 17 2021
20 comments
5,007 views