Change Tracking of SQLServer
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的更多相关文章
- Oracle 10g Block Change Tracking特性
Using Block Change Tracking to Improve Incremental Backup Performance 使用块改变跟踪改善增量备份的性能 The block cha ...
- SQL Server审计功能入门:更改跟踪(Change Tracking)
原文:SQL Server审计功能入门:更改跟踪(Change Tracking) 介绍 更改跟踪是一种轻量型解决方案,它为应用程序提供了一种有效的更改跟踪机制.常规的,自定义变更跟踪和读取跟踪数据, ...
- Entity Framework Code First - Change Tracking
In this post we will be discussing about change tracking feature of Entity Framework Code First. Cha ...
- Oracle Block Change Tracking功能(转)
from:http://space.itpub.net/?uid-25744374-action-viewspace-itemid-732091 通过使用block change tracking功能 ...
- Oracle 块修改跟踪 (Block Change Tracking) 说明
Block ChangeTracking 是Oracle 10g里推出的特性.官网对Block change tracking 的定义如下: Adatabase option that causes ...
- enable or disable Oracle block change tracking
Oracle的block change tracking用于记录上次备份以来改变过的block信息,因此打开block change tracking可以大大加快增量备份的速度. 1. Enable ...
- Entity Framework Tutorial Basics(19):Change Tracking
Change Tracking in Entity Framework: Here, you will learn how entity framework tracks changes on ent ...
- Block Change Tracking (块改变跟踪)
理论背景:Block ChangeTracking 是Oracle 10g里推出的特性. Block change tracking 会记录data file里每个block的update 信息,这些 ...
- Dynamics 365 Customer Enagement中的更改跟踪(change tracking)
我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...
随机推荐
- Js获取当前日期时间及其它格式化操作
Js获取当前日期时间及其它操作 var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); ...
- HTML中为何P标签内不可包含DIV标签?
起因:在做项目时发现原本在DW中无误的代码到了MyEclipse6.0里面却提示N多错误,甚是诧异.于是究其原因,发现块级元素P内是不能嵌套DIV的. 深究:我们先来认识in-line内联元素和blo ...
- 给iOS工程增加Daily Build
给iOS工程增加Daily Build 前言 Daily Build 是一件非常有意义的事情,也是敏捷开发中关于 "持续集成" 的一个实践.Daily Build 对于开发来说有 ...
- juniper-cisco-HP上网设置
网络拓扑: 路由模式: 第一步.配置防火墙的接口地址 编辑外网接口: 配置内网口(原理同上)(interface mode:nat) 第二步.配置防火墙的路由 第三步.配置防火墙安全策略 Cisco交 ...
- jquery限制div字符长度,超出部分已“…”显示
$(".content").each(function(){ if($(this).text().length>100){ $(this).text($(this).text ...
- hdu 最大报销额
本题也是一个背包的问题,我觉得这道题的核心就是根据精确度将浮点型转化为整型然后利用动态规划进行求解,注意对题意的理解,有3种支票是不能够报销的. 我开始照着这个思路进行思考,但是敲出来的第一个代码居然 ...
- 当一回Android Studio 2.0的小白鼠
上个星期就放出了Android studio出2.0的消息,看了一下what's new 简直抓到了那个蛋疼的编译速度痛点.在网上稍微搜索了一下后发现基本都是介绍视频.一番挣扎后(因为被这IDE坑过几 ...
- HttpRequestDeviceUtils
import javax.servlet.http.HttpServletRequest;public class HttpRequestDeviceUtils { /**Wap网关Via头信息中特有 ...
- Java判断一个时间是否在另一个时间段内
需求:当时间在凌晨0点至0点5分之间程序不执行. 也就是实现判断当前时间点是否在00:00:00至00:05:00之间 方法: Java代码 : /** * 判断时间是否在时间段内 * * @para ...
- linux网卡设置详解
centos7安装之后是需要在网卡配置文件中开始网络连接 onboot =yes 刚开始时网卡获取IP模式是dhcp 你会发现ifconfig不能用,猜测是废弃了,你要yum install net- ...