MySQL db Audit Trail Trigger
I need to track changes (audit trail) on specific tables in MySql Db. I am trying to implement the suggested solution here .
I have an AuditLog table with the following columns: AuditLogID, TableName, RowPK, FieldName, OldValue, NewValue, TimeStamp.
The mysql stored procedure is as follows (this runs fine and creates a procedure):
Calling a procedure such as: CALL addLogTrigger ('ProductTypes', 'ProductTypeID'); executes but does not create any triggers (see image). SHOW TRIGGERS returns an empty set.
Please let me know what the problem might be, or an alternative way to implement this.
DROP PROCEDURE IF EXISTS addLogTrigger;
DELIMITER $
CREATE PROCEDURE addLogTrigger(IN tableName VARCHAR(255), IN pkField VARCHAR(255))
BEGIN
SELECT CONCAT(
'DELIMITER $\n', 'CREATE TRIGGER ', tableName, '_AU AFTER UPDATE ON ', tableName, ' FOR EACH ROW BEGIN ',
GROUP_CONCAT(
CONCAT(
'IF NOT( OLD.', column_name, ' <=> NEW.', column_name, ') THEN INSERT INTO AuditLog (',
'TableName, ',
'RowPK, ',
'FieldName, ',
'OldValue, ',
'NewValue'
') VALUES ( ''',
table_name, ''', NEW.',
pkField, ', ''',
column_name, ''', OLD.',
column_name, ', NEW.',
column_name,
'); END IF;'
)
SEPARATOR ' '
), ' END;$'
)
FROM
information_schema.columns
WHERE
table_schema = database()
AND table_name = tableName;
END$
DELIMITER ;
alt text http://pssnet.com/~devone/pssops3/testing/callprocedure.png
a source to share
I think you will find that this stored procedure does not create triggers; it creates SQL statements to create triggers. Pipe the output from this procedure into a file somewhere and then run it.
Looking at the output, some vertical stripe spurious characters appear in it, which can cause problems; my vision is not what it can be, so I cannot be sure.
a source to share