SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Time Traveling with Temporal Tables on SQL Server 2016


Time Traveling with Temporal Tables on SQL Server 2016

Author
Message
JAhlen
JAhlen
Old Hand
Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)

Group: General Forum Members
Points: 352 Visits: 134
Comments posted to this topic are about the item Time Traveling with Temporal Tables on SQL Server 2016
PB_BI
PB_BI
SSCrazy Eights
SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)

Group: General Forum Members
Points: 8741 Visits: 2957
Great article. I hadn't really thought about the practical uses for this new feature beyond some sort of audit trail. This has opened my eyes to the possibilities.
Cheers :-)


Im on LinkedIn
Geoff A
Geoff A
SSCertifiable
SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)

Group: General Forum Members
Points: 5540 Visits: 1828
this is great info. thanks so much.

one question though.

when you ALTER an existing table and ADD the needed datetime2 columns and then ALTER it again to SET SYSTEM_VERSION = ON, will that create a new table in the database appended with the word HISTORY?

thanks!
JAhlen
JAhlen
Old Hand
Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)

Group: General Forum Members
Points: 352 Visits: 134
Geoff A (10/18/2016)
this is great info. thanks so much.

one question though.

when you ALTER an existing table and ADD the needed datetime2 columns and then ALTER it again to SET SYSTEM_VERSION = ON, will that create a new table in the database appended with the word HISTORY?

thanks!


Hi Geoff,

When you alter an existing table to SET SYSTEM_VERSIONING = ON, it will automatically create a history table if needed (or reuse an existing history table).

You can specify the name of the history table by the option HISTORY_TABLE = "", like in my CREATE TABLE example. If you don't specify the name of the history table, it will be something like MSSQL_TemporalHistoryFor_<objectid>. Probably you want to specify your own name instead.

Best Regards
Johan
nicoboey
nicoboey
Grasshopper
Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)Grasshopper (18 reputation)

Group: General Forum Members
Points: 18 Visits: 131
Hi,
great post!
Finally SQL Server can handle History data.
Is there any chance that the user that caused the change is also kept in the history file?
Regards,
Nico
JAhlen
JAhlen
Old Hand
Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)

Group: General Forum Members
Points: 352 Visits: 134
nicoboey (10/18/2016)
Hi,
great post!
Finally SQL Server can handle History data.
Is there any chance that the user that caused the change is also kept in the history file?
Regards,
Nico


Hi Nico,

No - there is no such information stored. You will have to add it from your application.

Best Regards
Johan
csj
csj
SSC-Enthusiastic
SSC-Enthusiastic (139 reputation)SSC-Enthusiastic (139 reputation)SSC-Enthusiastic (139 reputation)SSC-Enthusiastic (139 reputation)SSC-Enthusiastic (139 reputation)SSC-Enthusiastic (139 reputation)SSC-Enthusiastic (139 reputation)SSC-Enthusiastic (139 reputation)

Group: General Forum Members
Points: 139 Visits: 104
It is possible to add a new column, to modify a column and drop a column, while system_versioning is ON!!!

USE master;
GO
DROP DATABASE IF EXISTS TemporalDB;
GO
CREATE DATABASE TemporalDB;
GO
USE TemporalDB;
GO
CREATE TABLE dbo.Person
(
Personid INT NOT NULL IDENTITY PRIMARY KEY,
Firstname VARCHAR(20) NOT NULL,
Lastname VARCHAR(20) NULL,
Adress VARCHAR(30) NOT NULL,
Zipcode SMALLINT NOT NULL,
SysStartTime DATETIME2(0) GENERATED ALWAYS AS ROW START NOT NULL,
SysEndTime DATETIME2(0) GENERATED ALWAYS AS ROW END NOT NULL,
PERIOD FOR SYSTEM_TIME (SysStartTime,SysEndTime)
)
WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.PersonHistory));
GO
ALTER TABLE dbo.Person
ADD Tlfno VARCHAR(8) NULL;
GO
SELECT *
FROM sys.columns
WHERE object_id IN(OBJECT_ID('dbo.Person'), OBJECT_ID('dbo.PersonHistory'))
ORDER BY object_id;
GO
ALTER TABLE dbo.Person
ALTER COLUMN Tlfno CHAR(10) NULL;
GO
SELECT *
FROM sys.columns
WHERE object_id IN(OBJECT_ID('dbo.Person'), OBJECT_ID('dbo.PersonHistory'))
ORDER BY object_id;
GO
ALTER TABLE dbo.Person
DROP COLUMN Tlfno;
GO
SELECT *
FROM sys.columns
WHERE object_id IN(OBJECT_ID('dbo.Person'), OBJECT_ID('dbo.PersonHistory'))
ORDER BY object_id;
JAhlen
JAhlen
Old Hand
Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)

Group: General Forum Members
Points: 352 Visits: 134
csj (10/20/2016)
It is possible to add a new column, to modify a column and drop a column, while system_versioning is ON!!!

USE master;
GO
DROP DATABASE IF EXISTS TemporalDB;
GO
CREATE DATABASE TemporalDB;
GO
USE TemporalDB;
GO
CREATE TABLE dbo.Person
(
Personid INT NOT NULL IDENTITY PRIMARY KEY,
Firstname VARCHAR(20) NOT NULL,
Lastname VARCHAR(20) NULL,
Adress VARCHAR(30) NOT NULL,
Zipcode SMALLINT NOT NULL,
SysStartTime DATETIME2(0) GENERATED ALWAYS AS ROW START NOT NULL,
SysEndTime DATETIME2(0) GENERATED ALWAYS AS ROW END NOT NULL,
PERIOD FOR SYSTEM_TIME (SysStartTime,SysEndTime)
)
WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.PersonHistory));
GO
ALTER TABLE dbo.Person
ADD Tlfno VARCHAR(8) NULL;
GO
SELECT *
FROM sys.columns
WHERE object_id IN(OBJECT_ID('dbo.Person'), OBJECT_ID('dbo.PersonHistory'))
ORDER BY object_id;
GO
ALTER TABLE dbo.Person
ALTER COLUMN Tlfno CHAR(10) NULL;
GO
SELECT *
FROM sys.columns
WHERE object_id IN(OBJECT_ID('dbo.Person'), OBJECT_ID('dbo.PersonHistory'))
ORDER BY object_id;
GO
ALTER TABLE dbo.Person
DROP COLUMN Tlfno;
GO
SELECT *
FROM sys.columns
WHERE object_id IN(OBJECT_ID('dbo.Person'), OBJECT_ID('dbo.PersonHistory'))
ORDER BY object_id;


Hi Csj,

Thanks very much for pointing this out. It has obviously changed since I did my research and testing on Temporal Tables. I will update the article!

Best Regards!
Johan
Iwas Bornready
Iwas Bornready
SSC-Dedicated
SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)SSC-Dedicated (35K reputation)

Group: General Forum Members
Points: 35268 Visits: 886
Great idea, thanks.
Yev.d
Yev.d
SSC-Addicted
SSC-Addicted (409 reputation)SSC-Addicted (409 reputation)SSC-Addicted (409 reputation)SSC-Addicted (409 reputation)SSC-Addicted (409 reputation)SSC-Addicted (409 reputation)SSC-Addicted (409 reputation)SSC-Addicted (409 reputation)

Group: General Forum Members
Points: 409 Visits: 146
Thanks for the article, Johan!

It's good to know what's out there even though in many cases you can't really apply it in your workplace.

In regards to history clean-up, I'm surprised there isn't a standard option to set it up when creating a temporal table. Something like KEEP FOR n DAY/WEEK/MONTH.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search