我们正在测试的Server上有一个T迁移脚本.我们需要从时态表中删除某些数据(但不是全部),但是随机地,脚本现在正在失败。要删除历史记录,我们需要关闭历史记录,这样就不会生成新的历史记录,并且可以空出历史记录的一部分。
由于前面的失败,它会关闭一个表历史记录,并且不会在删除结束时打开它。如果历史记录已经关闭,那么新的运行将失败。
谢谢!
发布于 2022-11-21 01:28:10
您可以通过sys.tables查找表是否有关联的历史表。
select
t.name,
t.history_table_id
from sys.tables t
where t.name in (
'Table1',
'Table2'
);在null中使用history_table_id的表没有启用历史记录。
如果不想提供列表,可以通过sys.columns检查PERIOD列:
select
t.name,
t.history_table_id
from sys.tables t
where exists (select 1
from sys.columns c
where c.object_id = t.object_id
and c.generated_always_type > 0
);下面的脚本将使用默认名称自动启用历史表。
DECLARE @sql nvarchar(max);
select @sql =
STRING_AGG('
ALTER TABLE ' + QUOTENAME(s.name) + '.' + QUOTENAME(t.name) + '
SET (SYSTEM_VERSIONING = ON);
', '')
from sys.tables t
join sys.schemas s on s.schema_id = t.schema_id
where exists (select 1
from sys.columns c
where c.object_id = t.object_id
and c.generated_always_type > 0
)
and t.history_table_id is null;
PRINT @sql; -- your friend
EXEC sp_executesql @sql;https://dba.stackexchange.com/questions/319881
复制相似问题