Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2018 March SQL Server 2016: Enable System-versioned Temporal Table on existing Table

SQL Server 2016: Enable System-versioned Temporal Table on existing Table

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

SQL Server 2016 introduced System-versioned Temporal Table to keep a history of data change by adding additional history table.

In the below article, You can read more about System-versioned Temporal Table and you must know all the rules before creating a System-versioned Temporal Table.

You can also enable System-versioned Temporal Table in the existing table.

Check this demonstration:

Create a table with sample data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
CREATE TABLE tbl_Employees
(
EmpID INT PRIMARY KEY
,EmpName VARCHAR(20)
,EmpDepartment VARCHAR(20)
,EmpSalary INT
)
GO
INSERT INTO tbl_Employees
VALUES
(1,'Anvesh','Database',90000)
,(2,'Jenny','JAVA',65000)
,(3,'Martin','PHP',85000)
,(4,'Roy','PHP',94000)
GO

Enable System-versioned Temporal Table on this table:

1
2
3
ALTER TABLE tbl_Employees
SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE=dbo.tbl_EmployeesHistory))
GO

You will get a below error because you must configure SYSTEM_TIME column.

1
2
Msg 13510, Level 16, State 1, Line 1
Cannot set SYSTEM_VERSIONING to ON when SYSTEM_TIME period is not defined.

Configure SYSTEM_TIME column:

1
2
3
4
5
6
ALTER TABLE tbl_Employees
ADD
StartTime DATETIME2 GENERATED ALWAYS AS ROW START DEFAULT GETUTCDATE()
,EndTime DATETIME2 GENERATED ALWAYS AS ROW END DEFAULT CONVERT(DATETIME2, '9999-12-31 23:59:59.9999999')
,PERIOD FOR SYSTEM_TIME (StartTime, EndTime)
GO

Now, enable System-versioned Temporal Table:

1
2
3
ALTER TABLE tbl_Employees
SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE=dbo.tbl_EmployeesHistory))
GO

Mar 7, 2018Anvesh Patel
SQL Server 2016: Drop table operation failed on table because it is not supported operation on system-versioned temporal tablesSQL Server Interview: Difference between Sequence Object and Identity Column

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

March 7, 2018 SQL ServerAnvesh Patel, database, database research and development, dbrnd, history table, history_table, SQL Query, SQL Server, SQL Server 2016, SQL Server Administrator, SQL Server Monitoring, SQL Server Performance Tunning, SQL Server Tips and Tricks, system_versioning, system-versioned temporal table, track data change, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....