SQL Server 2008。是否可以为数据库中的多个相关表创建更改数据捕获(或更改跟踪)?例如多对多关系。
它会是什么样子?
SQL Server 2008。是否可以为数据库中的多个相关表创建更改数据捕获(或更改跟踪)?例如多对多关系。
它会是什么样子?
从未使用过它,但从MSDN 文档中我会说:是的!
首先准备数据库:
ALTER DATABASE [yourdb] SET ALLOW_SNAPSHOT_ISOLATION ON
ALTER DATABASE [yourdb]
SET CHANGE_TRACKING = ON
(CHANGE_RETENTION = 2 DAYS, AUTO_CLEANUP = ON)
现在在所需表上启用更改跟踪:
ALTER TABLE [yourdb].[yourschema].[yourtable]
ENABLE CHANGE_TRACKING
查询更改:
IF @sync_initialized = 0
SELECT *
FROM [yourschema].[yourtable] LEFT OUTER JOIN
CHANGETABLE(CHANGES [yourschema].[yourtable], @sync_last_received_anchor) CT
ON CT.[yourkey] = [yourschema].[yourtable].[yourkey]
ELSE
BEGIN
SELECT *
FROM Sales.Customer
JOIN CHANGETABLE(CHANGES [yourschema].[yourtable], @sync_last_received_anchor) CT
ON CT.[yourkey] = [yourschema].[yourtable].[yourkey]
WHERE (CT.SYS_CHANGE_OPERATION = 'I'
AND CT.SYS_CHANGE_CREATION_VERSION
<= @sync_new_received_anchor)
END