在SQL Server 2008中使用變更跟蹤獲得資料變更
資料變更需要與一個基線做對比。當你首先將你的客戶端(一個NET應用程式和/或另一個SQL Server表)與HumanResources.Department表同步時,你從這個表的所有記錄獲得一個原始的資料集。你還獲得基線版本數字,例如這時所有記錄的最大版本數字,並將它記錄下來用於下一次同步。下一次同步時,系統確定哪些記錄從這個基線版本以來進行了修改,而一個新的基線版本被儲存下來用於下次的同步。這個演算法如下所示。
-- Obtain the current synchronization version. This will be used the next time CHANGETABLE(CHANGES...) is called. SET @synchronization_version = CHANGE_TRACKING_CURRENT_VERSION(); -- If this is the first synchronization session IF (@sync_initialized = 0) BEGIN -- Initialize from the table SELECT DepartmentID, Name, GroupName, ModifiedDate FROM HumanResources.Department END ELSE BEGIN -- Obtain incremental changes by using the synchronization version obtained the last time the data was synchronized. SELECT CT.SYS_CHANGE_OPERATION, DepartmentID, Name, GroupName, ModifiedDate FROM HumanResources.Department RIGHT OUTER JOIN CHANGETABLE(CHANGES HumanResources.Department, @last_synchronization_version) AS CT ON P.DepartmentID = CT.DepartmentID END |
DECLARE @synchronization_version bigint -- Obtain the current synchronization version. This will be used the next time CHANGETABLE(CHANGES...) is called. SET @synchronization_version = CHANGE_TRACKING_CURRENT_VERSION() SELECT @synchronization_version -- Initialize from the base table SELECT DepartmentID, Name, GroupName, ModifiedDate FROM HumanResources.Department |
讓我們插入一條新的記錄到這個表中並更新一條已有的記錄。
INSERT INTO HumanResources.Department
(Name, GroupName, ModifiedDate)
VALUES ('Product Design', 'Research and Development', GETDATE())
UPDATE HumanResources.Department
SET GroupName='Accounting'
WHERE DepartmentID=10
要獲得上次版本0以來的變更,執行下面的指令碼。
DECLARE @synchronization_version bigint -- Obtain the current synchronization version. This will be used the next time CHANGETABLE(CHANGES...) is called. SET @synchronization_version = CHANGE_TRACKING_CURRENT_VERSION() SELECT @synchronization_version -- Obtain incremental changes by using the synchronization version obtained the last time the data was synchronized. SELECT CT.SYS_CHANGE_VERSION, CT.SYS_CHANGE_OPERATION, CT.DepartmentID, Name, GroupName, ModifiedDate FROM HumanResources.Department D RIGHT OUTER JOIN CHANGETABLE(CHANGES HumanResources.Department, 0) AS CT ON D.DepartmentID = CT.DepartmentID |
如果你在表HumanResources.Department 上啟用了欄位跟蹤,那麼你可以只從變更了的欄位獲取資料。
DECLARE @synchronization_version bigint -- Obtain the current synchronization version. This will be used the next time CHANGETABLE(CHANGES...) is called. SET @synchronization_version = CHANGE_TRACKING_CURRENT_VERSION() SELECT @synchronization_version -- Obtain incremental changes by using the synchronization version obtained the last time the data was synchronized. SELECT CT.SYS_CHANGE_VERSION, CT.SYS_CHANGE_OPERATION, CT.DepartmentID, CASE CHANGE_TRACKING_IS_COLUMN_IN_MASK (COLUMNPROPERTY(OBJECT_ID('HumanResources.Department'), 'Name', 'ColumnId'), SYS_CHANGE_COLUMNS) WHEN 1 THEN Name ELSE NULL END as Name, CASE CHANGE_TRACKING_IS_COLUMN_IN_MASK(COLUMNPROPERTY (OBJECT_ID('HumanResources.Department'), 'GroupName', 'ColumnId'), SYS_CHANGE_COLUMNS) WHEN 1 THEN GroupName ELSE NULL END AS GroupName, ModifiedDate FROM HumanResources.Department D RIGHT OUTER JOIN CHANGETABLE(CHANGES HumanResources.Department, 0) AS CT ON D.DepartmentID = CT.DepartmentID |
讓我們刪除新新增的記錄。
DELETE FROM HumanResources.Department WHERE DepartmentID=17 要獲得從上次版本2以來的變更,執行之前的指令碼,使用新版本2。 DECLARE @synchronization_version bigint -- Obtain the current synchronization version. This will be used the next time CHANGETABLE(CHANGES...) is called. SET @synchronization_version = CHANGE_TRACKING_CURRENT_VERSION() SELECT @synchronization_version -- Obtain incremental changes by using the synchronization version obtained the last time the data was synchronized. SELECT CT.SYS_CHANGE_VERSION, CT.SYS_CHANGE_OPERATION, CT.DepartmentID, Name, GroupName, ModifiedDate FROM HumanResources.Department D RIGHT OUTER JOIN CHANGETABLE(CHANGES HumanResources.Department, 2) AS CT ON D.DepartmentID = CT.DepartmentID |
因為如果AUTO_CLEANUP選項設定為開啟,那麼變更跟蹤資訊可以在下次同步執行之前被刪除,我們還需要將@last_synchronization_version和用於特定表的變更跟蹤表中儲存的最小版本相比較。如果@last_synchronization_version小於CHANGE_TRACKING_MIN_VALID_VERSION(OBJECT_ID('HumanResources.Department')),那麼我們需要重新初始化並獲得一個包含HumanResources.Department base表所有記錄的新基線。因此,這個條件
IF (@sync_initialized = 0)
需要改為
IF (@last_synchronization_version 在一個繁忙的系統上,在獲得當前版本和從CHANGETABLE()函式獲得變更之間,表HumanResources.Department 有可能被其它會話改變,或變更跟蹤資訊可能被清理程式刪除了並啟動CHANGE_TRACKING_MIN_VALID_VERSION來新增。因此,最好使用快照隔離來確保在事務過程中所有的變更跟蹤資訊是一致的。完整的演算法如下所示。
在資料庫上啟用快照隔離會對資料庫增加不小的效能成本。如果你傾向於不使用快照隔離,那麼一個獲得變更的替代演算法如下所示。
SET TRANSACTION ISOLATION LEVEL SNAPSHOT;
BEGIN TRAN
DECLARE @synchronization_version bigint
-- Obtain the current synchronization version. This will be used the next time CHANGETABLE(CHANGES...) is called.
SET @synchronization_version = CHANGE_TRACKING_CURRENT_VERSION()
SELECT @synchronization_version
IF (@last_synchronization_version BEGIN
-- Initialize from the base table
SELECT DepartmentID, Name, GroupName, ModifiedDate
FROM HumanResources.Department
END
ELSE
BEGIN
-- Obtain incremental changes by using the synchronization version obtained the last time the data was synchronized.
SELECT CT.SYS_CHANGE_OPERATION, CT.DepartmentID, Name, GroupName, ModifiedDate
FROM HumanResources.Department D
RIGHT OUTER JOIN
CHANGETABLE(CHANGES HumanResources.Department, @last_synchronization_version) AS CT
ON
D.DepartmentID = CT.DepartmentID
END
COMMIT TRAN
DECLARE @synchronization_version bigint SET @synchronization_version = CHANGE_TRACKING_CURRENT_VERSION() SELECT @synchronization_version IF (@last_synchronization_version -- Initialize from the base table SELECT DepartmentID, Name, GroupName, ModifiedDate FROM HumanResources.Department ELSE BEGIN -- Obtain incremental changes by using the synchronization version obtained the last time the data was synchronized. SELECT CT.SYS_CHANGE_OPERATION, CT.DepartmentID, Name, GroupName, ModifiedDate FROM HumanResources.Department D RIGHT OUTER JOIN CHANGETABLE(CHANGES HumanResources.Department, @last_synchronization_version) AS CT ON D.DepartmentID = CT.DepartmentID WHERE (CT.SYS_CHANGE_CREATION_VERSION <= @synchronization_version) END |
總結
本篇文章介紹了怎樣使用CHANGETABLE函式來獲取資料變更。展示了兩個演算法。你可以在你的.NET應用程式中使用Sync Services來執行這兩個演算法。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/16436858/viewspace-630024/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 解讀SQL Server 2008變更跟蹤SQLServer
- SQL Server 變更資料捕獲(CDC)SQLServer
- 跟蹤model中屬性(值)的變更
- 【SAP-BASIS】稅碼變更,及賬號角色變更跟蹤查詢
- 使用SQLServer 2008的CDC功能實現資料變更捕獲SQLServer
- sql server跟蹤資料庫SQLServer資料庫
- Oracle CDC 變更資料捕獲技術Oracle
- 【SQL】Oracle資料庫變更後sql效能對比SQLOracle資料庫
- 使用SQLServerAudit記錄資料庫變更SQLServer資料庫
- 【變更】線上庫的變更操作
- [Kogel.Subscribe.Mssql]SQL Server增量訂閱,資料庫變更監聽SQLServer資料庫
- SQL Server 2005安裝:版本變更檢查SQLServer
- Airbnb的變更資料捕獲系統,實現資料突變實時響應AI
- DataGear 變更部署資料庫為SQL Server填坑指南(含轉寫後的SQL server程式碼及SQL server配置檔案)資料庫SQLServer
- 變更資料捕獲CDC的八個實際案例 - Dunith
- 分析:Google讓我們變得更愚蠢嗎Go
- 資料變更通知的一種方案
- 騰訊大資料智慧營銷平臺讓“尋 TA ”變得更容易大資料
- [譯] 讓滾動在預設情況下變得更流暢
- 如何讓 Emacs 俄羅斯方塊變得更難Mac
- Smartour——讓網頁導覽變得更簡單網頁
- 讓動畫變得更簡單之FLIP技術動畫
- WPF自定義Panel:讓拖拽變得更簡單
- GPO策略變更
- LiquiBase 管理資料庫變更實踐UI資料庫
- 海量資料處理_表結構變更
- 變更資料捕獲CDC幾種應用場景 - RTInsights
- Oracle資料庫跟蹤SQLOracle資料庫SQL
- Minitab 2021:讓資料分析變得更簡單,更直觀 win版
- CUPS讓Linux列印變得更輕鬆(轉)Linux
- 資料變更白屏化利器-推送軌跡上線
- 通過修改資料字典,變更表的owner
- 一次資料變更的稽核過程
- cassandra版本變更列表
- SOM:研究發現導航 App 在讓城市交通變得更擁堵APP
- 使用Spring Cloud Data Flow + CDC Debezium源實時實現變更資料捕獲 - SpringIOSpringCloud
- 如何獲得SQL Server索引使用情況SQLServer索引
- SQL SERVER 跟蹤標記總結SQLServer