Showing posts with label Temporal Table - Truncate. Show all posts
Showing posts with label Temporal Table - Truncate. Show all posts

Tuesday, May 14, 2019

How to truncate system version table ( temporal table)

This blog explains how to truncate the system version enabled table (i.e. temporal table) and Stopping System-Versioning on a System-Versioned Temporal Table
If you want to truncate the temporal table, you will get below error message

truncate table dbo.Project

Msg 13545, Level 16, State 1, Line 21
Truncate failed on table 'dbo.Project' because it is not a supported operation on system-versioned tables.

You want to perform any specific DML operation on temporal table and you want to temporary disable system–versioned table or you don’t need any version table anymore.

You can simple use SYSTEM_VERSIONING = OFF option and history table will stop capturing the updates

-- Disable the system version table
ALTER TABLE dbo.Project SET (SYSTEM_VERSIONING = OFF);

--Truncate data
Truncate Table dbo.Project

--Enable the system version table

ALTER TABLE dbo.Project SET ( SYSTEM_VERSIONING = ON );