如何从 SQL 服务器更改跟踪中获取所有已更改表的列表
How to get a list of all changed tables from SQL Server Change Tracking
如何获取在给定版本之后有任何跟踪更改的所有表(已启用更改跟踪)的列表?
好吧,要获得所有启用了更改跟踪的 table 的列表,您可以执行类似
的查询
SELECT sys.tables.name FROM sys.change_tracking_tables
JOIN sys.tables ON tables.object_id = change_tracking_tables.object_id
然后您可以根据需要为版本添加 where 条件。我相信这回答了你的问题。
此外,如果您想查看有关更改的一些信息,您可以使用更改table 函数运行 查询,例如下面针对特定 table 的查询。
DECLARE @synchronization_version NVARCHAR(MAX),@last_synchronization_version NVARCHAR(MAX)
SET @synchronization_version = CHANGE_TRACKING_CURRENT_VERSION();
SELECT
CT.*
FROM
CHANGETABLE(CHANGES Sales.CreditCard, @last_synchronization_version) AS CT
更新
我更新了原始查询以执行查看并打印结果,您可以在执行查询之前查看 tables,因为每个人有超过 1000 tables您可能想删除一些评论。
SET NOCOUNT ON;
DECLARE @Views as TABLE (name nvarchar(200));
INSERT INTO @Views (name)
SELECT sys.tables.name FROM sys.change_tracking_tables
JOIN sys.tables ON tables.object_id = change_tracking_tables.object_id
DECLARE @viewName nvarchar(200) = (select top 1 name from @Views);
DECLARE @sql nvarchar(max) = '';
DECLARE @union NVARCHAR(20)
DECLARE @sql1 NVARCHAR(max)
SET @sql1 = 'DECLARE @synchronization_version NVARCHAR(MAX),@last_synchronization_versionNVARCHAR(MAX)
SET @synchronization_version = CHANGE_TRACKING_CURRENT_VERSION();'
PRINT(@sql1)
WHILE(Exists(select 1 from @Views)) BEGIN
SET @union = '';
SET @sql = '
SELECT
CT.*
FROM
CHANGETABLE(CHANGES ' + @ViewName +', @last_synchronization_version) AS CT'
IF (SELECT COUNT(name) FROM @Views) > 2
BEGIN
SET @union = ' UNION'
END
Print (@sql+@union);
DELETE FROM @Views where name = @viewName;
SET @ViewName = (select top 1 name from @Views);
END;
这将return列出自上一个跟踪版本以来更改过的所有表:
set nocount on;
-- We want to check for changes since the previous version
--declare @prevTrackingVersion int = INSERT_YOUR_PREV_VERSION_HERE
-- Comment out this line if you know the previous version
declare @prevTrackingVersion int = CHANGE_TRACKING_CURRENT_VERSION() - 1
-- Get a list of table with change tracking enabled
declare @trackedTables as table (name nvarchar(1000));
insert into @trackedTables (name)
select sys.tables.name from sys.change_tracking_tables
join sys.tables ON tables.object_id = change_tracking_tables.object_id
-- This will be the list of tables with changes
declare @changedTables as table (name nvarchar(1000));
-- For each table name in tracked tables
declare @tableName nvarchar(1000)
while exists(select top 1 * from @trackedTables)
begin
-- Set the current table name
set @tableName = (select top 1 name from @trackedTables order by name asc);
-- Determine if the table has changed since the previous version
declare @sql nvarchar(250)
declare @retVal int
set @sql = 'select @retVal = count(*) from changetable(changes ' + @tableName + ', ' + cast(@prevTrackingVersion as varchar) + ') as changedTable'
exec sp_executesql @sql, N'@retVal int output', @retVal output
if @retval > 0
begin
insert into @changedTables (name) select @tableName
end
-- Delete the current table name
delete from @trackedTables where name = @tableName;
end
select * from @changedTables;
如何获取在给定版本之后有任何跟踪更改的所有表(已启用更改跟踪)的列表?
好吧,要获得所有启用了更改跟踪的 table 的列表,您可以执行类似
的查询SELECT sys.tables.name FROM sys.change_tracking_tables
JOIN sys.tables ON tables.object_id = change_tracking_tables.object_id
然后您可以根据需要为版本添加 where 条件。我相信这回答了你的问题。
此外,如果您想查看有关更改的一些信息,您可以使用更改table 函数运行 查询,例如下面针对特定 table 的查询。
DECLARE @synchronization_version NVARCHAR(MAX),@last_synchronization_version NVARCHAR(MAX)
SET @synchronization_version = CHANGE_TRACKING_CURRENT_VERSION();
SELECT
CT.*
FROM
CHANGETABLE(CHANGES Sales.CreditCard, @last_synchronization_version) AS CT
更新
我更新了原始查询以执行查看并打印结果,您可以在执行查询之前查看 tables,因为每个人有超过 1000 tables您可能想删除一些评论。
SET NOCOUNT ON;
DECLARE @Views as TABLE (name nvarchar(200));
INSERT INTO @Views (name)
SELECT sys.tables.name FROM sys.change_tracking_tables
JOIN sys.tables ON tables.object_id = change_tracking_tables.object_id
DECLARE @viewName nvarchar(200) = (select top 1 name from @Views);
DECLARE @sql nvarchar(max) = '';
DECLARE @union NVARCHAR(20)
DECLARE @sql1 NVARCHAR(max)
SET @sql1 = 'DECLARE @synchronization_version NVARCHAR(MAX),@last_synchronization_versionNVARCHAR(MAX)
SET @synchronization_version = CHANGE_TRACKING_CURRENT_VERSION();'
PRINT(@sql1)
WHILE(Exists(select 1 from @Views)) BEGIN
SET @union = '';
SET @sql = '
SELECT
CT.*
FROM
CHANGETABLE(CHANGES ' + @ViewName +', @last_synchronization_version) AS CT'
IF (SELECT COUNT(name) FROM @Views) > 2
BEGIN
SET @union = ' UNION'
END
Print (@sql+@union);
DELETE FROM @Views where name = @viewName;
SET @ViewName = (select top 1 name from @Views);
END;
这将return列出自上一个跟踪版本以来更改过的所有表:
set nocount on;
-- We want to check for changes since the previous version
--declare @prevTrackingVersion int = INSERT_YOUR_PREV_VERSION_HERE
-- Comment out this line if you know the previous version
declare @prevTrackingVersion int = CHANGE_TRACKING_CURRENT_VERSION() - 1
-- Get a list of table with change tracking enabled
declare @trackedTables as table (name nvarchar(1000));
insert into @trackedTables (name)
select sys.tables.name from sys.change_tracking_tables
join sys.tables ON tables.object_id = change_tracking_tables.object_id
-- This will be the list of tables with changes
declare @changedTables as table (name nvarchar(1000));
-- For each table name in tracked tables
declare @tableName nvarchar(1000)
while exists(select top 1 * from @trackedTables)
begin
-- Set the current table name
set @tableName = (select top 1 name from @trackedTables order by name asc);
-- Determine if the table has changed since the previous version
declare @sql nvarchar(250)
declare @retVal int
set @sql = 'select @retVal = count(*) from changetable(changes ' + @tableName + ', ' + cast(@prevTrackingVersion as varchar) + ') as changedTable'
exec sp_executesql @sql, N'@retVal int output', @retVal output
if @retval > 0
begin
insert into @changedTables (name) select @tableName
end
-- Delete the current table name
delete from @trackedTables where name = @tableName;
end
select * from @changedTables;