-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02 SourceControlTrigger.sql
More file actions
87 lines (74 loc) · 2.91 KB
/
02 SourceControlTrigger.sql
File metadata and controls
87 lines (74 loc) · 2.91 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*******************************
Author: Eric Austin - https://github.com/austineric/SimpleSQLServerSourceControl
Create date: November 2018
Description: Create trigger used in SimpleSQLSourceControl system
This trigger should be created in any database where source control should be implemented
*******************************/
--ensure desired database is being used
PRINT 'Make sure this is the database you want to create the trigger in: ' + DB_NAME()
--check to ensure a trigger of the same name does not already exist in the database
IF EXISTS (SELECT * FROM sys.triggers WHERE name='SourceControlTrigger')
PRINT 'Warning: a trigger named SourceControlTrigger already exists in this database.'
ELSE PRINT 'Okay to proceed with SourceControlTrigger creation.'
--exit to ensure the above statements are run before the trigger creation statement below
RETURN;
/*
IMPORTANT: if you have created the SourceControl table in a different database than the one where the trigger will reside,
you must go to the "INSERT INTO SourceControl" statement below and change it to "INSERT INTO DatabaseName.SchemaName.SourceControl"
*/
--create source control trigger
--(SSMS may be warning that 'CREATE TRIGGER' must be the only statement in the batch; just run the CREATE TRIGGER statement separately from the above statements)
CREATE TRIGGER SourceControlTrigger ON DATABASE
FOR
CREATE_PROCEDURE, ALTER_PROCEDURE, DROP_PROCEDURE
,CREATE_VIEW, ALTER_VIEW, DROP_VIEW
,CREATE_TABLE, ALTER_TABLE, DROP_TABLE
,CREATE_FUNCTION, ALTER_FUNCTION, DROP_FUNCTION
,CREATE_TRIGGER, ALTER_TRIGGER, DROP_TRIGGER
,CREATE_INDEX, ALTER_INDEX, DROP_INDEX
AS
SET NOCOUNT ON;
BEGIN
DECLARE @data XML
DECLARE @eventtimestamp DATETIME
DECLARE @server VARCHAR(255)
DECLARE @database VARCHAR(255)
DECLARE @user VARCHAR(255)
DECLARE @objectname VARCHAR(255)
DECLARE @objecttype VARCHAR(255)
DECLARE @action VARCHAR(255)
DECLARE @definition VARCHAR(MAX)
SET @data=EVENTDATA()
SET @eventtimestamp=GETDATE()
SET @server=@data.value('(/EVENT_INSTANCE/ServerName)[1]', 'VARCHAR(255)')
SET @database=@data.value('(/EVENT_INSTANCE/DatabaseName)[1]', 'VARCHAR(255)')
SET @user=@data.value('(/EVENT_INSTANCE/LoginName)[1]', 'VARCHAR(255)')
SET @objectname=@data.value('(/EVENT_INSTANCE/ObjectName)[1]', 'VARCHAR(255)')
SET @objecttype=@data.value('(/EVENT_INSTANCE/ObjectType)[1]', 'VARCHAR(255)')
SET @action=@data.value('(/EVENT_INSTANCE/EventType)[1]', 'VARCHAR(255)')
SET @definition=@data.value('(/EVENT_INSTANCE/TSQLCommand)[1]', 'VARCHAR(MAX)')
BEGIN
INSERT INTO SourceControl
(
EventTimestamp
,[Server]
,[Database]
,[User]
,ObjectName
,ObjectType
,[Action]
,[Definition]
)
VALUES
(
@eventtimestamp
,@server
,@database
,@user
,@objectname
,@objecttype
,@action
,@definition
)
END;
END;