DDL and DML Capture

Babawale Dawodu 110 Reputation points
2025-10-09T10:57:00.12+00:00

Hello Community,

I am wondering if there's an already established way to track and log all TRUNCATE, INSERTS AND DELETES statements across SQL Server. I already have a DDL trigger process that logs every DDL event on the server, specifically for CREATE, ALTER, DROP, and GRANT. But to extend my process, I need a tracker for Truncates, Insert and Delete. From my research it seems like Extended Events may be the only way possible as I have tried other methods. Yet to try XE but wanted to find out from the community if anyone knows of a proven process yet before wasting even more time on XE.

Just for context, by design SQL Server does not currently have a feature to capture such activity unlike the DDL trigger mentioned above, as they're treated as DML actions and can't be fired from server level except on DB level on per table basis.

I'll like to hear thoughts please. Thanks all

SQL Server Database Engine
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Marcin Policht 63,730 Reputation points MVP Volunteer Moderator
    2025-10-09T11:59:17.9633333+00:00
    1. Extended Events (XE) - as you stated, Extended Events is a viable option way to track all TRUNCATE, INSERT AND DELETE statements across the server without intrusive code changes. You can capture:
    • sqlserver.sql_statement_completed
    • sqlserver.sql_batch_completed

    Then filter by statements containing INSERT, DELETE, TRUNCATE.

    Example:

    CREATE EVENT SESSION [Track_DML] ON SERVER
    ADD EVENT sqlserver.sql_statement_completed(
        ACTION(sqlserver.sql_text, sqlserver.database_name, sqlserver.session_id)
        WHERE (sqlserver.sql_text LIKE '%INSERT%' 
            OR sqlserver.sql_text LIKE '%DELETE%' 
            OR sqlserver.sql_text LIKE '%TRUNCATE%')
    )
    ADD TARGET package0.event_file (SET filename = 'C:\XE\Track_DML.xel', max_file_size = 100);
    GO
    
    ALTER EVENT SESSION [Track_DML] ON SERVER STATE = START;
    

    The benefits include:

    • Centralized, lightweight, server-level coverage.
    • Minimal performance overhead.
    • No schema changes required.
    • Works across all databases.

    On the down side:

    • You get statement-level text, not the affected rows or before/after data.
    • Complex to filter “only real user” DMLs vs. system/internal ones.
    • TRUNCATE is captured as a statement, but not as a DML triggerable event.
    1. Change Data Capture (CDC) or Temporal Tables, which give you row-level auditing
    • CDC (Enterprise Edition, or Standard from SQL 2016 SP1+) captures INSERT, DELETE, and row versions for UPDATE.
    • Temporal Tables give you a full version history (for supported tables).

    Effectively, you get:

    • Full data-level audit trail.
    • Natively supported; integrates with queries.

    There are some drawbacks though:

    • Must enable per table.
    • Doesn't track TRUNCATE TABLE (as it bypasses CDC).
    • Adds storage overhead.
    1. Database-level DML Triggers – Per-table Granular Auditing

    These can track data mutations and user context (HOST_NAME(), APP_NAME(), SUSER_SNAME()), but only for the specific table.

    This gives you the following benefits:

    • Fine-grained control and access to the data itself.
    • Can write to your existing audit table.

    The drawbacks include:

    • Maintenance burden (needs adding to every table).
    • Can impact write performance.
    • Still cannot capture TRUNCATE (no trigger fires).
    1. SQL Server Audit (Enterprise / Standard in newer builds)

    You can configure an Audit Specification to log DMLs at the server or database level.

    Example audited actions:

    • INSERT, UPDATE, DELETE on a table or schema.

    Pluses:

    • Built-in, tamper-resistant audit trail.
    • Can audit specific operations per object.
    • Centralized in .sqlaudit files or event logs.

    Drawbacks:

    • No TRUNCATE auditing (treated as DDL).
    • Enterprise Edition used to be required for fine-grained DML auditing (Standard now supports some in newer builds).

    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.