Skip to main content

Temporal tables of SQL Server 2016 not temporary


Temporal table is an add on to SQL Server 2016 which allows history maintaining. In simple terms it allows us to have a system versioning. For an example if we have a person's salary as 1000 usd as per today and tomorrow if it is updated as 1005 usd in such scenario the temporal table maintains two tables namely the generic one and a history table where the present record will be stored in the normal table and the previous one will be stored in the history table.

Let’s dig in deeper that with an example, First off I will create a employee temporal table with system versioning enabled. In this case even though we are creating a single table it creates two identical tables with one having a history part concatenated to it's name.

***Note : For easier identification the queries are given in italic.

CREATE TABLE [master].dbo.Employee  
(   
  [EmployeeID] int NOT NULL PRIMARY KEY CLUSTERED  
  , [Name] nvarchar(100) NOT NULL 
  , [Position] varchar(100) NOT NULL  
  , [AnnualSalary] decimal (10,2) NOT 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.EmployeeHistory)); 


Now we will insert a record to it,

INSERT INTO [master].dbo.Employee([EmployeeID],[Name],[Position],[AnnualSalary])  VALUES(1,'sam','lawyer',1000)

Now we can now use a select statement to check the insertion has happened,

Select * from dbo.Employee

Now to see the functionality of a temporal table I will provide an update to the existing record.

UPDATE [master].dbo.Employee
SET [Position]='Engineer'
where [EmployeeID]=1

Now I will select the record from the employee and employee history table(system generated) to see both current and historical record,

select * from employee
select * from employeehistory

Now the temporal table allows to select the record as per the time we are providing,

Declare @currentdatetime as datetime
SET @currentdatetime = (select getdate() as datetime)

SELECT * FROM employee for system_time as of  @currentdatetime
WHERE EmployeeID =1

Simply there will be a single version of truth according to the time we are providing unless we give the exact same time a new record is overriding the previous one. If we are planning to drop a temporal table It’s not just a simple drop statement. Fist we want to off the system versioning then drop both history and the normal table.

ALTER TABLE [dbo].[Employee] SET ( SYSTEM_VERSIONING = OFF  )
DROP TABLE [dbo].[Employee]
DROP TABLE [dbo].[EmployeeHistory]

In a holistic view temporal table is a great add on to maintain historical data without the intervene of an SSIS package.








Comments

Popular posts from this blog

Choosing color scheme for Power BI reports

Choosing beautiful colors for an attention seeking Power BI report is always challenging. Sometimes we may have an important message but it might go unseen if proper color scheme is not used, so it is important to create reports which are informational and visually appealing. In this blog our main objective is to get an idea about selecting appropriate color scheme and tools that can be beneficial to do this. What is a color scheme? As you all know not all colors look great together and you can tell stories by just having only two colors, have a look at the following image, there in the middle you can see a circle with same color and same dimension, only thing that changes is the back ground, but depending on the back ground the way you see those circles are totally different. Some small circles appear softer, some appear sharper, and some are brighter also you might notice movements or depth effect too. It is important to choose colors wisely to serve the purp

Ragged hierarchy in Power BI

Case- I have a data set which has to be visualized in a ragged hierarchy. What is ragged hierarchy A ragged hierarchy is a user defined hierarchy with an uneven number of levels.   You can see the above table which has 5 levels of hierarchy. In the first level we only have “World” as a category then moving forward to the next level we have two categories  namely “UK” and “USA” likewise the level of hierarchy is drilling down up to five levels with an uneven amount of categories. Creating a hierarchy in Power BI In the above image  you can notice that I have right clicked the “level 1” column which has to be the first level of our hierarchy, Once it is right clicked there you will notice “New hierarchy” as the second option then you will have to click that in order to create a new one. As you can notice in the above image once I have created a new hierarchy then a new column is created named “Level 1 Hierarchy” in that I have dragged and dro

Five tips to enhance a tableau model with usability features.

In the previous blogs I was more focused in Power BI since i work with it day to day, but this time we are going to dig into increasing the usability features of tableau. This blog revolves around the end-user simplicity, if a model is hard to comprehend then it will be a disaster, so we should deploy some means to stay simple. So without further ado lets head on to the first topic of the day. Configuring table and  column  properties. A model can consist of many tables but the important fact is they have to be unique and it is better if we can have a table description to elaborate on whats it about. This can be also considered as an internal documentation, when a project member is changed it is easy to pick up and continue. We use connections to load data for the tables and we have a connection name for it but once if we set the source it cannot be changed in the latter part, so be mindful because once you are dropping a connection you are losing all the tables assoc