-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCDC.sql
More file actions
49 lines (42 loc) · 1.12 KB
/
CDC.sql
File metadata and controls
49 lines (42 loc) · 1.12 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
SET UNSI ON
-- Using CDC
use [DB_name]
go
-- Owner of database must be 'sa'
-- Change dbowner
exec sp_changedbowner 'sa'
go
-- Enable CDC on database
use [DB_name]
go
exec sys.sp_cdc_enable_db
go
-- Enable CDC on specific table [dbo].[test]
exec sys.sp_cdc_enable_table @source_schema = N'dbo', @source_name = N'test', @role_name=null, @capture_instance=null
----------
-- Enable all tables by CDC CURSOR
USE test
go
exec sp_changedbowner 'sa'
go
exec sys.sp_cdc_enable_db
go
DECLARE @Schema AS VARCHAR(300)
DECLARE @Table AS VARCHAR(300)
DECLARE @cdc_Role AS VARCHAR(300)
SET @cdc_Role = 'NULL'
DECLARE cdc_cursor CURSOR FOR
SELECT TABLE_SCHEMA, TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE';
OPEN cdc_cursor
FETCH NEXT FROM cdc_cursor INTO @Schema , @Table
WHILE @@FETCH_STATUS = 0
BEGIN
EXECUTE sys.sp_cdc_enable_table
@source_schema = @Schema
, @source_name = @Table
, @role_name = @cdc_Role;
PRINT 'cdc Enable done for ' + @Schema + '.'+@Table ;
FETCH NEXT FROM cdc_cursor INTO @Schema , @Table
END
CLOSE cdc_cursor
DEALLOCATE cdc_cursor