-
将系统版本控制添加到表中。
-
收集数据以添加到系统版本表中,其中的列用于填充目标表中的system\u TIME列。请注意,SYSTEM_时间值将位于UTC时区,因此可能需要在“UTC”时区执行类似操作,以获得正确的日期。
-
-
为CRUD操作创建动态SQL。
-
重新启用系统版本控制。
-- System versioned table: dbo.ManagerList
-- Primary Key: ManagerName
-- History table: dbo.ManagerList_History
-- SYSTEM_TIME columns: _PeriodStart, _PeriodEnd
-- Table with Data to Import: #SourceData
DECLARE @Script varchar(max)
-- Disable system versioning
ALTER TABLE dbo.ManagerList SET (SYSTEM_VERSIONING = OFF);
ALTER TABLE dbo.ManagerList
DROP PERIOD FOR SYSTEM_TIME;
-- Prepare source data (in temporary table #SourceData)
UPDATE A
SET _PeriodEnd = B.PeriodEnd
FROM #SourceData as A
INNER JOIN
(
SELECT ManagerName,
_PeriodStart,
_PeriodEnd =
LEAD(_PeriodStart, 1, datetime2fromparts(9999,12,31,23,59,59,9999999,7)) OVER
(
PARTITION BY ManagerName
ORDER BY _PeriodStart
)
) as B
ON A.ManagerName = B.ManagerName
AND A._PeriodStart = B._PeriodStart
-- DELETE from System-Versioned table
DELETE A
FROM dbo.ManagerList as A
WHERE NOT EXISTS
(
SELECT 1
FROM #SourceData
WHERE ManagerName = A.ManagerName
)
-- UPDATE script for System-Versioned table
SET @Script =
'UPDATE A ' +
'SET FavoriteColor = B.FavoriteColor, ' +
' _PeriodStart = B._PeriodStart ' +
'FROM dbo.ManagerList as A ' +
' INNER JOIN #SourceData as B ' +
' ON A.ManagerName = B.ManagerName ' +
'WHERE B._PeriodEnd > datefromparts(9999,12,31) ' +
' ( ' +
' A.ManagerName != B.ManagerName ' +
' OR A._PeriodStart != B.PeriodStart ' +
' )'
EXEC (@Script)
-- UPDATE script for System-Versioned table
SET @Script =
'UPDATE A ' +
'SET FavoriteColor = B.FavoriteColor, ' +
' _PeriodStart = B._PeriodStart ' +
'FROM dbo.ManagerList ' +
' INNER JOIN #SourceData as B ' +
' ON A.ManagerName = B.ManagerName ' +
'WHERE B._PeriodEnd > datefromparts(9999,12,31) ' +
' AND (' +
' A.FavoriteColor != B.FavoriteColor ' +
' OR A._PeriodStart != B.PeriodStart ' +
' )'
EXEC (@Script)
-- INSERT script for System-Versioned table
SET @Script =
'INSERT dbo.ManagerList ' +
'( ' +
' ManagerName, ' +
' FavoriteColor, ' +
' _PeriodStart ' +
') ' +
'SELECT ManagerName, ' +
' FavoriteColor, ' +
' _PeriodStart ' +
'FROM #SourceData as A ' +
'WHERE _PeriodEnd > datefromparts(9999,12,31) ' +
' AND NOT EXISTS ' +
' ( ' +
' SELECT 1 ' +
' FROM dbo.ManagerList ' +
' WHERE ManagerName = A.ManagerName ' +
' )'
EXEC (@Script)
-- DELETE script for History table
SET @Script =
'DELETE A ' +
'FROM dbo.ManagerList_History as A ' +
'WHERE NOT EXISTS ' +
' ( ' +
' SELECT 1 ' +
' FROM #SourceData ' +
' WHERE ManagerName = A.ManagerName ' +
' AND _PeriodEnd < datefromparts(9999,12,31) ' +
' )'
EXEC (@Script)
-- UPDATE script for History table
SET @Script =
'UPDATE A ' +
'SET FavoriteColor = B.FavoriteColor ' +
'FROM dbo.ManagerList_History as A' +
' INNER JOIN #SourceData as B ' +
' ON A.ManagerName = B.ManagerName ' +
' AND A._PeriodStart = B._PeriodStart ' +
'WHERE B._PeriodEnd < datefromparts(9999,12,31) ' +
' AND (' +
' A.FavoriteColor != B.FavoriteColor' +
' )'
EXEC (@Script)
-- INSERT script for History table
SET @Script =
'INSERT dbo.ManagerList ' +
'( ' +
' ManagerName, ' +
' FavoriteColor, ' +
' _PeriodStart ' +
') ' +
'SELECT ManagerName, ' +
' FavoriteColor, ' +
' _PeriodStart ' +
'FROM #SourceData as A ' +
'WHERE _PeriodEnd < datefromparts(9999,12,31) ' +
' AND NOT EXISTS ' +
' ( ' +
' SELECT 1 ' +
' FROM dbo.ManagerList_History ' +
' WHERE ManagerName = A.ManagerName ' +
' AND _PeriodStart = A._PeriodStart ' +
' )'
EXEC (@Script)
-- Re-enabling system versioning
-- Note: Making this dynamic SQL solves compiler error
SET @Script =
'ALTER TABLE dbo.ManagerList ' +
'ADD PERIOD FOR SYSTEM_TIME (_PeriodStart, _PeriodEnd)';
EXEC (@Script)
ALTER TABLE dbo.ManagerList
SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.ManagerList_History));
在最初从现有表填充表之后,关于不修改SYSTEM\u时间日期或删除历史记录的所有限制在世界上都是有意义的。
希望这能帮助别人。