(+351) 21 24 10006  ·  info@bconcepts.pt
Carnaxide, Lisbon

How to audit schema changes in SQL Server: step by step

João Barros 02 de December de 2026 4 min read

Auditing schema changes in SQL Server helps know who and when created, altered, or dropped tables, procedures, or other entities — useful for security and traceability. This guide shows how to log those operations simply using DDL triggers and an audit table.

Prerequisites

  • SQL Server instance (supported version) with sysadmin or ALTER DATABASE permissions.
  • SQL Server Management Studio (SSMS) or another tool to run T-SQL.
  • Basic T-SQL knowledge: CREATE TABLE, INSERT, SELECT.

Step 1: Create the schema audit table

Create a central table to log DDL events. We store event type, login, object_name, command and timestamp. This is the minimum to start and can be extended as needed.

CREATE DATABASE IF NOT EXISTS SchemaAuditDB; -- se a versão suportar, senão crie manualmente
GO
USE SchemaAuditDB;
GO
CREATE TABLE SchemaChangeAudit (
    AuditID BIGINT IDENTITY(1,1) PRIMARY KEY,
    EventType NVARCHAR(100),
    EventDate DATETIME2 DEFAULT SYSUTCDATETIME(),
    ServerPrincipal NVARCHAR(200),
    DatabaseName NVARCHAR(200),
    SchemaName NVARCHAR(200),
    ObjectName NVARCHAR(500),
    TSQLCommand NVARCHAR(MAX)
);

Step 2: Create a database-level DDL trigger

A DDL trigger captures events like CREATE/ALTER/DROP. We create a trigger that inserts a record into the audit table whenever a relevant DDL event occurs.

USE YourTargetDatabase; -- substitua pelo nome da base de dados a auditar
GO
CREATE OR ALTER TRIGGER trg_Audit_Schema_Changes
ON DATABASE
FOR CREATE_TABLE, ALTER_TABLE, DROP_TABLE,
    CREATE_PROCEDURE, ALTER_PROCEDURE, DROP_PROCEDURE,
    CREATE_FUNCTION, ALTER_FUNCTION, DROP_FUNCTION,
    CREATE_VIEW, ALTER_VIEW, DROP_VIEW
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @eventData XML = EVENTDATA();
    INSERT INTO SchemaAuditDB.dbo.SchemaChangeAudit (
        EventType, ServerPrincipal, DatabaseName, SchemaName, ObjectName, TSQLCommand
    )
    VALUES (
        @eventData.value('(/EVENT_INSTANCE/EventType)[1]', 'NVARCHAR(100)'),
        @eventData.value('(/EVENT_INSTANCE/LoginName)[1]', 'NVARCHAR(200)'),
        @eventData.value('(/EVENT_INSTANCE/DatabaseName)[1]', 'NVARCHAR(200)'),
        @eventData.value('(/EVENT_INSTANCE/SchemaName)[1]', 'NVARCHAR(200)'),
        @eventData.value('(/EVENT_INSTANCE/ObjectName)[1]', 'NVARCHAR(500)'),
        @eventData.value('(/EVENT_INSTANCE/TSQLCommand)[1]', 'NVARCHAR(MAX)')
    );
END;
GO

Step 3: Test a change and view the log

Execute a sample DDL operation (create, alter, or drop an object) to confirm the trigger logs the details in the audit table.

USE YourTargetDatabase;
GO
CREATE TABLE dbo.TestAudit (Id INT PRIMARY KEY, Name NVARCHAR(100));
GO
-- Alter the table
ALTER TABLE dbo.TestAudit ADD CreatedAt DATETIME2 DEFAULT SYSUTCDATETIME();
GO
-- Drop the table
DROP TABLE dbo.TestAudit;
GO

-- Query the audit table
SELECT TOP 100 *
FROM SchemaAuditDB.dbo.SchemaChangeAudit
ORDER BY EventDate DESC;

Step 4: Handle common errors and limitations

Common errors include lack of permissions to create database-level triggers or errors when inserting into the remote table. If the trigger fails, the DDL operation may also fail — so test first in a non-production environment. To reduce impact, keep logging operations simple and fast.

-- Verificar permissões necessárias
-- Sysadmin ou ALTER DATABASE pode ser necessário para criar triggers a nível de base de dados
SELECT SUSER_SNAME(), USER_NAME();

-- Se não quiser que a DDL falhe em caso de erro de logging, envolva a escrita com TRY/CATCH
-- Exemplo dentro do trigger (simplificado):
BEGIN TRY
   INSERT INTO SchemaAuditDB.dbo.SchemaChangeAudit (...)
   VALUES (...);
END TRY
BEGIN CATCH
   -- opcional: armazenar erro em ficheiro de log ou tabela local leve
END CATCH;

Verify the result

Confirm that events appear in the SchemaChangeAudit table with EventType and TSQLCommand populated. Also check the user (ServerPrincipal) and DatabaseName to ensure traceability across databases.

Conclusion

With a DDL trigger and a simple audit table, you can now log schema changes in SQL Server and gain visibility into who changes objects. Next steps: add a column for hostname, record the schema before the change, or integrate with SIEM. Tip: always test audit triggers in a development environment before applying in production.