【发布时间】:2019-04-04 17:56:34
【问题描述】:
我正在寻找删除系统版本化时态表的过程,最好不使用动态 SQL。我查看了 Microsoft 文档并弄清楚了如何获取自动生成的历史表名称,但我对游标了解甚少,对动态 SQL 更是知之甚少。
You can't just drop a temporal table。您必须首先禁用版本控制,这将导致历史表变成普通表。然后你可以同时删除临时表及其对应的历史表。
ALTER TABLE [dbo].[TemporalTest] SET ( SYSTEM_VERSIONING = OFF )
GO
DROP TABLE [dbo].[TemporalTest]
GO
DROP TABLE [dbo].[TemporalTestHistory]
GO
我正在使用带有自动生成历史表的临时表,所以我不知道它们的名称。但是,Microsoft docs 提供了有关如何列出历史表的信息,因此我有办法获取这些名称。
select schema_name(t.schema_id) as temporal_table_schema,
t.name as temporal_table_name,
schema_name(h.schema_id) as history_table_schema,
h.name as history_table_name,
case when t.history_retention_period = -1
then 'INFINITE'
else cast(t.history_retention_period as varchar) + ' ' +
t.history_retention_period_unit_desc + 'S'
end as retention_period
from sys.tables t
left outer join sys.tables h
on t.history_table_id = h.object_id
where t.temporal_type = 2
order by temporal_table_schema, temporal_table_name
我希望我可以使用带有 DROP 语句的子查询,例如DROP TABLE (SELECT '#t')。这会引发语法错误。
我正在寻找一个带有两个参数的存储过程:要删除的表的名称以及如果表中有任何数据(例如,必须 ROWCOUNT=0)是否应该进行删除。任何人都可以提供帮助,或推荐动态 SQL 上的游标,或推荐另一种技术吗?谢谢!
【问题讨论】:
标签: sql-server tsql