使用Change Tracking实现数据同步
通常的做法是自定义应用程序使用比如触发器、timestamp 列和新表组合来存储跟踪信息,同事还需要自定义清除程序清除过时的数据。
在日常应用中经常会有这样的需求,需要Audit那些数据更改,,或者需要跟踪更改的数据实现对数据的同步。(最常见的应用如数据仓库数据同步,因为数据量巨大,需要将数据的更改同步到数据仓库,这种同步不要求实时)。
通常的做法是自定义应用程序使用比如触发器、timestamp 列和新表组合来存储跟踪信息,同事还需要自定义清除程序清除过时的数据。在SQL Server 2008以后提供了一个功能更改跟踪(Change Tracking).这一种轻量型解决方案(相对于自己自定义的程序,性能要高)为应用程序提供了一种有效的更改跟踪机制。
注意:用程序需要有关所有所做更改的信息以及所更改数据的中间值,则可能适合使用变更数据捕获,而不适合使用更改跟踪。
工作原理:为表配置了更改跟踪后,任何影响该表中的行的 DML 语句都将导致针对每个有所修改的行的更改跟踪信息被记录下来。更改信息会记录到SQL Server内部表中,可以使用查询到内部表,使用CHANGETABLE函数获得数据更改信息。
下面是启动Change Tracking并且获得更改数据的脚本:
--创建测试数据库和表
go
20)NULL,
(
[MAXID] ASC
--在数据库和表启动更改跟踪:
2
-- AUTO_CLEANUP = ON在经过指定的保持期后会自动删除更改跟踪信息。
ENABLE CHANGE_TRACKING
如果表没有主键启动更改跟踪会出现下面的错误:
Msg 4997, Level16, State 1, Line 1
Cannot enablechange tracking on table 'A'. Change tracking requires a primary key on thetable. Create a primary key on the table before enabling change tracking.
原因:主键列值是来自所跟踪的并记录更改信息的表中的唯一信息。这些值用于标识发生更改的行。要获取这些行的最新数据,应用程序可以使用主键列值联接源表和所跟踪的表。
(也可以在SSMS中启用更改跟踪)
-- 查询数据库和表更改跟踪信息
--返回与上次提交的事务相关联的版本
--指定的表中获取更改跟踪信息的最低版本
0
--插入测试数据并且查询跟踪信息最新版本号
1
2
go
go
1
go
0
2
--将更改跟踪信息与原表信息关联获得最新数据(这些数据就是我们需要同步的数据)
2
GO
0
GO
CHANGETABLE返回值:
注意:
更多信息参考MSDN:更改跟踪概述

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Microsoft SQL Server is a relational database management system launched by Microsoft. It is a comprehensive database platform that uses integrated business intelligence (BI) tools to provide enterprise-level data management. It is easy to use, has good scalability, and has a high degree of integration with related software. High advantages. The SQL Server database engine provides more secure and reliable storage functions for relational data and structured data, allowing users to build and manage highly available and high-performance data applications for business.

SQLServer or MySQL? The latest research reveals the best database selection. In recent years, with the rapid development of the Internet and big data, database selection has become an important issue faced by enterprises and developers. Among many databases, SQL Server and MySQL, as the two most common and widely used relational databases, are highly controversial. So, between SQLServer and MySQL, which one should you choose? The latest research sheds light on this problem for us. First, let

With the popularity of the Internet, website and application development has become the main business of many companies and individuals. PHP and SQLServer database are two very important tools. PHP is a server-side scripting language that can be used to develop dynamic websites; SQL Server is a relational database management system developed by Microsoft and has a wide range of application scenarios. In this article, we will discuss the development of PHP and SQL Server, as well as their advantages, disadvantages and application methods. First, let's

In web development, the combination of PHP and MySQL is very common. However, in some cases, we need to connect to other types of databases, such as SQL Server. In this article, we will cover five different ways to connect to SQL Server using PHP.

Introduction to how to use PDO to connect to a Microsoft SQL Server database: PDO (PHPDataObjects) is a unified interface for accessing databases provided by PHP. It provides many advantages, such as implementing an abstraction layer of the database and making it easy to switch between different database types without modifying a large amount of code. This article will introduce how to use PDO to connect to a Microsoft SQL Server database and provide some related code examples. step

With the continuous development of the Internet, database selection has become increasingly important. Among the many databases, SQLServer and MySQL are two high-profile options. SQLServer is a relational database management system developed by Microsoft, while MySQL is an open source relational database management system. So how to choose the best database solution between SQLServer and MySQL? First, we can compare these two databases in terms of performance. SQLServer is processing

SQL Server vs. MySQL: Which database is more suitable for high availability architecture? In today's data-driven world, high availability is one of the necessities for building reliable and stable systems. As the core component of data storage and management, the database's high availability is crucial to the business operation of the enterprise. Among the many databases, SQLServer and MySQL are common choices. So in terms of high availability architecture, which database is more suitable? This article will compare the two and give some suggestions.

SQLServer and MySQL are currently two very popular relational database management systems (RDBMS). They are both powerful tools for storing and managing large-scale data. However, they have some differences in handling large-scale data. This article will compare SQL Server and MySQL, focusing on their suitability for large-scale data processing. First, let us understand the basic characteristics of SQLServer and MySQL. SQLServer is developed by Microsoft
