In my SQL Server backend for my app, I want to create history tables for a bunch of my key tables, which will track a history of changes to the rows.
My entire application uses Stored Procedures, there is no embedded SQL. The only connection to the database to modify these tables will be through the application and the SP interface. Traditionally, shops I’ve worked with have performed this task using triggers.
If I have a choice between Stored Procedures and Triggers, which is better? Which is faster?
Triggers.
We wrote a GUI (internally called Red Matrix Reloaded) to allow easy creation/management of audit logging triggers.
Here’s some DDL of the stuff used:
The AuditLog table
Trigger to log inserts
Trigger to log Updates
Trigger to log Delete
And in order to know which user in the software did the update, every connection ‘logs itself onto SQL Server’ by calling a stored procedure:
Notes
sometimes the ‘OldValue’ and ‘NewValue’ values are written as a sub-select – to get a meaningful string. i.e.’
OldValue: {233d-ad34234..} NewValue: {883-sdf34…}
is less useful in the audit trail than:
Final note: Feel free to not do what we do. This is great for us, but everyone else is free to not use it.