Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2016 October SQL Server: Encrypted DDL Trigger to Track all Database Changes

SQL Server: Encrypted DDL Trigger to Track all Database Changes

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

I know, you guys are thinking that why we require DDL Trigger to track database changes in SQL Server.

SQL Server already has Changed Data Capture (CDC) and Audit option.
When we are talking about DDL Trigger, compare to other automated process this manual process is still preferred by SQL DBA because It is easy to use and manage.

A Database Administrator is also responsible for all different types of Database Security like: Database / Schema / Object level permission, Tracking object creation.

I have enabled SQL Server Audit for one of our production report server, but some time It does not work as per the expectation.

I have searched the alternate solution and found that we can create DDL trigger on a database to track the DDL operation of different objects.
We can also use DDL triggers to verify the result of SQL Server Audit.

Other take care points of DDL Trigger are:

Only SA or Admin user has to perform this stuff. The DBA should restrict other common database user to view or modify the DDL trigger.

It should be encrypted.

If our database is heavily loaded with tons of transaction, we should not create any type of DDL trigger on it.

Here, I am creating one encrypted DDL Trigger which tracks different types of object like:
Stored Procedure, Table, View, Function, Trigger, Sequence, Index.

Step to create Encrypted DDL Trigger:

First Create separate database:

1
2
CREATE DATABASE [DDLChanges]
GO

Create one table which will hold all event information:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
CREATE TABLE [DDLChanges].[dbo].[tbl_TrackDDLChanges](
[EventDate] [datetime] NOT NULL DEFAULT (getdate()),
[EventType] [nvarchar](64) NULL,
[EventDDL] [nvarchar](max) NULL,
[LoginName] [nvarchar](255) NULL,
[DatabaseName] [nvarchar](255) NULL,
[SchemaName] [nvarchar](255) NULL,
[ObjectName] [nvarchar](255) NULL,
[HostName] [varchar](64) NULL,
[IPAddress] [varchar](32) NULL,
[ProgramName] [nvarchar](255) NULL,
[EventXML] [xml] NULL
)
GO

SELECT your application Database and create below encrypted DDL trigger:

If you require to enable DDL trigger on multiple database, execute DDL trigger one by one on each require a database.

Please do not create a DDL Trigger on [DDLChanges] database, that we have created in the first step which is for tracking purposes.

Now, perform any DDL action on your database and check [DDLChanges].[dbo].[tbl_TrackDDLChanges] table where you can find important information like: which SQL Query, who created, which host, what time, on which database and other.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
CREATE TRIGGER [trg_DDL_Changes]
ON DATABASE
WITH ENCRYPTION
FOR
CREATE_PROCEDURE
,ALTER_PROCEDURE
,DROP_PROCEDURE
,CREATE_TABLE
,ALTER_TABLE
,DROP_TABLE
,CREATE_VIEW
,ALTER_VIEW
,DROP_VIEW
,CREATE_FUNCTION
,ALTER_FUNCTION
,DROP_FUNCTION
,CREATE_TRIGGER
,ALTER_TRIGGER
,DROP_TRIGGER
,CREATE_SEQUENCE
,ALTER_SEQUENCE
,DROP_SEQUENCE
,CREATE_INDEX
,ALTER_INDEX
,DROP_INDEX
AS
BEGIN
SET NOCOUNT ON;
DECLARE
@EventData XML = EVENTDATA();
DECLARE
@ip VARCHAR(32) =
(
SELECT client_net_address
FROM sys.dm_exec_connections
WHERE session_id = @@SPID
);
INSERT DDLChanges.dbo.tbl_TrackDDLChanges
(
EventType
,EventDDL
,LoginName
,DatabaseName
,SchemaName
,ObjectName
,HostName
,IPAddress
,ProgramName
,EventXML
)
SELECT
@EventData.value('(/EVENT_INSTANCE/EventType)[1]','NVARCHAR(100)')
,@EventData.value('(/EVENT_INSTANCE/TSQLCommand)[1]','NVARCHAR(MAX)')
,SUSER_SNAME()
,DB_NAME()
,@EventData.value('(/EVENT_INSTANCE/SchemaName)[1]','NVARCHAR(255)')
,@EventData.value('(/EVENT_INSTANCE/ObjectName)[1]','NVARCHAR(255)')
,HOST_NAME()
,@ip
,PROGRAM_NAME()
,@EventData
END
GO

Oct 21, 2016Anvesh Patel
MySQL: Controlling Query Optimizer to choose the Best Execution PlanSQL Server: Script to find all Backup History Information
Comments: 4
  1. Stephen Swan
    November 3, 2017 at 10:48 am

    Awesome ddl trigger! I really appreciate the work.

    You could make it a little shorter by using ddl groups like DDL_VIEW_EVENTS instead of listing them individually.

    • Anvesh Patel
      Anvesh Patel
      November 3, 2017 at 6:18 pm

      Thank you Stephen

  2. Ashish Gorana
    January 7, 2019 at 7:31 am

    Many Thanks for this.

  3. vijay
    June 4, 2019 at 4:17 am

    Hi, How to compile in MYSQL.

Anvesh Patel
Anvesh Patel

Database Engineer

October 21, 2016 SQL Server, SQL Server DBA ScriptAnvesh Patel, database, database research and development, database security, dbrnd, DDL Trigger, security, SQL Query, SQL Server, SQL Server Administrator, SQL Server Monitoring, SQL Server Performance Tunning, SQL Server Tips and Tricks, Track Object, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....