如何判断sql表是否是Temporal?

时间:2015-06-29 20:28:27

标签: sql sql-server sql-server-2016

SQL Server 2016支持Temporal Tables我想知道是否有办法确定表当前是否属于临时表?像

这样的东西

select * from sys.objects where object_id('dbo.MyTable', 'u') = parent_object_id and type_desc = "SYSTEM_VERSIONED"

3 个答案:

答案 0 :(得分:11)

SELECT temporal_type
FROM   sys.tables
WHERE  object_id = OBJECT_ID('dbo.MyTable', 'u') 
     

0 = NON_TEMPORAL_TABLE

     

1 = HISTORY_TABLE

     

2 = SYSTEM_VERSIONED_TEMPORAL_TABLE

Documentation

答案 1 :(得分:3)

将时态表与历史表一起列出的另一种方法在本SQL教程中给出List Temporal and History Tables in a SQL Server Database

select
 t.object_id,
 t.name,
 t.temporal_type,
 t.temporal_type_desc,
 h.object_id,
 h.name,
 h.temporal_type,
 h.temporal_type_desc
from sys.tables t
inner join sys.tables h on t.history_table_id = h.object_id 

答案 2 :(得分:1)

Here is a simple answer to the original basic question:

SELECT *
FROM sys.tables
WHERE name = 'MyTable'
    AND schema_id = SCHEMA_ID('dbo')
    AND temporal_type_desc = 'SYSTEM_VERSIONED_TEMPORAL_TABLE'

And here is a similar query looking for the actual system managed history table:

SELECT h.* FROM sys.tables p 
INNER JOIN sys.tables h
    ON p.history_table_id = h.object_id
WHERE p.name = 'MyTable'
    AND p.schema_id = SCHEMA_ID('dbo')
    AND p.temporal_type_desc = 'SYSTEM_VERSIONED_TEMPORAL_TABLE';