2010-04-16 47 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和端點之間使用)的預期模式的腳本存儲,並且一旦在客戶數據庫上實現了更改,我們將數據從暫存轉移到端點並更新暫存模式。

希望這會爲思想提供食物。

相關問題