复制临时表 SQL Server

huangapple go评论74阅读模式
英文:

Copy temporal table sql server

问题

我有一个在SQL Server中的时间/系统版本表,我想复制/复制这个表以及原始时间表的整个历史记录。

是否可以在SQL Server中复制时间表,如果可以的话,步骤是什么?

这只是一次复制当前可用表的操作。

谢谢

英文:

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

答案1

得分: 2

为了完成之前的回答,可以在同一数据库中使用不同的表名复制时间表。您不一定需要为此目的创建新数据库。

您可以按照之前提到的相同步骤进行操作,只是创建新的时间表时要使用不同的名称,并且在同一数据库中。例如:

  1. 使用 SQL Server Management Studio (SSMS) 或 Transact-SQL 命令 BACKUP DATABASE 对包含时间表的数据库进行完全备份。

  2. 使用 SSMS 或 Transact-SQL 命令 RESTORE DATABASE 在同一服务器或同一数据库上使用不同名称还原备份。

  3. 一旦数据库恢复,使用与源时间表相同的模式创建一个新的时间表,但使用不同的名称。为此,您可以运行以下 Transact-SQL 脚本:

CREATE TABLE [dbo].[NewTemporalTable] (
    [Id] INT NOT NULL,
    [Name] VARCHAR(50),
    [StartDate] DATETIME2 GENERATED ALWAYS AS ROW START HIDDEN NOT NULL,
    [EndDate] DATETIME2 GENERATED ALWAYS AS ROW END HIDDEN NOT NULL,
    PERIOD FOR SYSTEM_TIME ([StartDate], [EndDate]),
    CONSTRAINT [PK_NewTemporalTable_Id] PRIMARY KEY CLUSTERED ([Id])
) WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[NewTemporalTableHistory]));

这将创建一个名为 "NewTemporalTable" 的新时间表,其模式与源时间表相同,以及一个名为 "NewTemporalTableHistory" 的新历史表。

  1. 运行以下命令在新时间表上禁用系统版本控制:
ALTER TABLE [dbo].[NewTemporalTable] SET (SYSTEM_VERSIONING = OFF);
  1. 将来自源时间表的数据插入新时间表,不包括系统版本的列。您可以通过显式指定列名来实现此目的,如下所示:
INSERT INTO [dbo].[NewTemporalTable] ([Id], [Name])
SELECT [Id], [Name] FROM [dbo].[SourceTemporalTable];
  1. 运行以下命令在新时间表上重新启用系统版本控制:
ALTER TABLE [dbo].[NewTemporalTable] SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[NewTemporalTableHistory]));

现在,您已经在同一数据库中复制了一个具有不同名称的时间表,以及其完整的历史记录。

英文:

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:

  1. Make a full backup of the database that contains the temporal table using the SQL Server Management Studio (SSMS) or Transact-SQL command BACKUP DATABASE.

  2. Restore the backup on the same server or the same database with a different name using the SSMS or Transact-SQL command RESTORE DATABASE.

  3. Once the database is restored, create a new temporal table with a different name but with the same schema as the source temporal table. To do this, you can run the following Transact-SQL script:

> CREATE TABLE [dbo].[NewTemporalTable] (
> [Id] INT NOT NULL,
> [Name] VARCHAR(50),
> [StartDate] DATETIME2 GENERATED ALWAYS AS ROW START HIDDEN NOT NULL,
> [EndDate] DATETIME2 GENERATED ALWAYS AS ROW END HIDDEN NOT NULL,
> PERIOD FOR SYSTEM_TIME ([StartDate], [EndDate]),
> CONSTRAINT [PK_NewTemporalTable_Id] PRIMARY KEY CLUSTERED ([Id])
> ) WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [dbo].[NewTemporalTableHistory]));

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".

  1. Disable system versioning on the new temporal table by running the following command:

> ALTER TABLE [dbo].[NewTemporalTable] SET (SYSTEM_VERSIONING = OFF);

  1. 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];

  2. Re-enable system versioning on the new temporal table by running the following command:

> 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.

答案2

得分: 1

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.

如果您有名为Department的表格和名为DepartmentHistory的临时表格,您可以简单地使用SELECT INTO来复制两者。

SELECT * INTO Department_COPY FROM Department
SELECT * INTO DepartmentHistory_COPY FROM DepartmentHistory

从Department复制到Department_COPY
从DepartmentHistory复制到DepartmentHistory_COPY

Here is a dbfiddle sample of this

这是这个操作的dbfiddle示例

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

显然,SELECT INTO只是一个简单的备份,如果您希望新表格具有所有的键并且以后继续使用,您可以首先手动创建它们,然后使用以下命令:

INSERT INTO Department_COPY SELECT * FROM Department
INSERT INTO DepartmentHistory_COPY SELECT * FROM DepartmentHistory

将数据从Department复制到Department_COPY
将数据从DepartmentHistory复制到DepartmentHistory_COPY

If you want to enable your history tracking on copies, you can do it with:

如果您想要在复制上启用历史跟踪,可以使用以下方法:

ALTER TABLE Department_COPY ADD PERIOD
FOR SYSTEM_TIME([ValidFrom], [ValidTo]);

ALTER TABLE Department_COPY SET (
SYSTEM_VERSIONING = ON (
HISTORY_TABLE = dbo.DepartmentHistory_COPY,
DATA_CONSISTENCY_CHECK = ON
)
);

更改Department_COPY表格,添加时间段以进行系统时间跟踪。
更改Department_COPY表格设置,启用系统版本控制,使用DepartmentHistory_COPY作为历史表格,并启用数据一致性检查。

英文:

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.

SELECT * INTO Department_COPY FROM Department
SELECT * INTO DepartmentHistory_COPY FROM DepartmentHistory

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

INSERT INTO Department_COPY SELECT * FROM Department
INSERT INTO DepartmentHistory_COPY  SELECT * FROM DepartmentHistory

If you want to enable your history tracking on copies, you can do it with:

ALTER TABLE Department_COPY ADD PERIOD
FOR SYSTEM_TIME([ValidFrom], [ValidTo]);

ALTER TABLE Department_COPY SET (
    SYSTEM_VERSIONING = ON (
        HISTORY_TABLE = dbo.DepartmentHistory_COPY,
        DATA_CONSISTENCY_CHECK = ON
    )
);

答案3

得分: 0

可以在SQL Server中复制包括完整历史记录的时间表。以下是步骤:

  1. 创建一个新的目标数据库。

  2. 在新数据库中,创建一个与原始时间表具有相同架构的空表。

  3. 使用ALTER TABLE语句在新表上禁用系统版本控制:

    ALTER TABLE [NewTable] SET (SYSTEM_VERSIONING = OFF);

  4. 使用SELECT INTO语句将原始时间表的数据插入新表:

    INSERT INTO [NewTable]
    SELECT *
    FROM [OriginalTemporalTable]
    
  5. 在新表上启用系统版本控制:

    ALTER TABLE [NewTable] SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [NewTableHistory]));

英文:

It is possible to replicate temporal tables in SQL Server with its entire history. Here are the steps:

  1. Create a new database as destination.

  2. In the new database, create a empty table with the same schema as the original temporal table.

  3. Disable system versioning on the new table using the ALTER TABLE statement:

    ALTER TABLE [NewTable] SET (SYSTEM_VERSIONING = OFF);

  4. Insert data from the original temporal table into the new table using the SELECT INTO statement:

    INSERT INTO [NewTable]
    SELECT *
    FROM [OriginalTemporalTable]
    
  5. Enable system versioning on the new table:

    ALTER TABLE [NewTable] SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = [NewTableHistory]));

huangapple
  • 本文由 发表于 2023年6月27日 19:04:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76564218.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定