This web page was created programmatically, to learn the article in its unique location you’ll be able to go to the hyperlink bellow:
https://devblogs.microsoft.com/azure-sql/time-travel-in-azure-sql-with-temporal-tables/
and if you wish to take away this text from our website please contact us
Applications typically must know what knowledge regarded like earlier than. Who modified it, when it modified, and what the earlier values had been. Rebuilding that historical past in software code is tedious and error inclined. This is very invaluable when exposing a database to an AI agent by means of MCP servers like SQL MCP Server, the place data discovery issues.
Learn extra about SQL MCP Server at
Azure SQL features a inbuilt characteristic that tracks row historical past robotically. Temporal tables let the database preserve a full change historical past with out triggers, audit tables, or customized logic.
A desk that robotically tracks each change over time.
A temporal desk requires two datetime columns that outline the validity interval. Azure SQL manages these robotically once you allow system versioning.
CREATE TABLE dbo.Todos
(
Id INT IDENTITY(1,1) PRIMARY KEY,
Title NVARCHAR(200) NOT NULL,
State NVARCHAR(20) NOT NULL DEFAULT 'pending',
ValidFrom DATETIME2 GENERATED ALWAYS AS ROW START,
ValidTo DATETIME2 GENERATED ALWAYS AS ROW END,
PERIOD FOR SYSTEM_TIME (ValidFrom, ValidTo)
)
WITH (
SYSTEM_VERSIONING = ON
(
HISTORY_TABLE = dbo.TodosHistory
)
);
GO
INSERT INTO dbo.Todos (Title, State) VALUES
('Buy groceries', 'pending'),
('Walk the canine', 'pending');
GO
This desk is created as a system-versioned temporal desk. The ValidFrom and ValidTo columns outline the interval throughout which every row model is legitimate, and Azure SQL maintains them robotically. With SYSTEM_VERSIONING = ON, each UPDATE and DELETE causes the earlier model of the row to be written to the historical past desk. By specifying HISTORY_TABLE = dbo.TodosHistory, you management the identify and schema of the historical past desk, whereas Azure SQL continues to handle its contents and lifecycle. If you don’t present a historical past desk, Azure SQL creates one for you robotically.
No particular syntax is required. Changes are tracked robotically.
UPDATE dbo.Todos
SET State="completed"
WHERE Title="Walk the dog";
GO
Each replace creates a historic model of the row.
SELECT * FROM dbo.Todos;
| Id | Title | State | ValidFrom | ValidTo |
|---|---|---|---|---|
| 1 | Buy groceries | pending | present | 9999-12-31 |
| 2 | Walk the canine | accomplished | present | 9999-12-31 |
Use FOR SYSTEM_TIME to question previous variations.
SELECT *
FROM dbo.Todos
FOR SYSTEM_TIME ALL
ORDER BY Id, ValidFrom;
| Id | Title | State | ValidFrom | ValidTo |
|---|---|---|---|---|
| 2 | Walk the canine | pending | earlier | replace time |
| 2 | Walk the canine | accomplished | replace time | 9999-12-31 |
DECLARE @time DATETIME2 = DATEADD(MINUTE, -1, SYSUTCDATETIME());
SELECT *
FROM dbo.Todos
FOR SYSTEM_TIME AS OF @time;
This returns the desk precisely because it existed at that second.
Temporal tables transfer knowledge historical past into the database engine itself. Once enabled, each change turns into observable, queryable, and dependable with out additional code.
This web page was created programmatically, to learn the article in its unique location you’ll be able to go to the hyperlink bellow:
https://devblogs.microsoft.com/azure-sql/time-travel-in-azure-sql-with-temporal-tables/
and if you wish to take away this text from our website please contact us
This web page was created programmatically, to learn the article in its authentic location you…
This web page was created programmatically, to learn the article in its authentic location you…
This web page was created programmatically, to learn the article in its unique location you'll…
This web page was created programmatically, to learn the article in its authentic location you…
This web page was created programmatically, to learn the article in its authentic location you…
This web page was created programmatically, to learn the article in its unique location you…