开启
ALTER DATABASE [数据库名]
SET CHANGE_TRACKING = ON
(CHANGE_RETENTION = 【Ct日志保留天数】 DAYS, AUTO_CLEANUP = ON);
关闭
ALTER DATABASE [数据库名]
SET CHANGE_TRACKING = OFF
2、表CT
开启
ALTER TABLE dbo.[表名]
ENABLE CHANGE_TRACKING
WITH (TRACK_COLUMNS_UPDATED = ON); -- 可选参数,是否跟踪更改过的列
关闭
ALTER TABLE dbo.[表名]
Disabled CHANGE_TRACKING
3、查询最新版本
SELECT CHANGE_TRACKING_CURRENT_VERSION()
4、查询新的变更
SELECT
CT.SYS_CHANGE_VERSION,
CT.SYS_CHANGE_OPERATION,
CT.[变更表的主键]
FROM CHANGETABLE([表名], [上一次更新的版本号]) AS CT
5、查看数据库是否启用更改跟踪
SELECT DB_NAME(database_id) DataBaseName,is_auto_cleanup_on,
retention_period,retention_period_units_desc
FROM sys.change_tracking_databases
6、查询开启变更的表
SELECT OBJECT_NAME(object_id) TableName,is_track_columns_updated_on
FROM sys.change_tracking_tables