Saturday, May 23, 2009

Restore a CDC Enabled Database

You might not be able to restore a CDC Enabled DB from a backup using the GUI(Management Studio). Below is the script which explicitly mentions about KEEP_CDC. Once you restore the DB, enable the cleanup and capture job and the DB should be completely up with CDC enabled.

RESTORE DATABASE Sample
FROM DISK = N'E:\SQLBackup\Sample_07232009_1058.bak'
WITH FILE = 1,
MOVE N'Sample' TO N'E:\Program Files\Microsoft SQL Server\MSSQL10.SQLDEV\MSSQL\DATA\Sample.mdf',
MOVE N'Sample_log' TO N'E:\Program Files\Microsoft SQL Server\MSSQL10.SQLDEV\MSSQL\DATA\Sample_1.ldf',
NOUNLOAD,
STATS = 10,
KEEP_CDC
GO

and then

USE Sample
GO
EXEC sys.sp_cdc_add_job @job_type = N'cleanup'
GO
EXEC sys.sp_cdc_add_job @job_type = N'capture'
GO