I have a temporal/system versioned table in sql server and I would like to take a copy/replicate the table along with entire history from the original temporal table.
Is it possible to replicate/copy temporal table in sql server and if so, what would be the steps.
It's just a one time copy of the current table available.
Thank you
3条答案
按热度按时间2exbekwf1#
I don't really see what's the issue here to just make a copy like you would with any other table. Just do it twice, once for main table and once for history table.
If you have table Department with temporal table DepartmentHistory, you can simply SELECT INTO copies for both.
Here is a dbfiddle sample of this
Obviously, SELECT INTO will just be a simple backup, if you want new tables to have all the keys and keep being used afterwards, you can CREATE them manually first and then use
If you want to enable your history tracking on copies, you can do it with:
o3imoua42#
To complete the previous answer, it is possible to replicate a temporal table within the same database but with a different table name. You don't necessarily need to create a new database for this purpose.
You can follow the same steps as mentioned earlier, except that you would create the new temporal table with a different name and in the same database. For example:
This will create a new temporal table named "NewTemporalTable" with the same schema as the source temporal table, along with a new history table named "NewTemporalTableHistory".
Disable system versioning on the new temporal table by running the following command:
ALTER TABLE [dbo].[NewTemporalTable] SET (SYSTEM_VERSIONING = OFF);
Insert the data from the source temporal table into the new temporal table, excluding the system-versioned columns. You can achieve this by specifying the column names explicitly as follows:
INSERT INTO [dbo].[NewTemporalTable] ([Id], [Name]) SELECT [Id], [Name] FROM [dbo].[SourceTemporalTable];
ALTER TABLE [dbo].[NewTemporalTable] SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[NewTemporalTableHistory]));
You have now replicated a temporal table within the same database but with a different name, along with its entire history.
5jdjgkvh3#
It is possible to replicate temporal tables in SQL Server with its entire history. Here are the steps:
ALTER TABLE [NewTable] SET (SYSTEM_VERSIONING = OFF);
ALTER TABLE [NewTable] SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [NewTableHistory]));