Skip to content
Advertisement

How to create trigger that will insert into table old and new value on update/insert

I have looked a lot through the internet the past 3 hours but I cannot manage to make the trigger work. Here is my setup:

There is a user USER1 that has granted UPDATE on view_faktura only. Making UPDATE statement on the view will change the underlying faktura table data.

What I want to do is log into another new table any UPDATEs that are done on the view (or underlying table):

Now, I try the trigger:

Can anyone help me with this?

Edit: Changed some quotation marks to ” instead of ‘. “field changed?” I do not know how to refer to this.

Error I get (without any of the fields I do not know how to refer to in double quotations) is:

Error(2,98): PLS-00103: Encountered the symbol “end-of-file” when expecting one of the following: ( begin case declare end exception exit for goto if loop mod null pragma raise return select update while with << continue close current delete fetch lock insert open rollback savepoint set sql execute commit forall merge pipe purge

Advertisement

Answer

Define your log table so that field_changed has a data type and a valid identifier:

Then you can define the trigger, using the :NEW and :OLD records to get the values and an END; statement to terminate the block:

or, as an AUTONOMOUS_TRANSACTION:

Then, after the update, the log table contains:

ID FIELD_CHANGED OLD_VALUE NEW_VALUE FAKTURANR DATE_OF_CHANGE
1 EXTNR 111 21 2 2022-06-21 09:21:16

db<>fiddle here


Or for multiple columns, you can either create a second copy of the trigger and change the column names or use:

db<>fiddle here

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement