Free Newsletters:
DatabaseJournal  
DBANews
Search Database Journal:
 
HOME News MS SQL Oracle DB2 Access MySQL PostgreSQL PHP SQL Etc Scripts Links Discussion
internet.com

» HOME
» NEWS
» FEATURES
» SERIES
MS SQL
Oracle
MS Access
MySQL
DB2
» RESOURCES
Products
Scripts
Links
» DISCUSSION
» TECH JOBS

Marketplace Partners
Be a Marketplace Partner




internet.commerce
Be a Commerce Partner
Car Donations
Desktop Computers
Baby Photo Contest
Home Improvement
Memory
Promotional Pens
Corporate Gifts
Promotional Products
Hurricane Shutters
Data Center Solutions
Holiday Gift Ideas
Computer Hardware
Promotional Golf
Remote Online Backup




MySpace Joins eBay, Yahoo in Open Profile Push

News Corp. Unit Under Fire for Ties to Hacker

Are Non-PC Devices Hurting 'Net Innovation?

internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Linked Data Planet Conference & Expo

CA ERwin® Data Modeler Proven database design and modeling. Efficiently analyze, design and deploy effective database solutions. Whitepaper: Manage SQL Server Deployments
Try it free: CA ERwin® Data Modeler


Guide to Oracle 11g and Database Migration
Oracle Database 11g includes more features for self-management and automation, which makes it easier for customers to cost-effectively manage their data. Download this Internet.com eBook for an overview of some of the new features in 11g and for an overview of the issues you need to consider as you prepare for a database migration. »
Innovate Faster with Oracle Database 11g
Read this in-depth analysis of 56 customers, which shows significant differences between the value software vendors Oracle and SAP deliver to midsize companies. »
Oracle Business Intelligence Standard Edition One
Find out how Newport Beach, CA-based Mobilitie is shaking up the telecom industry by leveraging technology to provide an entirely different financial model for deploying, upgrading, and owning wireless and wireline network assets. »
Business Intelligence and Enterprise Performance Management: Trends for Emerging Businesses
Quickly implementing an ERP software solution can be of tremendous benefit; however, companies often struggle to balance the benefits of reducing implementation time and cost with the risks of an accelerated deployment. Read this white paper to learn about easy-to-follow best practices for achieving a successful accelerated implementation. »
Making the Case for Oracle Database on Windows
Users benefit as vendors reduce enterprise complexity and deliver integration. »
Related Articles
Microsoft SQL Server 2008 - Change Data Capture – Part I

Production Manager (hands on)
Aquent
US-MA-Cambridge

Justtechjobs.com Post A Job | Post A Resume
MS SQL
February 6, 2008
Microsoft SQL Server 2008 - Change Data Capture – Part 2
By Muthusamy Anantha Kumar aka The MAK

One of the new feature in Microsoft SQL Server 2008 is track the changes on a table. You can enable change tracking on a table using Change Data Capture feature.

Part 1 of this article discussed how to enable the new SQL Server Feature “Change Data Capture” on a database. It also illustrated how to enable the Change Data Capture on a table, how to keep track of Data Definition Language changes on a table and explained the CDC schema and changes happening in the objects of CDC schema.

Note: This article is written based on the SQL Server 2008 – Nov CTP

Part 2 of this article illustrates how to enable Change Data Capture on a database, on a table and how SQL Server tracks the data changes of the CDC enabled table.

In Part 1 we created the database Mydatabase and created my table in the database Mydatabase. We altered the table by adding a couple of columns and saw how the DDL changes were captured.

Now let’s add some data to the table using the following SQL Statements:

Step 1

Execute the following Transact SQL Statement as shown below. [Refer Fig 1.1]

use MyDataBase
go
select  * from MyTable
go
Insert into Mytable values (1, 'Dance Doll','221, West Broad st, 
 Greenbay, Wisconsin',60000,1000)
Insert into Mytable values (2, 'Rainbow Colors','21, East st, 
 Denville, New Jersey',68000,1300)
Insert into Mytable values (3, 'River Dance','1, South Broad st, 
 Quincy, Massachusetts',76000,1600)
Insert into Mytable values (4, 'Mickey Mouse','5, Main, 
 Greenbay, Wisconsin',120000,12000)
Insert into Mytable values (5, 'Universal Studios','7, New road, 
 Danbury , Connecticut',45000,1600)
go

Results

ID, Name, Address, Salary, Bonus
home\sql2008 (HOME\MAK): (0 row(s) affected)
home\sql2008 (HOME\MAK): (1 row(s) affected)
home\sql2008 (HOME\MAK): (1 row(s) affected)
home\sql2008 (HOME\MAK): (1 row(s) affected)
home\sql2008 (HOME\MAK): (1 row(s) affected)
home\sql2008 (HOME\MAK): (1 row(s) affected)


Fig 1.1

The lsn_time_mapping table in the CDC schema records the lsn name and the beginning and end of the transaction. Let’s query the CDC.lsn_time_mapping table as shown below. [Refer Fig 1.2]

select * from cdc.lsn_time_mapping


Result

start_lsn, tran_begin_time, tran_end_time, tran_id
0x0000001C0000018B002F, 2008-01-16 01:53:38.840, 2008-01-16 01:53:38.840, 0x00000000029C
0x0000001C000001920003, 2008-01-16 01:53:38.857, 2008-01-16 01:53:38.857, 0x00000000029F
0x0000001C000001930003, 2008-01-16 01:53:38.857, 2008-01-16 01:53:38.857, 0x0000000002A0
0x0000001C000001940003, 2008-01-16 01:53:38.857, 2008-01-16 01:53:38.857, 0x0000000002A1
0x0000001C000001950003, 2008-01-16 01:53:38.857, 2008-01-16 01:53:38.857, 0x0000000002A2
home\sql2008 (HOME\MAK): (5 row(s) affected)


Fig 1.2

For every table that SQL Server is tracking, a table is created in the CDC schema with the naming convention SourceSchema_SourceTable_CT. In this case there is a table called dbo_MyTable_CT. [Refer Fig 1.3]


Fig 1.3

Now Let’s query the table dbo_MyTable_CT as shown below. [Refer Fig 1.4]

select * from cdc.dbo_MyTable_CT

Result

__$start_lsn, __$end_lsn, __$seqval, __$operation, __$update_mask, ID, Name
0x0000001C0000018B002F, NULL, 0x0000001C0000018B002E, 2, 0x03, 1, Dance Doll
0x0000001C000001920003, NULL, 0x0000001C000001920002, 2, 0x03, 2, Rainbow Colors
0x0000001C000001930003, NULL, 0x0000001C000001930002, 2, 0x03, 3, River Dance
0x0000001C000001940003, NULL, 0x0000001C000001940002, 2, 0x03, 4, Mickey Mouse
0x0000001C000001950003, NULL, 0x0000001C000001950002, 2, 0x03, 5, Universal Studios
home\sql2008(HOME\MAK): (5 row(s) affected)


Fig 1.4

Step 2

Now Let’s update and delete some data from the table as shown below. [Refer Fig 1.5]

use MyDataBase
go
Update MyTable set salary = 125000 where id = 4
go
delete Mytable where Id =4
go

Result

home\sql2008(HOME\MAK): (1 row(s) affected)
home\sql2008(HOME\MAK): (1 row(s) affected)


Fig 1.5

Query the lsn_time_mapping table as shown below.

select * from cdc.lsn_time_mapping

Result

start_lsn, tran_begin_time, tran_end_time, tran_id
0x0000001C0000018B002F, 2008-01-16 01:53:38.840, 2008-01-16 01:53:38.840, 0x00000000029C
0x0000001C000001920003, 2008-01-16 01:53:38.857, 2008-01-16 01:53:38.857, 0x00000000029F
0x0000001C000001930003, 2008-01-16 01:53:38.857, 2008-01-16 01:53:38.857, 0x0000000002A0
0x0000001C000001940003, 2008-01-16 01:53:38.857, 2008-01-16 01:53:38.857, 0x0000000002A1
0x0000001C000001950003, 2008-01-16 01:53:38.857, 2008-01-16 01:53:38.857, 0x0000000002A2
0x0000001C000001BB0004, 2008-01-16 02:08:37.357, 2008-01-16 02:08:37.357, 0x0000000002B1
home\sql2008(HOME\MAK): (6 row(s) affected)

You can see there is a new lsn entry in the table.

Query the cdc.dbo_MyTable_CT as shown below.

select * from cdc.dbo_MyTable_CT

Result

__$start_lsn, __$end_lsn, __$seqval, __$operation, __$update_mask, ID, Name
0x0000001C0000018B002F, NULL, 0x0000001C0000018B002E, 2, 0x03, 1, Dance Doll
0x0000001C000001920003, NULL, 0x0000001C000001920002, 2, 0x03, 2, Rainbow Colors
0x0000001C000001930003, NULL, 0x0000001C000001930002, 2, 0x03, 3, River Dance
0x0000001C000001940003, NULL, 0x0000001C000001940002, 2, 0x03, 4, Mickey Mouse
0x0000001C000001950003, NULL, 0x0000001C000001950002, 2, 0x03, 5, Universal Studios
0x0000001C000001BB0004, NULL, 0x0000001C000001BB0002, 1, 0x03, 4, Mickey Mouse
home\sql2008(HOME\MAK): (6 row(s) affected)

Let’s try to update a row and do not delete that row.

Update MyTable set salary = 1200 where id = 1
Update MyTable set name ='abc' where name ='Dance Doll'

Query the cdc.dbo_MyTable_CT as shown below.

select * from cdc.dbo_MyTable_CT

Result

__$start_lsn, __$end_lsn, __$seqval, __$operation, __$update_mask, ID, Name
0x0000001C0000018B002F, NULL, 0x0000001C0000018B002E, 2, 0x03, 1, Dance Doll
0x0000001C000001920003, NULL, 0x0000001C000001920002, 2, 0x03, 2, Rainbow Colors
0x0000001C000001930003, NULL, 0x0000001C000001930002, 2, 0x03, 3, River Dance
0x0000001C000001940003, NULL, 0x0000001C000001940002, 2, 0x03, 4, Mickey Mouse
0x0000001C000001950003, NULL, 0x0000001C000001950002, 2, 0x03, 5, Universal Studios
0x0000001C000001BB0004, NULL, 0x0000001C000001BB0002, 1, 0x03, 4, Mickey Mouse
0x0000001C000001E40004, NULL, 0x0000001C000001E40002, 3, 0x02, 1, Dance Doll
0x0000001C000001E40004, NULL, 0x0000001C000001E40002, 4, 0x02, 1, abc
home\sql2008(HOME\MAK): (8 row(s) affected)

As you can see from the data, it is obvious that the data changes are being tracked.

Note: This article is written based on the SQL Server 2008 – Nov CTP

Conclusion

This article has illustrated how to enable Change Data Capture on a database, on a table and how SQL Server tracks the data changes of the CDC enabled table. In the next article, we will see how to get the change in data in a useful form.

» See All Articles by Columnist MAK

Tools:
Add databasejournal.com to your favorites
Add databasejournal.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed

MS SQL Archives

Five Trends for Application Development & Program Management. Download Complimentary Report Now.
What's The Future Of IT? Find Out By Reading "IT in 2018" Now. Free Registration Required.
IT in 2018: Download Free eBook By The Author Of "Does IT Matter?" Simple Registration Is Required.
HP eBook: Using Business Service Management (BSM) to Manage Your Business Applications
Download: SQL Compare Pro 6--The fastest, easiest way to compare and synchronize two databases.


Latest Forum Threads
MS SQL Forum
Topic By Replies Updated
How To Transfer Access Data Records To SQL ?? ankurdjariwala 1 May 8th, 12:24 PM
problem with federated server linking majidkhan 1 April 29th, 10:00 AM
"SELECT rowguidcol" from tables on linked servers? brentbordelon 1 April 25th, 04:12 PM
"SELECT rowguidcol" vs. "SELECT <actual name>" rgarrison 9 April 16th, 03:46 PM







JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES