How to Stopping System-Versioning on a System-Versioned Temporal Table in SQL Server 2016?
Asked Answered
S

3

10

I have a table that has system versioning (temporal table), but I can not see a design environment visually. I do it because I can see the SYSTEM_VERSIONING clause has been used. I would like to have temporarily Stop and then enable it. Who can advise me?

Sandeesandeep answered 4/5, 2017 at 10:28 Comment(1)
Does this answer your question? Cannot delete rows from a temporal history tableClovah
S
24

My problem was solved when i using following query:

-- SET SYSTEM_VERSIONING TO OFF
ALTER TABLE [dbo].[MyTable]
SET (SYSTEM_VERSIONING = OFF)
GO

** Do what ever you want to **

-- SET SYSTEM_VERSIONING TO ON
ALTER TABLE [dbo].[MyTable]
SET 
    (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[MyTable_Archive] , DATA_CONSISTENCY_CHECK = ON ))
GO
Sandeesandeep answered 4/5, 2017 at 10:56 Comment(4)
Hi @Aiyob , Do we need to use the statement - DATA_CONSISTENCY_CHECK = ON here and is it require,Dagall
The option DATA_CONSISTENCY_CHECK = ON is not required. It is the default and performs various checks before turning the historization back on. Here is the corresponding SQL Docs documentation by Microsoft learn.microsoft.com/en-us/sql/relational-databases/tables/….Misplace
You should be careful with this answer as it will most likely destroy your continous history. When you set the system_versioning off you should also drop your period for system_time. You should also be aware of that DATA_CONSISTENCY_CHECK = ON "only" checks if ValidTo >= ValidFrom and NOT checks for gaps in the history...Partiality
You can use this gist to play around and check yourself.Partiality
S
9
ALTER TABLE dbo.MyTable SET (SYSTEM_VERSIONING = OFF);   

** Do what ever you want to **

ALTER TABLE dbo.MyTable SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[MyTable_Archive]));   

See this Microsoft article for more info

UPDATED 22/05/2023 : Added HISTORY_TABLE argument to keep the answer relevant. When the answer was originally written, this was a new feature and generally not required.

Sandisandidge answered 4/5, 2017 at 10:50 Comment(2)
This creates temporary table and does not use previous history table!!!Margotmargrave
Adding to @Margotmargrave comment, this answer does not specity the "HISTORY_TABLE" argument, and thus another table is created. From Microsoft docs "When turning system versioning back on, don't forget to specify the HISTORY_TABLE argument. Failing to do so results in a new history table being created and associated with the current table. The original history table can still exist as a normal table but won't be associated with the current table." learn.microsoft.com/en-us/sql/relational-databases/tables/…Umber
F
2

It seems that after doing SET( SYSTEM_VERSIONING = OFF ), the PERIOD FOR option on the table is also reset.

So this works for me:

ALTER TABLE dbo.MyTable
    SET (
        SYSTEM_VERSIONING = OFF
    )

GO

/* Do stuff here. */

ALTER TABLE dbo.MyTable
    ADD PERIOD FOR SYSTEM_TIME ( ValidFrom, ValidTo );

ALTER TABLE dbo.MyTable
    SET (
        SYSTEM_VERSIONING = ON (
            HISTORY_TABLE          = dbo.MyTable_History,
            DATA_CONSISTENCY_CHECK = ON
        ));

I assume it's possible to combine the last two commands into a single ALTER TABLE statement but I haven't found the magic incantation yet.

Floss answered 8/5, 2022 at 7:23 Comment(1)
You can only add the period for system_time if you have dropped it beforehand. So you should add an ALTER TABLE [dbo].[MyTable] DROP PERIOD FOR SYSTEM_TIME before /* Do your stuff here. */Partiality

© 2022 - 2024 — McMap. All rights reserved.