Change Data Capture (CDC) is one of the frequently used features in SQL Server 2008 and above, it records any (CDC enabled) table’s changes and stores in audit tables. Recently, I upgraded one of my client’s database from SQL Server 2005 database to SQL Server 2012 and one of the key reasons to upgrade is to utilize the new features in the upgraded version. Once I started enabling CDC feature in few tables of the database it gave me given below error.
Message Number: 22939
Severity : 16
Error Message: The parameter @supports_net_changes is set to 1, but the source table does not have a primary key defined and no alternate unique index has been specified.
I presume that CDC has been enabled on this particular database. Let me create a sample table to demonstrate this error.
USE AdventureWorks2012 GO --Create Sample Table CREATE TABLE tbl_Sample ( [ID] INT NOT NULL, [NAME] VARCHAR(50) ) GO -- Enable CDC feature on this table with net changes support parameter. USE AdventureWorks2012 GO EXEC sys.sp_cdc_enable_table @source_schema = N'dbo', @source_name = N'tbl_sample', @role_name = NULL, @supports_net_changes = 1 GO --OUTPUT
Msg 22939, Level 16, State 1, Procedure sp_cdc_enable_table_internal, Line 194
The parameter @supports_net_changes is set to 1, but the source table does not have a primary key defined and no alternate unique index has been specified.
Ooopps…… I am NOT able to enable CDC on sample table.
The reason behind this error is that you do not have either primary key or unique index on the sample table and you want to enable net changes support in CDC. Before proceeding with the resolution, you should check whether you can create a Primary Key or Unique Index on the table to avoid such error.
Given below is the simple script to add Primary Key OR Unique Index in the sample table.
--Create Primary Key USE AdventureWorks2012 GO ALTER TABLE dbo.tbl_Sample ADD PRIMARY KEY (ID) GO -- Create Unique Index USE AdventureWorks2012 GO ALTER TABLE tbl_Sample ADD CONSTRAINT UX_Constraint UNIQUE (ID) GO --OUTPUT
You can execute any one of the above scripts and can create Primary Key or Unique Index in the sample table.
Once you executed the above script, you can easily enabled the CDC with supports_net_changes as shown below.
USE AdventureWorks2012 GO EXEC sys.sp_cdc_enable_table @source_schema = N'dbo', @source_name = N'tbl_sample', @role_name = NULL, @supports_net_changes = 1 GO --OUTPUT
Command(s) completed successfully.
Remember, whenever you need to enable CDC with supports_net_changes, make sure that the particular table HAS either Primary Key or Unique Index in order to avoid this error.