In many cases, there are legal requirements to audit all updates to financial records. In other cases, you will want to be able to track and undo deletes to your database. With the use of Temporal tables, this becomes much easier to manage.
Temporal tables were introduced in SQL Server 2016 and enhanced with increased features in SQL Server 2017. They offer the ability to record all the entity changes to a history table allowing the querying of the entity at a point in time. ✅ Pros:
It’s actually quite simple, here is a code snippet converting a table from the Northwind schema into a temporal table.
CREATE TABLE dbo.Shippers([ShipperID] int IDENTITY(1, 1) NOT NULL,[CompanyName] nvarchar(40) NOT NULL,[Phone] nvarchar(24) NULL,[SysStartTime] datetime2 GENERATED ALWAYS AS ROW START, [SysEndTime] datetime2 GENERATED ALWAYS AS ROW END, PERIOD FOR SYSTEM_TIME (SysStartTime, SysEndTime), CONSTRAINT PK_Shippers PRIMARY KEY CLUSTERED([ShipperID]))WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.ShippersHistory));
Figure: Shippers table from the Northwind schema converted to a temporal table.
Figure: New temporal table shown in SQL Management Studio.
-- Update the tables history data retentionALTER TABLE dbo.ShippersSET (SYSTEM_VERSIONING = ON (HISTORY_RETENTION_PERIOD = 7 YEARS));
Figure: Code snippet for updating data retention.
Some alternative solutions are:
This means that you can devote your development time to areas other than auditing. Also, unlike other utilities which use triggers (such as ApexSQL Audit), there is no performance overhead because it relies upon log files already created by SQL Server. If required, you can export the log information to SQL Server, so you can perform advanced queries on it. It even allows you to recover previously deleted tables.