Last active
February 28, 2019 04:51
-
-
Save danielplawgo/ae9f50c7c9c3229e4a9b66762415ad42 to your computer and use it in GitHub Desktop.
Temporal Tables - Historia zmian w SQL Server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE dbo.Products | |
( | |
[Id] int IDENTITY(1,1) PRIMARY KEY, | |
[Name] nvarchar(250) NOT NULL, | |
[Description] varchar(max) NULL, | |
[ValidFrom] datetime2 (2) GENERATED ALWAYS AS ROW START, | |
[ValidTo] datetime2 (2) GENERATED ALWAYS AS ROW END, | |
PERIOD FOR SYSTEM_TIME (ValidFrom, ValidTo) | |
) | |
WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.ProductsHistory)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE dbo.Products | |
( | |
[Id] int IDENTITY(1,1) PRIMARY KEY, | |
[Name] nvarchar(250) NOT NULL, | |
[Description] varchar(max) NULL | |
); | |
ALTER TABLE dbo.Products | |
ADD ValidFrom datetime2(0) | |
GENERATED ALWAYS AS ROW START NOT NULL | |
DEFAULT SYSUTCDATETIME(), | |
ValidTo datetime2(0) | |
GENERATED ALWAYS AS ROW END NOT NULL | |
DEFAULT CAST('9999-12-31 23:59:59.9999999' AS datetime2), | |
PERIOD FOR SYSTEM_TIME (ValidFrom, ValidTo) | |
ALTER TABLE dbo.Products | |
SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.ProductsHistory)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INSERT INTO dbo.Products (Name, Description) VALUES ('Product Name', 'Product Description'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UPDATE dbo.Products SET Name = 'Updated Product Name' WHERE Id = 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DELETE FROM dbo.Products WHERE Id = 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT * From dbo.Products | |
FOR SYSTEM_TIME AS OF '2019-02-25 07:59:00.00' | |
WHERE Id = 1 | |
SELECT * From dbo.Products | |
FOR SYSTEM_TIME AS OF '2019-02-25 08:01:00.00' | |
WHERE Id = 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT * From dbo.Products | |
FOR SYSTEM_TIME FROM '2019-02-25 07:59:00.00' TO '2019-02-25 08:01:00.00' | |
WHERE Id = 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT * From dbo.Products | |
FOR SYSTEM_TIME ALL | |
WHERE Id = 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment