Here’s a step-by-step guide and a ready-to-use SQL Agent Job script.
- Create the script
- Save this script as a SQL Agent Job step
- Open SQL Server Management Studio (SSMS).
- Connect to your instance.
- Go to SQL Server Agent → Jobs → New Job.
- General Tab:
- Name: Enable CDC on All User DBs
- Description: Enables CDC on all user DBs except excluded ones every Sunday at 11 PM.
- Steps Tab:
- New Step
- Name: Enable CDC
- Type: Transact-SQL script (T-SQL)
- Database: master
- Paste the script above.
- New Step
- Schedules Tab:
- New Schedule
- Name: Weekly - Sunday 11 PM
- Frequency: Weekly
- Recurs every: 1 week on Sunday
- Daily Frequency: Occurs once at 11:00 PM.
- Name: Weekly - Sunday 11 PM
- New Schedule
- Save the Job.
3. Start SQL Server Agent - Make sure the SQL Server Agent is running, otherwise the job won’t execute.
Notes
- The job will skip master, msdb, tempdb, model by default — adjust @ExcludeList to add more.
- Make sure your SQL Server Agent account has permission to run sp_cdc_enable_db.
- You must be a member of the sysadmin server role to enable CDC.