System-Versioned Temporal Tables

Every once in a while, I like to take a moment and learn something new about the latest SQL Server gizmos and gadgets. Today I came across system-versioned temporal tables and it piqued my interest, so I figured I’d investigate and share my findings with you.

How many of you need to track data changes over time? I’ve needed this many times for things like auditing, investigating data changes, data fixes, and trend analysis of values over time. Having to do this in the past has been a very daunting task at times and sometimes nearly impossible. This is where system-versioned temporal tables will really help out. They have given us a new way to do just that with a new user table type. It keeps a full history of those data changes and gives us a way to query in order to do point in time analysis. What I really like about this is that you can’t INSERT or UPDATE data into the date time columns as they are automatically generated with the insert, which is great for auditing.

The syntax for Temporal Table Creation

Note we now have 2 required datetime2 fields that will be populated with our temporal history data for each row.

  1. CREATE TABLE dbo.[Department](  
  2.     [DepartmentID][smallintNOT NULL PRIMARY KEY CLUSTERED, [Namevarchar(50) NOT NULL, [GroupName] varchar(50) NOT NULL, [BeginDate] datetime2(2) GENERATED ALWAYS AS ROW START, [EndDate] datetime2(2) GENERATED ALWAYS AS ROW END, PERIOD FOR SYSTEM_TIME([BeginDate], [EndDate])) WITH(SYSTEM_VERSIONING = ON(HISTORY_TABLE = dbo.DepartmentHistory)  
  3.     ); 

Let's insert some records using INSERTS and see how the data looks.

  1. INSERT dbo.Department(DepartmentID, Name, GroupName)  
  2. SELECT 4, 'John''DBA'  
  3. UNION  
  4. SELECT 5, 'Joe''DEV'  
  5. UNION  
  6. SELECT 6, 'Jessica''DEV'  
  7. UNION  
  8. SELECT 7, 'Tim''DBA'  
  9. UNION  
  10. SELECT 8, 'Sam''DBA'  
  11. UPDATE dbo.Department  
  12. SET GroupName = 'DB Operations'  
  13. WHERE DepartmentID % 2 = 0  

This great illustration from Microsoft shows just how the history is tracked. For each INSERTED record, the SysStartTime will be populated in our BeginDate field. Each additional UPDATE/DELETE/MERGE our current record is copied to a history table and EndDate is updated with SysEndTime.

SQL Server

How do you query it?

It uses a new clause FOR SYSTEM_TIME that you can now query using it combined with AS OF, FROM TO, BETWEEN AND, CONTAINED IN, ALL

  1. SELECT * FROM DEPARTMENT  
  2. FOR SYSTEM_TIME  
  3. BETWEEN '2017-08-01'  
  4. AND '2017-08-31'  
  5. ORDER BY BeginDate;  

Results

Note the current record has an end date of  9999-12-31 23:59:59 because it's the current state of the record. It hasn't been modified yet so it gets the default future DateTime.

SQL Server

If you are lucky enough to be using SQL Server 2017 I highly recommend playing around with this new gadget, it may be of significant use to you.


Similar Articles
Denny Cherry and Associates
Expert Consultants From HA to DR to up-time to SQL virtualization to scalability.