Change Data Capture (CDC) T-SQL Useful Commands

Below are some very useful​​ Change Data Capture (CDC) T-SQL Commands.

 

--Check if cdc is enabled

 

USE​​ master​​ 

GO​​ 

SELECT​​ [name],​​ database_id,​​ is_cdc_enabled ​​​​ 

FROM​​ sys.databases ​​ ​​ ​​ ​​ ​​ ​​​​ 

GO ​​ ​​ ​​ ​​​​ 

 

 

--Enabled CDC at database level

 

USE​​ AdventureWorks​​ 

GO​​ 

EXEC​​ sys.sp_cdc_enable_db​​ 

 GO ​​ 

 

 

--Check which tables are CDC enabled

 

 USE​​ AdventureWorks​​ 

GO​​ 

SELECT​​ [name],​​ is_tracked_by_cdc ​​​​ 

FROM​​ sys.tables​​ 

GO ​​​​ 

 

 

--Cnabled CDC for a table

 

USE​​ AdventureWorks​​ 

GO​​ 

EXEC​​ sys.sp_cdc_enable_table​​ 

@source_schema​​ =​​ N'HumanResources',​​ 

@source_name ​​ ​​​​ =​​ N'Shift',​​ 

@role_name ​​ ​​ ​​ ​​​​ =​​ NULL​​ 

GO

 

--Disable CDC on a table​​ 

 

USE​​ AdventureWorks;

GO

EXECUTE​​ sys.sp_cdc_disable_table

@source_schema​​ =​​ N'HumanResources',

@source_name​​ =​​ N'Shift',

@capture_instance​​ =​​ N'HumanResources_Shift';

GO

 

 

--CaptureSelectedColumns

 

EXEC​​ sys.sp_cdc_enable_table​​ 

@source_schema​​ =​​ N'HumanResources',​​ 

@source_name ​​ ​​​​ =​​ N'Shift',​​ 

@role_name ​​ ​​ ​​ ​​​​ =​​ NULL,​​ 

@captured_column_list​​ =​​ '[ShiftID],[Name]'​​ 

GO

 

Knowledge worth sharing...Share on linkedin
Linkedin
Share on facebook
Facebook
Share on google
Google
Share on twitter
Twitter
Bookmark the permalink.

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *