I need to create an audit record for table changes. I was reading the following answer: SQL Insert/Update/Delete Trigger Efficiency
My audit table has the following columns:
audit_id
audit_date
audit_user
audit_table_name -> table name being audited
audit_table_id -> id of the row being audited
audit_parent_name -> parent table name if this table is a child
audit_parent_id -> parent id (is a foreign key in this table)
audit_type -> insert, update, delete
audit_fields -> all fields that were affected
The tricky part is that audit_fields has the following format:
column_name + chr(1) + old_value + chr(1) + new_value + chr(10) ...
Can this be achieved with a trigger and how exactly?
EDIT:
I'm basing my trigger on the following answer: SQL Server auto audit updated column
The trigger works fine if I use that same table. However, how can I concatenate the changed fields in the format I need and insert just 1 record in the audit table?