Data manipulation in temporal tables

In this section, you will see what happens when data is inserted, updated, or deleted into/in/from a temporal table. Note that all manipulations will be done against the current table. The history table is protected; only the system can write to it.

Even members of the sysadmin server role cannot insert, update, or delete rows from a history table of a system-versioned temporal table in SQL Server 2017.

As an example, you will use the dbo.Product temporal table created in the previous section. Use this code to recreate the table:

USE WideWorldImporters; 
--remove existing temporal table if exists ALTER TABLE dbo.Product SET (SYSTEM_VERSIONING = OFF); ALTER TABLE dbo.Product DROP PERIOD FOR SYSTEM_TIME; DROP TABLE IF EXISTS dbo.Product; DROP TABLE IF EXISTS dbo.ProductHistory;
GO
CREATE TABLE dbo.Product ( ProductId INT NOT NULL CONSTRAINT PK_Product PRIMARY KEY, ProductName NVARCHAR(50) NOT NULL, Price MONEY NOT NULL, ValidFrom DATETIME2 GENERATED ALWAYS AS ROW START NOT NULL, ValidTo DATETIME2 GENERATED ALWAYS AS ROW END NOT NULL, PERIOD FOR SYSTEM_TIME (ValidFrom, ValidTo) ) WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.ProductHistory));
GO

Now, you can insert, update, and delete rows in this table to demonstrate how these actions affect the current and history table.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.142.2