Temporal Tables, and how to deal with computed columns

Black and white Texas Instruments calculator

Long time readers will know I’m a big fan of Temporal Tables since their introduction in SQL Server 2016.

Thanks to my friend Erik Darling (blog | Twitter), I can share a tip when dealing with computed columns.

As I say in my Back to the future with Temporal Tables session, there are several limitations we should be aware of with this feature. One I had not previously considered is that computed columns are not permitted in a history table. This is a problem if we are converting an existing auditing process to make use of temporal tables, and we have computed columns in our table.

Computed columns are columns that are created from an expression based on one or more existing columns in a table. They are useful if we need to index a portion of a wider column, for example.

Erik has come up with a workaround which isn’t that intuitive, but makes sense given the limitation:

It was sort of counter intuitive to have to add the computed column to the regular table, then add a matching non-computed column to the history table, then re-enable system versioning.

Let’s say our current table contains computed columns, and we want to enable system versioning on this table. Whether we have a pre-existing history table or need to create one from scratch, the process is the same. Our history table must exist, with a schema that matches the current table. The main difference is that in place of our computed columns, the column data types in the history table must match the expression of each computed column in the current table.

Using the same example from previous posts, we have a table called [dbo].[Account]. In this case, we have a computed column called AccountCode which takes the first four digits of the AccountNumber column.

The history table’s structure must match the current table, like so:

Note that the computed column is a regular four-character wide string in the history table, CHAR(4).

We enable system versioning using the same method as before:

Hopefully this will help. Thanks Erik!

Leave your thoughts in the comments below.

Photo by Ray Reyes on Unsplash.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: