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.
On this page · 5 min · Advanced
On this page
- Learning objectives
- Explanation
- Step-by-step screens
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Advanced
- Updated
- Nov 26, 2026
Related tutorials
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#
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;
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#
- Two hidden period columns are added (existing queries with SELECT * don’t see them).
- SYSTEM_VERSIONING creates ProductsHistory and keeps 2 years.
- The UPDATE moves the old row to history automatically.
- AS OF returns the row as it was on that date.
- 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#
Period columns are UTC. Convert local times with AT TIME ZONE before querying.
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
SELECT * FROM dbo.Products FOR SYSTEM_TIME AS OF '2026-11-01T00:00:00';
Mini quiz#
Where do old row versions go?
Want more? Practise all SQL Server MCQs · SQL Server interview questions
My notes#
Saved in this browser.