Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8401689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:52:19+00:00 2026-06-09T21:52:19+00:00

I am writing a trigger to keep track of all the changes that happens

  • 0

I am writing a trigger to keep track of all the changes that happens in a table. Unfortunately the table has 150+ columns and I wanted to avoid writing each column in the code (Ex. new.col1, new.col2….) and thus I wrote a following query in “after update trigger”

INSERT INTO logs SELECT *, NOW() FROM abc WHERE abc.id = NEW.Id;

This idea is causing multiple issue due to duplication of data that is not changed in update query.

In a nutshell I want to dynamically find out which columns were part of the update query and if that is not possible is there a way to iterate through all the columns of “new” row so I can dynamically compare old.@colName == new.@colName?

I have already seen
Oracle PL/SQL: Loop Over Trigger Columns Dynamically, How to determine if anything changed in update trigger in t-sql and MySQL UPDATE trigger: INSERTing the values of the columns that actually changed.

The last link is the closes to what I need with only one difference, I don’t want to hard code column names in following statment because I have way over 100+ columns in all the tables I am going to write similar trigger for!!

IF NEW.column1 <> OLD.column1 THEN INSERT INTO... END IF; IF NEW.column2 <> OLD.column2 THEN INSERT INTO... END IF
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:52:20+00:00Added an answer on June 9, 2026 at 9:52 pm

    I’ve been doing a bit of research on this this morning and looks like I have come across much of the same search results as you. Ultimately it looks to me like there’s no way to loop over all table columns and reference the corresponding old/new values. I’m settling on explicitly checking each column and then logging:

    IF (NEW.fld1 <> OLD.fld1) OR (NEW.fld1 IS NOT NULL AND OLD.fld1 IS NULL) OR (NEW.fld1 IS NULL AND OLD.fld1 IS NOT NULL) THEN
     INSERT INTO `fld_audit` (`table`, `fldname`, `oldval`, `newval`)
     VALUES ("tblname", "fld1", OLD.fld1, NEW.fld1); 
    END IF; 
    
    IF (NEW.fld2 <> OLD.fld2) OR (NEW.fld2 IS NOT NULL AND OLD.fld2 IS NULL) OR (NEW.fld2 IS NULL AND OLD.fld2 IS NOT NULL) THEN
     INSERT INTO `fld_audit` (`table`, `fldname`, `oldval`, `newval`)
     VALUES ("tblname", "fld2", OLD.fld2, NEW.fld2); 
    END IF; ...
    

    I found an inkling of another solution here. In theory you could have 3 delimited lists, one for column names, one for old vals and one for new vals. You would have to explicitly reference the old and new vals, but that would be one line (easier to maintain or copy/paste to implement on other tables) and you could then loop. So in pseudo code it would look something like this:

    fields_array = concat_ws(",", "fld1", "fld2");
    old_vals_array = concat_ws(",", OLD.fld1, OLD.fld2);
    new_vals_array = concat_ws(",", NEW.fld1, NEW.fld2);
    
    foreach fields_array as key => field_name
         INSERT INTO `fld_audit` (`table`, `fldname`, `oldval`, `newval`)
         VALUES ("tblname", field_name, old_vals_array[key], vew_vals_array[key]);
    

    I haven’t thought this through too much. You might need to call into a stored procedure rather than set variables. But it might be worth looking into. I’ve spent enough time on my triggers already. Not sure I could validate (to my boss) trial and error time on a more elegant solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am interested in writing a trigger that would ignore queries from a specific
I am using SQL Server 2000. I am writing a trigger that is executed
When writing a row-level trigger in Oracle, I know that you can use the
I am writing an instead of trigger for updates that does some auditing and
I'm writing a function that I need to use either a TABLE variable for
Is it possible to create a trigger on a table that is initiated when
I am writing a trigger that will execute everytime a student completes an exam
I'm writing a sports app that needs to track the elapsed time of quarter/half/period.
I need to keep a table in sync with a recursive CTE query that
I'm having difficulty writing a trigger that sets the rank column to the max

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.