patternsqlMinor
SYS_CHANGE_OPERATION not showing correct value when row is Updated in CHANGE TRACKING
Viewed 0 times
trackingshowingvaluesys_change_operationcorrectupdatedwhenrownotchange
Problem
When a row is updated the SYS_CHANGE_OPERATION column is still showing I (Insert) and not U (Update) when querying CHANGETABLE .
Here's what I'm doing.
compatibility_level = 160
@@Version = Microsoft SQL Server 2022 (RTM) - 16.0.1000.6 (X64) Oct 8 2022 05:58:25 Copyright (C) 2022 Microsoft Corporation Developer Edition (64-bit) on Windows 10 Pro
10.0 (Build 19045: ) (Hypervisor)
Now if I update the row
As you can see
What am I doing wrong here?
Here's what I'm doing.
USE master
GO
CREATE DATABASE TestCT ;
GO
SELECT compatibility_level , @@VERSION
FROM sys.databases WHERE name = 'TestCT';compatibility_level = 160
@@Version = Microsoft SQL Server 2022 (RTM) - 16.0.1000.6 (X64) Oct 8 2022 05:58:25 Copyright (C) 2022 Microsoft Corporation Developer Edition (64-bit) on Windows 10 Pro
10.0 (Build 19045: ) (Hypervisor)
ALTER DATABASE TestCT
SET CHANGE_TRACKING = ON
(CHANGE_RETENTION = 2 DAYS, AUTO_CLEANUP = ON) ;
USE TestCT;
GO
DROP TABLE IF EXISTS dbo.TestCT
CREATE TABLE dbo.TestCT(
Id INT IDENTITY (1,1) CONSTRAINT PK__TestCT PRIMARY KEY ,
Col1 VARCHAR(100)
) ;
GO
ALTER TABLE dbo.TestCT
ENABLE CHANGE_TRACKING
WITH (TRACK_COLUMNS_UPDATED = ON) ;
GO
INSERT INTO dbo.TestCT (Col1)
VALUES ('INSERT1') ;
GO
SELECT *
FROM CHANGETABLE (CHANGES dbo.TestCT,0) as CT
LEFT JOIN dbo.TestCT EM ON CT.Id = EM.Id
ORDER BY SYS_CHANGE_VERSION ;
GOSYS_CHANGE_OPERATION here is I (Insert) as expected.Now if I update the row
UPDATE T
SET Col1 = 'UPDATE'
FROM dbo.TestCT T
WHERE Col1 = 'INSERT1';
GOSYS_CHANGE_OPERATION here is still I (Insert) and not U (Update).As you can see
SYS_CHANGE_VERSION has incremented by 1 as expected.What am I doing wrong here?
Solution
You aren't doing anything wrong, except ;-), in this case, the result should be an INSERT.
This is because in your query, you are requesting everything with the start of 0.
This means, you will get the definition from 0. Since 0, there has been an INSERT and an update, however when you will see the data, it will have the latest result. Therefor the UPDATE is not relevant. Since 0 it has been an INSERT for the reader.
When you change the 0 in a 1, SQL will see the changes since 1, and then it will be an UPDATE.
Microsoft has some good documentation about this:
https://learn.microsoft.com/en-us/sql/relational-databases/track-changes/work-with-change-tracking-sql-server?view=sql-server-ver16
This is because in your query, you are requesting everything with the start of 0.
This means, you will get the definition from 0. Since 0, there has been an INSERT and an update, however when you will see the data, it will have the latest result. Therefor the UPDATE is not relevant. Since 0 it has been an INSERT for the reader.
When you change the 0 in a 1, SQL will see the changes since 1, and then it will be an UPDATE.
Microsoft has some good documentation about this:
https://learn.microsoft.com/en-us/sql/relational-databases/track-changes/work-with-change-tracking-sql-server?view=sql-server-ver16
Context
StackExchange Database Administrators Q#320532, answer score: 4
Revisions (0)
No revisions yet.