When a value in your application looks wrong, the first question is who changed it, and when. SQL Server has several built-in ways to keep history, and each answers part of that question. For a business application, the answer to “who” has to be the person signed in to the application, and of the options below only triggers can record it, by reading who the application says is signed in.
The Four Options at a Glance
| Option | What it records | Who made the change | Watch for |
|---|---|---|---|
| Temporal tables | Every earlier version of each row, with the time it was valid | Not recorded; you add your own column | Every update adds a history row, even when nothing changed |
| Change data capture | Inserts, updates and deletes, with before and after values, read from the transaction log | Not recorded | Needs SQL Server Agent; changes appear after a delay and are kept for three days by default |
| SQL Server Audit | Actions, such as who ran a statement against a table | The database login | Records the action, not the before and after values |
| Triggers | Whatever you design: the field, its old and new values, the user and the time | The application’s user, passed in by the application | Written and maintained table by table, unless a tool writes them for you |
The “Who” Problem
A web application usually connects to SQL Server with one login of its own, whoever is using it. So SQL Server’s own features see the application, not the person. Ask the database who changed an invoice and it will name the application’s account every time.
The fix is for the application to tell the database who is signed in. SQL Server provides sp_set_session_context for this: the application sets the user’s name on its connection, and a trigger reads it back with SESSION_CONTEXT as it writes the history row. That is what makes a trigger-based audit trail answer “who”.
What a Good Audit Trail Records
- Field by field: the field, its old value and its new value, with who and when.
- Names, not codes: a changed lookup reads “Dana Price to Lena Ortiz”, not “11 to 12”.
- However the change was made: from screens, imports or scripts, because the triggers sit in the database.
- Sensitive values hidden: an encrypted field is logged as changed without showing its value.
- Large updates summarized, so an import of thousands of rows does not flood the log or slow the save.
- Kept as long as you need, table by table, and cleaned up on a schedule.
Which to Choose
- To answer who changed what, for people or for compliance: triggers that read the application’s user.
- To see a record as it was on a given date: temporal tables.
- To feed changes to another system, such as a data warehouse: change data capture.
- To watch sign-ins and permission changes at the server: SQL Server Audit.
Many applications use two of these together: an audit trail for people, and one of the others for a specific technical need.
Light Speed Framework’s Audit Trail writes the triggers for you: you choose the tables and fields, and every change is recorded field by field, with the person’s name and IP address when it is made in the application. Try a working audit trail.