SQL Server · Reliability & Concurrency

Temporal Tables

A temporal table remembers every previous version of each row. You can ask “what was the price on 1 October?” with plain SQL.

Advanced 5 min Lesson 39 of 44Examples run on SQL Server 2022 and Azure SQL Database.
0/44 in SQL Server
On this page · 5 min · Advanced

Learning objectives#

  • Create a system-versioned temporal table
  • Query data as of a past moment
  • See a row’s full history
  • Manage history size

Explanation#

Add two datetime2 columns GENERATED ALWAYS AS ROW START / ROW END, a PERIOD FOR SYSTEM_TIME, and turn on SYSTEM_VERSIONING with a history table. From then on, every UPDATE and DELETE copies the old version into the history table automatically.

Query the past with FOR SYSTEM_TIME AS OF '2026-10-01', or see every version with FOR SYSTEM_TIME ALL. Your application code for inserts and updates doesn’t change.

History grows forever unless you set HISTORY_RETENTION_PERIOD (for example 2 YEARS) — available in SQL Server 2017+ and Azure SQL.

SSMS shows a temporal table with “(System-Versioned)” and the history table nested beneath it in Object Explorer.

Step-by-step screens#

Drawings of SQL Server 2022 Setup and SQL Server Management Studio (SSMS 20) with the ShopDB sample database. The red numbers match the numbered actions under each picture; names on your computer will differ.

Loading pictures…

Code example: Price history without triggers#

SQLPrice history without triggers Illustrative
ALTER TABLE dbo.Products ADD
    ValidFrom DATETIME2 GENERATED ALWAYS AS ROW START HIDDEN NOT NULL DEFAULT SYSUTCDATETIME(),
    ValidTo   DATETIME2 GENERATED ALWAYS AS ROW END   HIDDEN NOT NULL DEFAULT CAST('9999-12-31 23:59:59.9999999' AS DATETIME2),
    PERIOD FOR SYSTEM_TIME (ValidFrom, ValidTo);


ALTER TABLE dbo.Products SET (SYSTEM_VERSIONING = ON (
    HISTORY_TABLE = dbo.ProductsHistory, HISTORY_RETENTION_PERIOD = 2 YEARS));


UPDATE dbo.Products SET Price = 1399 WHERE ProductId = 3;


-- What did it cost on 1 October?
SELECT Name, Price FROM dbo.Products FOR SYSTEM_TIME AS OF '2026-10-01' WHERE ProductId = 3;


-- Every version
SELECT Price, ValidFrom, ValidTo FROM dbo.Products FOR SYSTEM_TIME ALL WHERE ProductId = 3 ORDER BY ValidFrom;
Expected output
Name               Price
-----------------  -------
Keyboard Wireless  1499.00

Price    ValidFrom                    ValidTo
-------  ---------------------------  ---------------------------
1499.00  2026-08-14 09:12:40.1234567  2026-11-26 06:05:11.7654321
1399.00  2026-11-26 06:05:11.7654321  9999-12-31 23:59:59.9999999

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. Two hidden period columns are added (existing queries with SELECT * don’t see them).
  2. SYSTEM_VERSIONING creates ProductsHistory and keeps 2 years.
  3. The UPDATE moves the old row to history automatically.
  4. AS OF returns the row as it was on that date.
  5. ALL shows the timeline; times are in UTC.

Real-world use case#

Customer support can answer “I was charged ₹1,499 last month” in seconds, and auditors get a complete change history without custom triggers.

Common mistakes#

Local times in AS OF

Period columns are UTC. Convert local times with AT TIME ZONE before querying.

Unlimited history on busy tables

Set a retention period or the history table grows without limit.

Best practices#

  • Use HIDDEN period columns
  • Set HISTORY_RETENTION_PERIOD
  • Query in UTC
  • Index the history table for your queries

Remember this#

ROW START / ROW END + PERIOD FOR SYSTEM_TIME.

SYSTEM_VERSIONING = ON.

FOR SYSTEM_TIME AS OF / ALL.

Practice exercise#

Your task: Return all products as they were at 2026-11-01 00:00 UTC.

Show solution
SQL Illustrative
SELECT * FROM dbo.Products FOR SYSTEM_TIME AS OF '2026-11-01T00:00:00';

Mini quiz#

Temporal Tables quizMultiple choice Question 1 of 1

Where do old row versions go?

Score so far: 0/0

Want more? Practise all SQL Server MCQs · SQL Server interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.