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!

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

jgarry

Oracle tables are heap tables, there is no intrinsic order.

You can either use the order by statement when you select, or use an index-organized table with an order by.  The correct answer depends on what exactly you intend to do with the table, why you feel there should be an inherent order.

unknown-7404
I was trying to import a large text file to a table.  The text file has more that 1 million lines.  The table has only 1 column to hold one line from the text file.  I made sure that the CLEAR TABLE is checked before I start the process.  Commit every 100.

Oracle has no 'CLEAR TABLE' command so you must be using some tool or GUI front-end you haven't told us about.

If you commit every 100 you will just slow things down so just issue a commit at the end of the data load.

When I checked the resulting table, I noticed that it reoganized the sequence of lines from the text file.

No - Oracle doesn't 'organize' or 'reorganize' the rows. It just dumps them into a table; there is NO first row or last row. Just like if you throw a bunch of tennis balls into a basket you won't know afterward which one was the first ball you threw in.

How can I import from the text file onto a table without reorganizing the data.  I really need the data on this table to be in the same line sequence as the text file.  I also tried commit every 1 record.

Just use my 'tennis ball' example from above. If you want the balls to have an order you need to write a number on each one as you throw it into the basket. Then later you can find ball #1, #37 or sort the balls in order if you want.

For your use case that means add a second column to the table (e.g. LINE_NO) and use an Oracle sequence to 'number' them.

lov2tango

Now I understand.  Thank you very much!

lov2tango

I did used PL/SQL Developer's Text Importer feature.

unknown-7404
I did used PL/SQL Developer's Text Importer feature.

Thanks for sharing but how is that relevant? Your problem is that you need to create your own ordering since the database doesn't create one for you. What tool you use to import the data doesn't really matter unless that tool won't allow you to create an ordering.

1 - 5

Post Details

Added on Sep 17 2021
20 comments
5,241 views