1.Enable the change tracking at the database level.

ALTER DATABASE AdventureWorks2008 SET CHANGE_TRACKING = ON;

By Default retention is 2 dyas with auto clean up on.We can use below SQL to check it.

SELECT * FROM sys.change_tracking_databases

database_id  is_auto_cleanup_on      retention_period         retention_period_units         retention_period_units_desc
-----------      ------------------         ----------------         ---------------------------------     -----------------------------
6                    1                                  2                           3                                        DAYS

SQLServer allow us to sepcify the retention and the auto clean up.
When try to enable the change tracking if it already enabled, then need to disable it first

ALTER DATABASE AdventureWorks2008 SET CHANGE_TRACKING = OFF
GO
ALTER DATABASE AdventureWorks2008 SET CHANGE_TRACKING = ON (AUTO_CLEANUP=ON, CHANGE_RETENTION=1 hours);

SELECT   *  FROM sys.change_tracking_databases
database_id         is_auto_cleanup_on       retention_period       retention_period_units       retention_period_units_desc
-----------------   --------------------------- ---------------------  -----------------------------  --------------------------------
6                              1                              1                             2                                 HOURS

Note:
If you got error message saying "Change tracking is enabled for one or more tables in database 'AdventureWorks2008'.
Disable change tracking on each table before disabling it for the database.
Use the sys.change_tracking_tables catalog view to obtain a list of tables for which change tracking is enabled."

use below SQL to find out the table and disable it first. refer to section 2 on how to disable the change tracking on table level.

select object_name(object_id),* from sys.change_tracking_tables

After enable the Change tracking on database level ,then SQLServer create a system internal table sys.syscommittab.
Which we can not query use SELECT except the admin connected in the DAC mode.
But we can see what columns the table have via query th sys.all_columns catalog.

SELECT object_id, name
FROM sys.all_columns
WHERE object_id = OBJECT_ID('sys.syscommittab');

object_id               name 
----------------  -------------------
2089058478       commit_ts
2089058478       xdes_id 
2089058478       commit_lbn
2089058478       commit_csn
2089058478       commit_time
2089058478       dbfragid

--Column Name-- --Type---- -----------Description------------------------------------
commit_ts          BIGINT                The ascending CSN for the transaction
xdes_id              BIGINT                The internal identifi er for the transaction
commit_lbn         BIGINT                The log block number for the transaction
commit_csn        BIGINT                 The instance-wide sequence number for the transaction
commit_time       DATETIME             The time the transaction was committed
dbfragid INT      reserved for future use

List All the change tracking commit history.

select * from sys.dm_tran_commit_table

2. Enable the change tracking on table level.

ALTER TABLE HumanResources.Employee ENABLE CHANGE_TRACKING

SQLServer allow track the update on each column, you need to set option when enable table level tracking.

Disable it first when try to change the option.

ALTER TABLE HumanResources.Employee DISABLE CHANGE_TRACKING

GO

ALTER TABLE HumanResources.Employee ENABLE CHANGE_TRACKING WITH (TRACK_COLUMNS_UPDATED = ON);

list all the tables that already enabled the change tracking feature.

select object_name(object_id),* from sys.change_tracking_tables

For each table, SQLServer will create a system internal table in temp database.

select * from sys.objects where NAME like 'change_tracking_%'

3. Examples on how to use the change tracking feature.

user can set some identifier content on the change contest

DECLARE @context VARBINARY(128) =
CONVERT(VARBINARY(128), SUSER_SNAME());

WITH CHANGE_TRACKING_CONTEXT(@context)
UPDATE AdventureWorks2008.HumanResources.Employee
SET
JobTitle = 'Production Engineer'
WHERE
BUSINESSENTITYID=290

SELECT CHANGE_TRACKING_IS_COLUMN_IN_MASK (
(OBJECT_ID('AdventureWorks2008.HumanResources.Employee'), 'JobTitle', 'ColumnId'),
0x0000000006000000);

select CHANGE_TRACKING_MIN_VALID_VERSION(OBJECT_ID('AdventureWorks2008.HumanResources.Employee'));

select CHANGE_TRACKING_CURRENT_VERSION()

declare @last_version bigint=1;
select * from changetable(changes AdventureWorks2008.HumanResources.Employee,@last_version)
c left outer join AdventureWorks2008.HumanResources.Employee d on c.businessentityid=d.businessentityid

SYS_CHANGE_VERSION SYS_CHANGE_CREATION_VERSION SYS_CHANGE_OPERATION SYS_CHANGE_COLUMNS SYS_CHANGE_CONTEXT BusinessEntityID BusinessEntityID NationalIDNumber LoginID OrganizationNode OrganizationLevel JobTitle BirthDate MaritalStatus Gender HireDate SalariedFlag VacationHours SickLeaveHours CurrentFlag rowguid ModifiedDate
-------------------- --------------------------- -------------------- ----------------------------------------------------------------------- ---------------- ---------------- ---------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ----------------- -------------------------------------------------- ---------- ------------- ------ ---------- ------------ ------------- -------------- ----------- ------------------------------------ -----------------------
3 NULL U 0x0000000006000000 0x7000720069006E0063006500730073006400 290 290 134219713 adventure-works\ranjit0 0x95EF 3 Production 88888 1969-10-31 S M 2006-07-01 1 34 37 1 604213F9-DD0F-43B4-BDD2-C96E93D3F4BF 2008-07-31 00:00:00.000

SELECT
c.SYS_CHANGE_VERSION,
c.SYS_CHANGE_CONTEXT,
e.*
FROM AdventureWorks2008.HumanResources.Employee e
CROSS APPLY CHANGETABLE
(
VERSION AdventureWorks2008.HumanResources.Employee,
(BusinessEntityId),
(e.BusinessEntityId)
) c where c.SYS_CHANGE_VERSION is not null;

SYS_CHANGE_VERSION SYS_CHANGE_CONTEXT BusinessEntityID NationalIDNumber LoginID OrganizationNode OrganizationLevel JobTitle BirthDate MaritalStatus Gender HireDate SalariedFlag VacationHours SickLeaveHours CurrentFlag rowguid ModifiedDate
-------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ---------------- ---------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ ----------------- -------------------------------------------------- ---------- ------------- ------ ---------- ------------ ------------- -------------- ----------- ------------------------------------ -----------------------
3 0x7000720069006E0063006500730073006400 290 134219713 adventure-works\ranjit0 0x95EF 3 Production 88888 1969-10-31 S M 2006-07-01 1 34 37 1 604213F9-DD0F-43B4-BDD2-C96E93D3F4BF 2008-07-31 00:00:00.000

Change Tracking of SQLServer的更多相关文章

  1. Oracle 10g Block Change Tracking特性

    Using Block Change Tracking to Improve Incremental Backup Performance 使用块改变跟踪改善增量备份的性能 The block cha ...

  2. SQL Server审计功能入门:更改跟踪(Change Tracking)

    原文:SQL Server审计功能入门:更改跟踪(Change Tracking) 介绍 更改跟踪是一种轻量型解决方案,它为应用程序提供了一种有效的更改跟踪机制.常规的,自定义变更跟踪和读取跟踪数据, ...

  3. Entity Framework Code First - Change Tracking

    In this post we will be discussing about change tracking feature of Entity Framework Code First. Cha ...

  4. Oracle Block Change Tracking功能(转)

    from:http://space.itpub.net/?uid-25744374-action-viewspace-itemid-732091 通过使用block change tracking功能 ...

  5. Oracle 块修改跟踪 (Block Change Tracking) 说明

    Block ChangeTracking 是Oracle 10g里推出的特性.官网对Block change tracking 的定义如下: Adatabase option that causes ...

  6. enable or disable Oracle block change tracking

    Oracle的block change tracking用于记录上次备份以来改变过的block信息,因此打开block change tracking可以大大加快增量备份的速度. 1. Enable ...

  7. Entity Framework Tutorial Basics(19):Change Tracking

    Change Tracking in Entity Framework: Here, you will learn how entity framework tracks changes on ent ...

  8. Block Change Tracking (块改变跟踪)

    理论背景:Block ChangeTracking 是Oracle 10g里推出的特性. Block change tracking 会记录data file里每个block的update 信息,这些 ...

  9. Dynamics 365 Customer Enagement中的更改跟踪(change tracking)

    我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...

随机推荐

  1. 验证标题是否存在(TextBox控件失去焦点验证)

    首先解释两个属性, AutoPostBack 属性用于设置或返回当用户在 TextBox 控件中按 Enter 或 Tab 键时,是否发生自动回传到服务器的操作. 如果把该属性设置为 TRUE,则启用 ...

  2. CSS3选择器(三)之伪类选择器

    伪类选择器对于大家来说最熟悉的莫过于:link,:focus,:hover之类的了,因为这些在平时中是常用到的伪类选择器,那么先和大家一起简单总 结一下CSS中常用的伪类选择器的使用方法,最后把重心放 ...

  3. Myeclipse10 + JBPM4.4 环境搭建图文教程

    一.软件环境 IDE:Myeclipse10.0 (jbpm4.0以上版本好像只能与Myeclipse7.5以上版本集成) JBPM:4.4 与Myeclipse集成 1.解压jbpm-4.4.zip ...

  4. VBS发送邮件-1

    Set objMail = Server.CreateObject("CDO.Message") Set objCDOSYSCon = Server.CreateObject(&q ...

  5. 【iM_VGA模块】运行 ucgui 演示!

    挂在 iCore2 双核心板上的 VGA模块,跑 ucgui DEMO 演示.大家看看! ============================== 技术论坛:http://www.eeschool ...

  6. PHP 命名空间总结

    PHP 5.3 及以上版本中引入了命名空间 的概念. notes: 1. 在 PHP 中,命名空间用来解决在编写 类库 或 应用程序 时创建 可重用 的 代码如 类 或 函数 时碰到的两类问题: ① ...

  7. Web 在线文件管理器学习笔记与总结(2)显示文件列表(名称,类型,大小,可读,可写,可执行,创建时间,修改时间,访问时间)

    主要函数: filetype() 判断文件类型 filesize() 得到文件大小(字节) is_readable() 判断文件是否可读 is_writeable() 判断文件是否可写 is_exec ...

  8. cURL 学习笔记与总结(4)使用 cURL 从 ftp 上下载文件与上传文件到 ftp

    下载: <?php $curlobj = curl_init(); curl_setopt($curlobj, CURLOPT_URL, "ftp://192.***.*.***/文件 ...

  9. UIButton 去除按下效果(阴影)

    [btn setImage:[UIImage imageNamed:@"test.png"] forState:UIControlStateNormal];btn.adjustsI ...

  10. 20145235 《Java程序设计》第9周学习总结

    第十六章 整合数据库 JDBC Java语言访问数据库的一种规范,是一套API. JDBC (Java Database Connectivity) API,即Java数据库编程接口,是一组标准的Ja ...