How to know which columns were modified in an UPDATE using TRIGGER?

Asked

Viewed 55 times

1

Good afternoon, everyone.

I am creating a TRIGGER AFTER UPDATE in a table in the BD, and I need to know which columns were changed (regardless of the value that was placed), and put the column names in a variable that will be inserted in another table.

The problem is that it is a table with many columns, and it would be very ugly to check column by column in this way:

IF (NEW.Column!= OLD.Column) THEN

    SET @ColunasAlteradas = CONCAT(@ColunasAlteradas, ', NomeColunaAlterada');

END IF;

I have the idea to do something crazy and use STMT and CURSOR in the COLUMNS table of information_schema, but if you have a function to do this check in a TRIGGER would help too.

Thanks in advance!

  • What are you trying to do? Does that sound like a log to me? Make sure that doesn’t help you...https://answall.com/questions/143720/crea-trigger-update-mysql

1 answer

3

I think you’ll have to do the checks one by one, for example:

@IF (OLD.column != NEW.column) SET `outraColuna` = OLD.outraColuna + 1;

Something like that

Browser other questions tagged

You are not signed in. Login or sign up in order to post.