There are total four solutions found in SQL Server, following is the detail:
- SQL Server Change Tracking [CTC]
- SQL Server Change Data Capture [CDC]
- SQL Server Audit Trail with Triggers [Generic - Manual]
- SQL Server Audit
SQL Server Change Tracking [CTC]
Pros
- Works on all SQL Server editions
- DML Triggers and additional tables are not required, as single temp
table is created by CTC named CHANGETABLE
- Minimal disk space costs
- Packaged functions available to query the data
- Configurable retention policy and Auto clean-up of CHANGETABLE, Also
auto clean-up can be turned off when needed
- No need to access LDF file of that particular database
- Purging/truncation is possible when CTC enabled
Cons
- Need to enable on both database and each required table
- Synchronous tracking mechanism
- Tracking data of all tables is stored in single temp table named
CHANGETABLE
- Cannot get historical data also not give exact detail about previous
and new data in tracking columns
- Composite keys support issue
- Requires primary key
- Only maintains current state of the data w.r.t operation I = Insert,
U = Update, D = Delete
- SQL Server user group 'sysadmin' can only enable CTC
SQL Server Change Data Capture [CDC]
Pros
- This features provide a table-by-table solution, after enabling on
database level.
- Efficient & fast, asynchronous tracking mechanism
- Can get historical data, also able to give exact detail about
previous and new data in shadow table
- DML triggers are not required, as new table is created by CDC with
prefix 'dbo_' and suffix '_CT' for each table.
- All CDC related tables & functions will be referred with 'cdc'
schema.
- All CDC related stored procedures will be referred with 'sys' or
'cdc' schema
Cons
Information about the user who made the change isn't captured, for
that you may need to create some extra columns existing each table
desired to be tracked, the columns are like:
CreatedAt datetime default (getdate()),
CreatedBy nvarchar(100) default (suser_sname()),
UpdatedAt datetime default (getdate()),
UpdatedBy nvarchar(100) default (suser_sname())
- SQL Server Agent should be enabled for CDC, also CDC can't work
properly, when the Database Engine service or the SQL Server Agent
service is running under the NETWORK SERVICE account
- Need to enable on both database and each required table
- Works only on Enterprise, Developer and DataCenter editions
- Need to access LDF file of that particular database
- SQL Server Agent should be enabled to capture the data
- Purging/truncation is not possible on main table when CDC enabled,
Auto clean-up on shadow table can't be turned off once enabled
- Extra space is required for transaction log (LDF file)
- Db's user group 'db_owner' or server's role 'sysadmin' can only
enable CDC
- There are limitations when tracking changes in columns containing
XML, Sparse, Timestamp, CLOB and BLOB data types
- When the caller does not have permission to view the source data, the
function returns error 229
Audit Trail with Triggers [Generic Solution]
Pros
- Can get historical data, also able to give exact detail about
previous and new data in shadow table
- Well controlled w.r.t selection of columns, operations [I=Insert,
U=Update, D=Delete]
- Process can be made automated due to complexity, Audit Trail Process
can be govern with some routine or service.
- Purging/truncation as well as auto clean-up can also be automated
Cons
- Exact copy of table with some extra columns is required as Shadow
Tables
- Complex process w.r.t development, data management and purging
- Performance hit when data becomes huge in shadow tables, Purging is
required
- Takes much time to automate the process considering all the important
aspects of reliability, security and performance. Which in native
solution, DB Engine provides by default.