Temporal Tables Flashcards
What is the syntax for creating a Temporal Table?
CREATE TABLE dbo.TableName
(
PrimKeyField int PRIMARY KEY
, [ValidFrom] datetime2 GENERATED ALWAYS AS ROW START
, [ValidTo] datetime2 GENERATED ALWAYS AS ROW END
, PERIOD FOR SYSTEM_TIME (ValidFrom, ValidTo)
)
WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.TableHistory));
What are the necessary elements for a Temporal Table?
Primary Key
valid from column (datetime2) with GENERATED ALWAYS AS ROW START
valid to column (datetime2) with GENERATED ALWAYS AS ROW END
PERIOD FOR SYSTEM_TIME([VALIDFROMCOLUMN],[VALIDTOCOLUMN])
WITH (SYSTEM_VERSIONING = ON(HISTORY_TABLE=[HISTORYTABLE]));
How do you drop a Temporal Table?
ALTER TABLE [TABLENAME] SET (SYSTEM_VERSIONING=OFF)
GO
DROP TABLE [TABLENAME]
GO
DROP TABLE [TABLEHISTORYNAME]
GO
What is the query syntax for selecting data from a temporal table?
FOR SYSTEM TIME [AS OF ] [FROM TO ] [BETWEEN AND ] [CONTAINED IN ( , )] [ALL]