How to find the history table of a temporal table?

Even though, you can use the object explore panel of SSMS to find the history table attached to a system versioned temporal table, there are situations where you need a SQL script to find the same.

To find the name of the history table you can use the system table sys.tables along with the system function OBJECT_NAME. From sys.tables, you have to use the column history_table_id to get the object_id of the history table for the given current table. This history_table_id will return object id only when the given table is current / temporal table, else it will return NULL.

For example, consider the table People is a temporal table (current table). Here is the script to get the name of the corresponding history table. Here is the script.

SELECT 
	OBJECT_NAME(history_table_id) AS 'History Table'
FROM sys.tables
WHERE name = 'People'
GO
find the history table of a temporal table

Related Article

Reference


Leave your thoughts...

This site uses Akismet to reduce spam. Learn how your comment data is processed.