微软介绍了
SQL Server 2008的一项新功能:捕获数据变化
。捕获数据变化功能可以方便地监控到表的变化
。本文的第一、二部分介绍了怎样在数据库及表上开启捕获数据变化功能。下面将介绍的是
,如果表的结构发生变化
,捕获数据变化功能将有什么样的表现。
注:本文章基于
SQL Server 200811月CTP。
第一步
用下面的代码创建一个名为“CDCDB”的数据库。
USE [master]
GO
/*** Object: Database [CDCDB] Script Date: 01/07/2008 18:46:15 ***/
IF EXISTS (SELECT name FROM sys.databases WHERE name = N'CDCDB')
DROP DATABASE [CDCDB]
GO
USE [master]
GO
/*** Object: Database [CDCDB] Script Date: 01/07/2008 18:46:33 ***/
CREATE DATABASE [CDCDB]
GO
第二步
创建一个名为Employee的表,代码如:
use [CDCDB]
go
/*** Object: Table [dbo].[Employee] Script Date: 01/07/2008 18:52:14 ***/
IF EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[Employee]')
AND type in (N'U'))
DROP TABLE [dbo].[Employee]
GO
use [CDCDB]
go
/*** Object: Table [dbo].[Employee] Script Date: 01/07/2008 18:52:26 ***/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Employee](
[ID] [int] NOT NULL,
[Name] [varchar](100) NULL,
CONSTRAINT [Employee_PK] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
第三步
在数据库“CDCDB”上开启捕获数据变化功能,如下:
USE [CDCDB]
GO
EXEC sys.sp_cdc_enable_db_change_data_capture
GO
第四步
在表“Employee”上开启捕获数据变化功能,如下:
use [CDCDB]
go
GO
EXEC sys.sp_cdc_enable_table_change_data_capture
@source_schema = 'dbo',
@source_name = 'Employee',
@role_name = 'cdc_Employee'
GO