1
+ /* ****************************************************************
2
+ -------------------------------------
3
+ tsqltools - RDS - Auto CDC
4
+ -------------------------------------
5
+ Description: This stored procedure will help you to enable CDC
6
+ automatically when a tables is created. This is basically a database
7
+ Trigger and it'll ecxecute enable CDC procedure when we creat a
8
+ new table. This is a database level trigger, so it won't replicate
9
+ the new tables which are in another database.
10
+
11
+ How to Run: If you to enable this on DBAdmin database,
12
+ USE DBAdmin
13
+ GO
14
+ -- Execute the below Query.
15
+
16
+ -------------------------------------------------------------------
17
+
18
+ Version: v1.0
19
+ Release Date: 2018年02月10日
20
+ Author: Bhuvanesh(@SQLadmin)
21
+ Feedback: mailto:r.bhuvanesh@outlook.com
22
+ Updates: https://github.com/SqlAdmin/tsqltools/
23
+ License: GPL-3.0
24
+ tsqltools is free to download.It contains Tsql stored procedures
25
+ and scripts to help the DBAs and Developers to make job easier
26
+ (C) 2018
27
+
28
+ *******************************************************************/
29
+
30
+ -- READ THE DESCRIPTION BEFORE EXECUTE THIS ***
31
+
32
+ CREATE TABLE [dbo].[DBSchema_Change_Log]
33
+ (
34
+ [RecordId] [int] IDENTITY (1 ,1 ) NOT NULL ,
35
+ [EventTime] [datetime] NULL ,
36
+ [LoginName] [varchar](50 ) NULL ,
37
+ [UserName] [varchar](50 ) NULL ,
38
+ [DatabaseName] [varchar](50 ) NULL ,
39
+ [SchemaName] [varchar](50 ) NULL ,
40
+ [ObjectName] [varchar](50 ) NULL ,
41
+ [ObjectType] [varchar](50 ) NULL ,
42
+ [DDLCommand] [varchar](max ) NULL
43
+
44
+ ) ON [PRIMARY]
45
+ GO
46
+
47
+ CREATE TRIGGER [auto_cdc] ON Database
48
+ FOR CREATE_TABLE
49
+ AS
50
+ DECLARE @eventInfo XML
51
+ SET @eventInfo = EVENTDATA()
52
+ INSERT INTO DBSchema_Change_Log
53
+ VALUES
54
+ (
55
+ REPLACE (CONVERT (VARCHAR (50 ),@eventInfo.query(' data(/EVENT_INSTANCE/PostTime)' )),' T' , ' ' ),
56
+ CONVERT (VARCHAR (50 ),@eventInfo.query(' data(/EVENT_INSTANCE/LoginName)' )),
57
+ CONVERT (VARCHAR (50 ),@eventInfo.query(' data(/EVENT_INSTANCE/UserName)' )),
58
+ CONVERT (VARCHAR (50 ),@eventInfo.query(' data(/EVENT_INSTANCE/DatabaseName)' )),
59
+ CONVERT (VARCHAR (50 ),@eventInfo.query(' data(/EVENT_INSTANCE/SchemaName)' )),
60
+ CONVERT (VARCHAR (50 ),@eventInfo.query(' data(/EVENT_INSTANCE/ObjectName)' )),
61
+ CONVERT (VARCHAR (50 ),@eventInfo.query(' data(/EVENT_INSTANCE/ObjectType)' )),
62
+ CONVERT (VARCHAR (MAX ),@eventInfo.query(' data(/EVENT_INSTANCE/TSQLCommand/CommandText)' ))
63
+ )
64
+
65
+ declare @tbl varchar (100 ) = (select top (1 )
66
+ OBJECTname
67
+ from DBSchema_Change_Log
68
+ order by recordid desc )
69
+ DECLARE @schemaname varchar (100 ) = (select top (1 )
70
+ schemaname
71
+ from DBSchema_Change_Log
72
+ order by recordid desc )
73
+ DECLARE @primarykey int = ( select case CONSTRAINT_TYPE when ' PRIMARY KEY' THen 1 else 0 end as PRIMARYkey
74
+ from INFORMATION_SCHEMA .TABLE_CONSTRAINTS
75
+ where TABLE_NAME= @tbl and TABLE_SCHEMA= @schemaname)
76
+
77
+ exec sys .sp_cdc_enable_table
78
+ @source_schema = @schemaname,
79
+ @source_name = @tbl,
80
+ @role_name = NULL ,
81
+ @supports_net_changes = @primarykey
82
+ GO
83
+ -- Enable the Trigger
84
+ ENABLE TRIGGER [auto_cdc] ON database
85
+ GO
86
+
0 commit comments