Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if sql table is Temporal?

With SQL Server 2016 supporting Temporal Tables I wonder if there is a way to determine if a table is currently temporal? Something like

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

like image 715
Mathew Ruberg Avatar asked Jun 29 '15 20:06

Mathew Ruberg


People also ask

How do I find temporal tables in SQL Server?

tables there are specific columns (temporal_type and temporal_type_desc and history_table_id) to identify whether a table is system-versioned temporal table or history table. Using these columns, you can easily get the list of all the temporal current tables and the history tables and get the list of such tables.

What is a temporal table in SQL?

Temporal tables (also known as system-versioned temporal tables) are a database feature that brings built-in support for providing information about data stored in the table at any point in time, rather than only the data that is correct at the current moment in time.

What two classes are required for temporal table?

A temporal table must have a primary key defined in order to correlate records between the current table and the history table, and the history table can't have a primary key defined.


1 Answers

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

like image 129
Mathew Ruberg Avatar answered Oct 05 '22 17:10

Mathew Ruberg