171 views
asked in MS SQL Server by
Enable Auditing in SQL Server and demonstrate - Part 1

1 Answer

answered by

inchirags@gmail.com   Chirag's SQL Server DBA Tutorial         https://www.chirags.in

*****************************************************************************************

* Enable Auditing in SQL Server and demonstrate - Part 1 *

*****************************************************************************************

YouTube Video:

For Part 2 - Add a new database to an existing audit specification in SQL Server

Enable auditing in SQL Server and demonstrate it with a step-by-step example, we will create a database, tables, perform CRUD operations, and log user activity. Here's how to do it:

Step 1: Create a Test Database

Run the following query to create a sample database:

CREATE DATABASE AuditDemoDB;
GO

USE AuditDemoDB;
GO

-- Create a sample table
CREATE TABLE Employees (
    EmployeeID INT PRIMARY KEY IDENTITY(1,1),
    Name NVARCHAR(50),
    Position NVARCHAR(50),
    Salary DECIMAL(10, 2)
);
GO

Step 2: Enable Server Audit

Server-level auditing is required to capture database-specific events.

    Create a Server Audit and specify the file location to save logs:

CREATE SERVER AUDIT ServerAudit
TO FILE (FILEPATH = 'C:\AuditLogs\', MAXSIZE = 10 MB);
GO

Enable the Server Audit:

    ALTER SERVER AUDIT ServerAudit WITH (STATE = ON);
    GO

Step 3: Create a Database Audit Specification

    Create an audit specification to track SELECT, INSERT, UPDATE, DELETE operations for a specific database:

CREATE DATABASE AUDIT SPECIFICATION DbAuditSpec
FOR SERVER AUDIT ServerAudit
ADD (SELECT, INSERT, UPDATE, DELETE ON DATABASE::AuditDemoDB BY [public]);
GO

Enable the Database Audit Specification:

    ALTER DATABASE AUDIT SPECIFICATION DbAuditSpec WITH (STATE = ON);
    GO

Step 4: Perform CRUD Operations

Perform some sample operations to generate audit logs:

USE AuditDemoDB;
GO

-- Insert records
INSERT INTO Employees (Name, Position, Salary) VALUES ('Chirag', 'DBA', 70000);
INSERT INTO Employees (Name, Position, Salary) VALUES ('Sanju', 'Teacher', 50000);
GO

-- Update a record
UPDATE Employees SET Salary = 55000 WHERE Name = 'Sanju';
GO

-- Select records
SELECT * FROM Employees;
GO

-- Delete a record
DELETE FROM Employees WHERE Name = 'Sanju';
GO

Step 5: View Audit Logs

Audit logs can be queried or viewed in SSMS:

1. Query Audit Logs using T-SQL

Use the following query to read the audit logs:

SELECT event_time,
       action_id,
       succeeded,
       object_name,
       statement,
       server_principal_name
FROM sys.fn_get_audit_file('C:\AuditLogs\*.sqlaudit', DEFAULT, DEFAULT);
GO

2. View Logs in SSMS

    Navigate to Security → Audits in SSMS.

    Right-click the ServerAudit and choose View Audit Logs.

Step 6: Disable or Drop Auditing

When auditing is no longer required, disable or drop it:

    Disable database and server audit:

ALTER DATABASE AUDIT SPECIFICATION DbAuditSpec WITH (STATE = OFF);
ALTER SERVER AUDIT ServerAudit WITH (STATE = OFF);
GO

Drop the audit configuration:

    DROP DATABASE AUDIT SPECIFICATION DbAuditSpec;
    DROP SERVER AUDIT ServerAudit;
    GO

Expected Output

    Logs will show user activity such as INSERT, SELECT, UPDATE, and DELETE statements.

    Fields in the log include timestamp, SQL statement executed, user name, and success status.

Let me know if you'd like further assistance!

For any doubts and query, please write on YouTube video comments section.

Note : Flow the Process shown in video.

Subscribe and like for more videos:

https://www.youtube.com/@chiragstutorial

Don't forget to, Follow, Like,  Share &, Comment

Thanks & Regards,

Chitt Ranjan Mahto "Chirag"

_________________________________________________________________________________________

Note: All scripts used in this demo will be available in our website.

Link will be available in description.

Most popular tags

laravel postgresql laravel-10 replication ha postgresql mongodb laravel-11 mongodb database mongodb tutorial ubuntu 24.04 lts streaming-replication mysql database laravel postgresql backup laravel login register logout database mysql php laravel 11 - login with otp valid for 10 minutes. user and admin registration user and admin login multiauth technlogy asp.net asp.net c# mysql master slave replication centos linux laravel sql server schedule backup autobackup postgresql django python haproxy load balancer install self sign ssl laravel 11 gaurds zabbix 7 how to install graylog on ubuntu 24.04 lts | step-by-step asp.net core mvc .net mvc network upload c# ssl integration sql server on ubuntu 22.04 lts mssql server ms sql server sql server user access in postgres mysql password change cent os linux configure replica laravel 11 socialite login with google account google login kubernetes (k8s) install nginx load balancer install install and configure .net 8.0 in ubuntu 24.04 lts php in iis php with iis php tutorial chirags php tutorials chirags php tutorial chirags tutorial laravel 11 guards mongodb sharding metabase business analytics metabase postgresql 16 to postgresql 17 postgresql migration letsencrypt mongodb crud rocky linux laravel custom captcha laravel 11 captcha laravel captcha mongo dll php.ini debian 12 nginx apache nextcloud gitea in ubuntu git gitea npm error node js mysql ndb cluster mysql cluster ssl oracle login register logout in python debian windows shell batch file bat file time stamp date time shopping cart in laravel centos rhel swap memeory rhel 5.5
...