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
SSC-Enthusiastic
SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)

Group: General Forum Members
Points: 116 Visits: 127
Comments posted to this topic are about the item Time Traveling with Temporal Tables on SQL Server 2016
PB_BI
PB_BI
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3112 Visits: 2674
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 :-)




I'm on LinkedIn
Geoff A
Geoff A
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1734 Visits: 1808
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
SSC-Enthusiastic
SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)

Group: General Forum Members
Points: 116 Visits: 127
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
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 113
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
SSC-Enthusiastic
SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)

Group: General Forum Members
Points: 116 Visits: 127
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 Journeyman
SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)SSC Journeyman (93 reputation)

Group: General Forum Members
Points: 93 Visits: 99
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
SSC-Enthusiastic
SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)SSC-Enthusiastic (116 reputation)

Group: General Forum Members
Points: 116 Visits: 127
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
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18684 Visits: 885
Great idea, thanks.
Yev.d
Yev.d
SSC Veteran
SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)SSC Veteran (234 reputation)

Group: General Forum Members
Points: 234 Visits: 129
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