Sql Server 更改数据捕获:添加列时保留历史记录?

发布于 2024-08-28 19:29:23 字数 1814 浏览 8 评论 0原文

将新列添加到配置为更改数据捕获 (cdc) 的表时,捕获实例表不会有新列,直到为源表禁用并重新启用 cdc。在此过程中,现有的捕获实例将被删除。

我想我可以将现有数据复制到临时表中,然后使用以下 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')

When a new column is added to table that is configured for change data capture (cdc), the capture instance table will not have the new column until cdc is disabled and re-enabled for the source table. In the process the existing capture instance is dropped.

I thought I could copy existing data out to a temp table and then copy back using the following SQL. However, other CDC meta information, such as the cdc.change_tables.start_lsn, becomes invalid.

How can the capture instance history be preserved, using the same capture instance name, if at all?

Thanks,
Rich

/*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')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缘字诀 2024-09-04 19:29:23

丰富,

保存此类数据的最佳方法是创建一个暂存持久表来定期捕获 _CT 表数据。了解 cdc 数据在被端点(仓库/数据集市等)使用之前通常具有较短的保质期,您可以确保任何更改在维护窗口内完成,此时 _CT 表数据被复制到在实施更改之前进行分阶段。

其中需要考虑的一个方面是,一旦 _CT 架构发生更改(通过添加或删除一列或多列),用于将该数据拉出到端点的过程也必须更新。

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

希望这能提供一些思考。

Rich,

The best method to preserve such data is to create a staging persisted table to capture the _CT table data periodically. Knowing that cdc data generally has a short shelf-life prior to be consumed by the endpoint (warehouse/data mart, etc.) you can ensure that any changes are completed within a maintenance window at which time the _CT table data is copied off into staging prior to the changes being implemented.

The one aspect to consider in this is that once the _CT schema has been changed (by adding or removing one or more columns) the process used to pull that data out into the endpoint must also be updated.

To overcome this we implemented a script store that stores the intended schema of the staging table (used between _CT and endpoint) and once the changes are implemented on the client DB, then we move the data from staging into endpoint and update the staging schema.

Hopefully this will provide food for thought.

〆凄凉。 2024-09-04 19:29:23

我认为您还必须写出 lsn 记录,然后将它们带回到 lsntimemapping 表中。

I think you'd have to write out the lsn records also and then bring them back into the lsntimemapping table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文