System-Versioned Temporal Tables
This lesson explains how temporal tables keep row history automatically and how FOR SYSTEM_TIME queries return data as it was.
Learning outcomes
After completing this lesson, you will be able to:
- Describe the period columns and history table of a temporal table.
- Choose the FOR SYSTEM_TIME subclause for a point-in-time or range question.
- Explain history retention and the limits of temporal history as an integrity proof.
How system versioning works
A temporal table has two datetime2 period columns, declared GENERATED ALWAYS AS ROW START and ROW END, a PERIOD FOR SYSTEM_TIME clause, and SYSTEM_VERSIONING = ON with a history table. When a row is updated or deleted, the engine copies the previous version to the history table with its validity period, inside the same transaction. The period columns can be HIDDEN so that SELECT * and existing INSERT statements are unaffected.
CREATE TABLE Sales.Product
(
ProductID int NOT NULL PRIMARY KEY CLUSTERED,
Name nvarchar(100) NOT NULL,
ListPrice decimal(10,2) NOT NULL,
ValidFrom datetime2 GENERATED ALWAYS AS ROW START HIDDEN NOT NULL,
ValidTo datetime2 GENERATED ALWAYS AS ROW END HIDDEN NOT NULL,
PERIOD FOR SYSTEM_TIME (ValidFrom, ValidTo)
)
WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = Sales.ProductHistory,
HISTORY_RETENTION_PERIOD = 2 YEARS));The following diagram shows the current and history tables after two updates.

An update to a temporal table
Querying history
The FOR SYSTEM_TIME clause queries the current and history tables together. Its subclauses answer different questions, as the following table explains.
| Subclause | Returns |
|---|---|
| AS OF @t | The rows as they were at one moment |
| FROM @a TO @b | Every version active at any time between the two moments, excluding versions that started exactly at @b |
| BETWEEN @a AND @b | Like FROM ... TO, but including versions that started at @b |
| CONTAINED IN (@a, @b) | Versions that both started and ended within the range |
| ALL | Every version from both tables |
SELECT ProductID, Name, ListPrice
FROM Sales.Product FOR SYSTEM_TIME AS OF '2026-06-30T23:59:59'
WHERE ProductID = 42;FOR SYSTEM_TIME subclauses
Maintaining temporal tables
Period values are in UTC. The history table can have its own indexes, such as a clustered columnstore index for large histories, and HISTORY_RETENTION_PERIOD removes old history automatically. Schema changes such as adding a column are applied to both tables by ALTER TABLE on the current table. To change history directly, for example to correct data, system versioning must be turned off and back on, which breaks the guarantee of an untouched history; ledger tables provide that guarantee instead.
Price as of an order date
Customer service must show the list price that applied on the day each historical order was placed, and prices change several times a year. The Product table is not temporal today. What should the developer implement?
Convert Sales.Product to a system-versioned temporal table and query it with FOR SYSTEM_TIME AS OF o.OrderDate, for example through an APPLY for each order. The engine records each price change automatically, so no trigger or application change is needed. A trigger-based audit table would work but requires custom code, and a ledger table proves integrity but is not needed when the requirement is point-in-time reporting. History begins only when versioning is enabled, so earlier prices must be loaded separately if they are needed.
In which time zone are a temporal table's period columns recorded?
Show answer
UTC, so AS OF values must be expressed in UTC.
Exam Tips
Remember for the exam
- Data as it was at a moment: FOR SYSTEM_TIME AS OF.
- HIDDEN period columns keep SELECT * and INSERT statements unchanged.
- HISTORY_RETENTION_PERIOD cleans up old history automatically.
- Period columns are in UTC.