How to Disable Change Data Capture(CDC) On a Database in SQL Server - SQL Server Tutorial

Scenario: 

You are working as SQL Server DBA or Developer, you need to write script that should accept database name and then disable Change Data Capture ( CDC ) on that database.

The below script can be used to Disable Change Data Capture on a database. You need to set the value for @DBName variable to database on which you want to disable Change Data Capture.


/*--------------------------------
1: Disable CDC ON Database
----------------------------------*/
DECLARE @DBName NVARCHAR(100)
DECLARE @Cdc_Status BIT
 --> Provide your Database Name on which you want to Disable CDC
SET @DBName='TESTDB'
SET @Cdc_Status=(SELECT is_cdc_enabled
                 FROM   sys.databases
                 WHERE  name = @DBName)
IF @Cdc_Status = 0
  PRINT ' CDC is already disabled on Database:: '
        + @DBName
IF @Cdc_Status = 1
  BEGIN
      DECLARE @SQL NVARCHAR(500)

      SET @SQL=@DBName + '.sys.Sp_cdc_disable_db'

      EXEC (@SQL)

      PRINT ' CDC Disabled on ' + @DBName
            + ' successfully'
  END