Temporal tables in SQL Server, explained

What a system-versioned temporal table is, how SQL Server keeps every old version of a row for you, and how to ask what the data looked like at any time.

Short answer: a system-versioned temporal table keeps the history of every row for you. Each row has two period columns, and when a row is updated or deleted, SQL Server moves the old version into a history table with the time it was valid. You then ask FOR SYSTEM_TIME AS OF '2026-03-01' and get the table exactly as it was at that moment. It works in SQL Server, Azure SQL Database and Azure SQL Managed Instance.

"What was this customer's credit limit when we approved the order?" is hard to answer from a table that only holds today. A temporal table answers it with one clause.

How does SQL Server keep the history?

  1. 1
    Current tableCredit limit 5,000, valid since 1 January
  2. 2
    UPDATEthe limit becomes 8,000 on 1 March
  3. 3
    History tablegets the 5,000 row, valid 1 January to 1 March
  4. 4
    Current tableholds 8,000, valid since 1 March
One update, two tables

Nobody writes to the history table; the database does it in the same transaction as the change.

How do you create one and ask it?

CREATE TABLE dbo.Customer (
    CustomerID  int PRIMARY KEY,
    Name        nvarchar(100) NOT NULL,
    CreditLimit decimal(18,2) NOT NULL,
    ValidFrom   datetime2 GENERATED ALWAYS AS ROW START NOT NULL,
    ValidTo     datetime2 GENERATED ALWAYS AS ROW END NOT NULL,
    PERIOD FOR SYSTEM_TIME (ValidFrom, ValidTo)
)
WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.CustomerHistory));
-- The table as it was on 15 February
SELECT CustomerID, CreditLimit
FROM dbo.Customer FOR SYSTEM_TIME AS OF '2026-02-15';

-- Every version of one customer
SELECT CreditLimit, ValidFrom, ValidTo
FROM dbo.Customer FOR SYSTEM_TIME ALL
WHERE CustomerID = 1042
ORDER BY ValidFrom;
ClauseRows
AS OF tthe versions valid at time t
FROM a TO bversions valid at any point between a and b, b not included
BETWEEN a AND blike FROM, with b included
CONTAINED IN (a, b)versions that started and ended inside the range
ALLevery version, current and history
FOR SYSTEM_TIME and what it returns

Temporal table or slowly changing dimension?

Both keep history, for different readers. A temporal table records when the database changed, with system times, and suits the operational side: audits, "as of" questions, undoing a bad update. A type 2 slowly changing dimension records when the business change took effect and gives each version a key a fact table can point at. A warehouse can build its type 2 dimension from a temporal source.

What goes wrong?

  • History that grows for ever. Set a retention period (HISTORY_RETENTION_PERIOD) on the table, or clean the history table on a schedule.
  • Times in UTC. The period columns are in UTC. Convert before comparing with local business dates.
  • Bulk updates. Updating every row writes every old row to history. Plan big corrections with that in mind.

Where can you check this?

Where do you start?

Temporal tables are one of the specialised tables on DP-800, and the open course Upserts into Azure SQL builds a target table that could keep its history this way.

Keep reading

More from the blog.

Every post

Next

Building this? Say hello.

A question about this post, a pattern you would build differently, or the next thing you want written out: Behnia reads every message.