SQL Server: Change Tracking 可以忽略更改为相同值的字段吗?
SQL Server: Change Tracking can ignore the field that is changed with the same value?
我正在跟踪 table 中的特定列更改(示例:跟踪 CustomerName,TestCustomer Table 中的 Address1),使用 SQL 服务器中的更改跟踪功能。到目前为止,下面的代码看起来一切都很好,但是当我使用相同的值更新字段时注意到,如下所示:
update TestCustomers set customerName=customerName where CustomerID= 2
所以查询仍然会告诉我这个 customerName 有变化。问题是:使用Change Tracking功能,是否有一个字段或方式可以告诉我这个字段的内容没有真正改变?并且仍然具有相同的内容?
下面是完整的 sql 脚本和结果:
create table dbo.TestCustomers
(
CustomerID int IDENTITY(1,1) NOT NULL,
customerName varchar(255) not null,
Address1 varchar(255) null,
CONSTRAINT [PK__TestCustomers__customerid] PRIMARY KEY CLUSTERED ( CustomerID ASC )
) ON [PRIMARY]
GO
insert into TestCustomers (CustomerName,Address1)
values ('custName1','custaddress1')
insert into TestCustomers (CustomerName,Address1)
values ('custName2','custaddress2')
insert into TestCustomers (CustomerName,Address1)
values ('custName3','custaddress3')
insert into TestCustomers (CustomerName,Address1)
values ('custName4','custaddress4')
ALTER TABLE TestCustomers
ENABLE CHANGE_TRACKING
WITH (TRACK_COLUMNS_UPDATED = ON)
select CustomerID,customerName,address1 from TestCustomers where CustomerID=2
update TestCustomers set customerName=customerName where CustomerID= 2
select CustomerID,customerName,address1 from TestCustomers where CustomerID=2
SELECT
ChVer = SYS_CHANGE_VERSION,
ChCrVer = SYS_CHANGE_CREATION_VERSION,
ChOp = SYS_CHANGE_OPERATION,
CustomerName_Changed = CHANGE_TRACKING_IS_COLUMN_IN_MASK
(COLUMNPROPERTY(OBJECT_ID('TestCustomers'), 'CustomerName', 'ColumnId')
,ChTbl.sys_change_columns),
Address1_Changed = CHANGE_TRACKING_IS_COLUMN_IN_MASK
(COLUMNPROPERTY(OBJECT_ID('TestCustomers'), 'Address1', 'ColumnId')
,ChTbl.sys_change_columns),
CustomerID
FROM CHANGETABLE(CHANGES TestCustomers, 1) AS ChTbl;
从图片中可以看出,虽然customerName改了同样的内容,但是结果显示是改的,我想看的只是内容真正改过的记录!请问有什么方法可以区分内容是否更改,主要是通过Change Track!
请注意:我们没有兴趣知道内容是什么,只需要知道该字段的内容是否更改!
谢谢
没有。确定 UPDATE 是否真的在更改数据将是额外的工作,Change Tracking is optimized for simplicity and low overhead. If you want this information, or need to see all the intermediate versions of rows, then Change Data Capture 可能是更好的选择。
我正在跟踪 table 中的特定列更改(示例:跟踪 CustomerName,TestCustomer Table 中的 Address1),使用 SQL 服务器中的更改跟踪功能。到目前为止,下面的代码看起来一切都很好,但是当我使用相同的值更新字段时注意到,如下所示:
update TestCustomers set customerName=customerName where CustomerID= 2
所以查询仍然会告诉我这个 customerName 有变化。问题是:使用Change Tracking功能,是否有一个字段或方式可以告诉我这个字段的内容没有真正改变?并且仍然具有相同的内容? 下面是完整的 sql 脚本和结果:
create table dbo.TestCustomers
(
CustomerID int IDENTITY(1,1) NOT NULL,
customerName varchar(255) not null,
Address1 varchar(255) null,
CONSTRAINT [PK__TestCustomers__customerid] PRIMARY KEY CLUSTERED ( CustomerID ASC )
) ON [PRIMARY]
GO
insert into TestCustomers (CustomerName,Address1)
values ('custName1','custaddress1')
insert into TestCustomers (CustomerName,Address1)
values ('custName2','custaddress2')
insert into TestCustomers (CustomerName,Address1)
values ('custName3','custaddress3')
insert into TestCustomers (CustomerName,Address1)
values ('custName4','custaddress4')
ALTER TABLE TestCustomers
ENABLE CHANGE_TRACKING
WITH (TRACK_COLUMNS_UPDATED = ON)
select CustomerID,customerName,address1 from TestCustomers where CustomerID=2
update TestCustomers set customerName=customerName where CustomerID= 2
select CustomerID,customerName,address1 from TestCustomers where CustomerID=2
SELECT
ChVer = SYS_CHANGE_VERSION,
ChCrVer = SYS_CHANGE_CREATION_VERSION,
ChOp = SYS_CHANGE_OPERATION,
CustomerName_Changed = CHANGE_TRACKING_IS_COLUMN_IN_MASK
(COLUMNPROPERTY(OBJECT_ID('TestCustomers'), 'CustomerName', 'ColumnId')
,ChTbl.sys_change_columns),
Address1_Changed = CHANGE_TRACKING_IS_COLUMN_IN_MASK
(COLUMNPROPERTY(OBJECT_ID('TestCustomers'), 'Address1', 'ColumnId')
,ChTbl.sys_change_columns),
CustomerID
FROM CHANGETABLE(CHANGES TestCustomers, 1) AS ChTbl;
从图片中可以看出,虽然customerName改了同样的内容,但是结果显示是改的,我想看的只是内容真正改过的记录!请问有什么方法可以区分内容是否更改,主要是通过Change Track! 请注意:我们没有兴趣知道内容是什么,只需要知道该字段的内容是否更改! 谢谢
没有。确定 UPDATE 是否真的在更改数据将是额外的工作,Change Tracking is optimized for simplicity and low overhead. If you want this information, or need to see all the intermediate versions of rows, then Change Data Capture 可能是更好的选择。