2010-04-16 46 views
5

当一个新的列被添加到被配置成用于变更数据捕获(CDC)表中,捕获实例表不会有新列直到CDC被禁用,并且重新启用源表。在这个过程中,现有的捕获实例被删除。Sql Server更改数据捕获:添加列时保留历史记录?

我以为我可以现有的数据复制出一个临时表,然后复制回使用以下SQL。但是,其他CDC元信息(例如cdc.change_tables.start_lsn)无效。

如何捕获实例历史记录保存,使用相同的捕获实例的名称,如果在所有?

感谢, 丰富

/*Change Data Capture Test - Alter table definition test */ 

/*Enter restricted mode so we don't lose data changes during this process*/ 
alter database ChangeDataCaptureTest set AUTO_UPDATE_STATISTICS_ASYNC OFF 
alter database ChangeDataCaptureTest set RESTRICTED_USER with ROLLBACK IMMEDIATE 
go 

/*Add a column to the table*/ 
alter table dbo.Table1 add value3 varchar(20) DEFAULT '' not null 

/*Copy the existing change tracking into a temp table*/ 
select * into cdc.dbo_Table1_temp from cdc.dbo_Table1_CT 

/*Add the new column to the temp table so that we don't have to map 
all columns when we copy back, note that we use NULL as the default*/ 
alter table cdc.dbo_Table1_temp add value3 varchar(20) DEFAULT NULL 

/*Disable CDC on the source table, this will drop the associated cdc table*/ 
exec sys.sp_cdc_disable_table 
@source_schema='dbo', 
@source_name='Table1', 
@capture_instance='dbo_Table1' 

/*Enable CDC for the table which recreates the CDC table*/ 
EXEC sys.sp_cdc_enable_table 
@source_schema = N'dbo', 
@source_name = N'Table1', 
@role_name  = NULL, 
@supports_net_changes = 1, 
@filegroup_name = N'ChangeDataCapture' 
GO 

/*Insert values from the temp table back into the new CDC Table*/ 
Insert into cdc.dbo_Table1_CT 
SELECT * 
From cdc.dbo_Table1_temp 
go 

/*Drop the temp table*/ 
drop table cdc.dbo_Table1_temp 

/*Go back into multi-user mode*/ 
alter database ChangeDataCaptureTest set AUTO_UPDATE_STATISTICS_ASYNC ON 
alter database ChangeDataCaptureTest set MULTI_USER 
go 

/*Add a new row to the table*/ 
insert into table1 
values(12,'zz','g') 
+0

您可能无法将它们放回同一张表中。将它们复制到一个新表中。在检索报告时,'联合'该表以获取以前的记录。 – 2013-01-10 05:03:35

回答

-2

我想你也必须写出来的LSN记录,然后把它们放回lsntimemapping表。

0

丰富,

最好的方法来保护这些数据是创建一个分段持续表周期性地捕获_CT表数据。由于知道cdc数据在被端点(仓库/数据集市等)消耗之前通常具有较短的货架期,因此可以确保在维护窗口内完成任何更改,此时_CT表数据被复制到在实施变更之前进行分期。

的一个方面在此考虑的是,一旦_CT架构已被改变(通过添加或删除一个或多个列),用于拉出该数据到端点的处理也必须被更新。

为了克服这个问题,我们实现了一个存储暂存表(在_CT和端点之间使用)的预期模式的脚本存储,并且一旦在客户数据库上实现了更改,我们将数据从暂存转移到端点并更新暂存模式。

希望这会为思想提供食物。

相关问题