Loop columns in mysql trigger

Is it possible to scroll through all column names at run time?

Scenario: Record all table columns that have been modified. If some values ​​have not changed, do not register them.

DROP TRIGGER IF EXISTS t_before_update_test; DELIMITER $$ CREATE TRIGGER t_before_update_test BEFORE UPDATE ON test FOR EACH ROW BEGIN -- Loop here for all columns, not just col1 IF OLD.col1 <> NEW.col1 THEN INSERT INTO change_logs( log_on, user_id, table_name, colum_name, old_data, new_data ) VALUES ( UNIX_TIMESTAMP(NOW()), '0', 'test', 'col1', OLD.col1, NEW.col1 ); END IF; -- process looping all columns -- col1, col2, ... should be dynamic per loop END $$ 

This is an example of a working copy, where now I need to skip all the columns available in OLD or NEW.

+6
source share
1 answer

yes, a cursor can be added to a trigger to loop through columns. here are some links:

mysql iterating over column names

https://dba.stackexchange.com/questions/22925/mysql-loop-over-cursor-results-ends-ahead-of-schedule

from experience, it might be easier to create a stored procedure that executes a loop and inserts and calls it from a trigger

-1
source

Source: https://habr.com/ru/post/970858/


All Articles