Deleting data from an application-period temporal table

Deleting data from an application-period temporal table removes rows from the table and can potentially result in new rows that are inserted into the application-period temporal table itself.

About this task

In addition to the regular DELETE statement, application-period temporal tables also support time range deletes where the DELETE statement includes the FOR PORTION OF BUSINESS_TIME clause. A row is a candidate for deletion if its period-begin column, period-end column, or both fall within the range specified in the FOR PORTION OF BUSINESS_TIME clause.

Procedure

To delete data from an application-period temporal table, use the DELETE FROM statement to delete data.
For example, it was discovered that policy A123 should not provide coverage from June 15, 2008 to August 15, 2008 and therefore that data should be deleted from the table that was updated in the Updating data in an application-period temporal table topic.
DELETE FROM policy_info
   FOR PORTION OF BUSINESS_TIME FROM '2008-06-15' TO '2008-08-15'
   WHERE policy_id = 'A123';

Results

The original policy_info table data is as follows:
Table 1. Data in the application-period temporal table (policy_info) before the DELETE statement
policy_id coverage bus_start bus_end
A123 12000 2008-01-01 2008-06-01
A123 14000 2008-06-01 2008-07-01
A123 14000 2008-07-01 2008-08-01
A123 16000 2008-08-01 2009-01-01
B345 18000 2008-03-01 2009-01-01
C567 25000 2008-01-01 2009-01-01
Deleting data from an application-period temporal table by using the FOR PORTION OF BUSINESS_TIME clause causes rows to be deleted and can result in rows that are inserted when the time period for a row covers a portion of the range specified in the DELETE FROM statement. Deleting data related to policy A123 applies to the BUSINESS_TIME period from 2008-06-15 to 2008-08-15. There are three rows in the policy_info table for policy_id A123 that include all or part of that time period.
The update to policy A123 affects the system-period temporal table and its history table, causing the following the things to occur:
  • There is one row where the BUSINESS_TIME period in the DELETE FROM statement covers the entire time period for a row. The row with a bus_start value of 2008-07-01 and a bus_end value of 2008-08-01 is deleted.
  • When only the bus_end value falls into the specified period, the row is deleted. A new row is inserted with the original values from the deleted row, except that the bus_end value is set to 2008-06-15.
  • When only the bus_start value falls into the specified period, the row is deleted. A new row is inserted with the original values from the deleted row, except that the bus_start value is set to 2008-08-15.
Table 2. Data in the application-period temporal table (policy_info) after the DELETE statement
policy_id coverage bus_start bus_end
A123 12000 2008-01-01 2008-06-01
A123 14000 2008-06-01 2008-06-15
A123 16000 2008-08-15 2009-01-01
B345 18000 2008-03-01 2009-01-01
C567 25000 2008-01-01 2009-01-01

Example

This section contains more deleting application-period temporal table examples.
Delete targets
The FOR PORTION OF BUSINESS_TIME clause can be used only when the target of the delete statement is a table or a view. The following DELETE statement returns an error:
DELETE FROM (SELECT * FROM policy_info) FOR PORTION OF BUSINESS_TIME 
   FROM '2008-01-01' TO '2008-06-15';