solving sql server problems for millions of dbas and developers since 2006



SQL Server DBA Tips SQL Server Developer Tips SQL Server Business Intelligence Tips SQL Server Career Tips SQL Server Tip Categories SQL Server Tutorials SQL Server Webcasts SQL Server Whitepapers SQL Server Tools SQL Server Questions and Answers MSSQLTips Authors About MSSQLTips SQL Server User Groups MSSLQTips Giveaways MSSQLTips Advertising Options

MSSQLTips Facebook Page MSSQLTips LinkedIn Page MSSQLTips RSS Feed MSSQLTips Twitter Page MSSQLTips Google+ Page





SQL Product Highlight

Red Gate Software - SQL Monitor

SQL Server performance monitoring and alerting - SQL Monitor offers an easy entrance to advanced server monitoring with a simple design that's a refreshing change from the status quo. Red Gate have added custom metrics and user roles to the product without spoiling its ease-of-use, to help you answer that timeless question, 'How healthy are your servers?'

Learn more!




Restoring a SQL Server database that uses Change Data Capture

By: | Read Comments (2) | Print

Jugal has 8+ years of extensive SQL Server experience and has worked on SQL Server 2000, 2005, 2008 and 2008 R2.

Related Tips: 1 | 2 | 3 | 4 | More

Problem

When restoring a database that uses Change Data Capture (CDC), restoring a backup works differently depending on where the database is restored.  In this tip we take a look at different scenarios when restoring a database when CDC is enabled.

Solution

When restoring a CDC enabled database you may have these scenarios:

  • Restore the CDC enabled database on the same SQL instance by overwriting existing database
  • Restore the CDC enabled database with the different name on same SQL instance
  • Restore the CDC enabled database on a different SQL instance

Sample CDC Database Setup

The below script creates a database and enables CDC.

-- Create database sqldbpool
CREATE DATABASE [sqldbpool] ON  PRIMARY 
( NAME = N'SQLDBPool', FILENAME = N'L:\SQLDBPool.mdf' , SIZE = 5120KB , 
MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'SQLDBPool_log', FILENAME = N'F:\SQLDBPool_log.LDF' , SIZE = 3840KB , 
MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO
use sqldbpool;
go
-- creating table
create table Customer
(
custID int constraint PK_Employee primary key Identity(1,1)
,custName varchar(20)
)
--Enabling CDC on SQLDBPool database
USE SQLDBPool
GO
EXEC sys.sp_cdc_enable_db
--Enabling CDC on Customer table
USE SQLDBPool
GO
EXEC sys.sp_cdc_enable_table
@source_schema = N'dbo',
@source_name = N'Customer',
@role_name = NULL
GO
--Inserting values in customer table
insert into Customer values('jugal'),('shah')
-- Querying CDC table to get the changes
select * from cdc.dbo_customer_CT
--Taking full database backup which I am going to restore for all the above scenarios
backup database sqldbpool to disk = 'l:\sqldbpool.bak'

Restore to same instance with same database name

In the scenario of restoring a database on the same instance by overwriting the existing database, CDC remains enabled and all related metadata is persisted. CDC will start working since the Capture and Cleanup jobs are already on the box.

--Restoring on the same SQL Instance with the Same database name
restore database sqldbpool from disk = 'l:\sqldbpool.bak' with replace

Restore to same instance with different database name or Restore to a different instance

In these scenarios CDC will be disabled and all the related metadata is deleted from the database.  To not loose this information you must use the Keep_CDC option with the database restore statement as shown below.

--Restoring on the same instance with the different database name
Restore Database sqldbpool_1 
from disk = 'l:\sqldbpool.bak' 
with move 'SQLDBPool' to 'L:\SQLDBPool1.mdf',
move 'SQLDBPool_log' to 'F:\SQLDBPool_log1.LDF',keep_cdc
--Restoring on a different instance with same database name
restore database sqldbpool from disk = 'l:\sqldbpool.bak' with keep_cdc

In addition, you need to add the Capture and Cleanup jobs using the following commands in the appropriate database. 

Use sqldbpool_1
exec sys.sp_cdc_add_job 'capture'
GO
exec sys.sp_cdc_add_job 'cleanup'
GO

Next Step

  • Try all the above solutions by creating different scenarios for a CDC enabled database
  • Read these other CDC tips


Related Tips: 1 | 2 | 3 | 4 | More | Become a paid author


Last Update: 6/24/2011

Share: Share 






Comments and Feedback:

Friday, June 24, 2011 - 5:35:45 PM - Cardy Read The Tip

Nice & neat example to illustrate this DB feature  for thise that havent ever used it.

Just wanted to add the importance of using the "keep_cdc" switch .. because this option is not in the Restore DB Dialog of SSMS in the same way the with KEEP_Replication exists.

Consequently you have to Restore a CDC enabled DB via T-sql (arggh just like the good old days)

I've seen a few other grumbles on Cnnect about why this feature isnt there  but no signs of any lans to add it.


Friday, June 24, 2011 - 5:38:02 PM - Cardy Read The Tip

Apologies .. fingers not working tonight ...

Nice & neat example to illustrate this DB feature for those that havent ever used it.

Just wanted to add the importance of using the "keep_cdc" switch .. because this option is not part of the Restore DB Dialog of SSMS in the same way the with KEEP_Replication switch is.

Consequently you have to Restore a CDC enabled DB via T-sql (argh- just like the good old days)

I've seen a few other grumbles on Connect about this ommission from SSMS - but no signs of any plans to add it.



Post a Comment or Question

Keep it clean and stay on the subject or we may delete your comment.
Your email address is not published. Required fields are marked with an asterisk (*)

*Name   *Email   Notify for updates
Comments
*Enter Code refresh code


 
Sponsor Information
Try the free performance monitoring tool from Idera!

It takes just 5 minutes to connect your SQL Databases to source control. Got 5 minutes? Get started now.

Need SQL Server help and not sure where to turn? Reach out to the Edgewood consultants for a Health Check.

Get SQL Server Tips Straight from Kevin Kline.

Join the over million SQL Server Professionals who get their issues resolved daily.

Free Learning - Introduction to SQL Azure Delivered by Herve Roggero on Wednesday, June 13 @ 3:00 PM EST


Copyright (c) 2006-2012 Edgewood Solutions, LLC All rights reserved
privacy | disclaimer | copyright | advertise | about
authors | contribute | feedback | giveaways | user groups
Some names and products listed are the registered trademarks of their respective owners.


Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com