Lesson 2 of 4 Specialized Tables

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.

How system versioning works. The current table holds each row with its period columns, ValidFrom and ValidTo. 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. A query with FOR SYSTEM_TIME AS OF a point in time reads whichever version was valid then, from either table.
The engine maintains history; queries choose a point in time.
Put in order

An update to a temporal table

Exam skills 1.1.2

Put the events of an UPDATE on a system-versioned table in order.

Log in to try this activity Interactive activities are free with an account.

Querying history

The FOR SYSTEM_TIME clause queries the current and history tables together. Its subclauses answer different questions, as the following table explains.

SubclauseReturns
AS OF @tThe rows as they were at one moment
FROM @a TO @bEvery version active at any time between the two moments, excluding versions that started exactly at @b
BETWEEN @a AND @bLike FROM ... TO, but including versions that started at @b
CONTAINED IN (@a, @b)Versions that both started and ended within the range
ALLEvery version from both tables
SELECT ProductID, Name, ListPrice
FROM Sales.Product FOR SYSTEM_TIME AS OF '2026-06-30T23:59:59'
WHERE ProductID = 42;
Match the pairs

FOR SYSTEM_TIME subclauses

Exam skills 1.1.2

Match each question with the FOR SYSTEM_TIME subclause that answers it.

Log in to try this activity Interactive activities are free with an account.

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.

Worked example

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.

Check yourself

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.