title: Enable SYSTEM_VERSIONING Error - Overlapping Dates in History Table
body: <p>I recently migrated my SQL 2019 database from a VM into Azure SQL.
  I used the MS Data Migration tool, but unfortunately, it wouldn't migrate data from Temporal Tables.</p>
  
  <p>So. I just used the tool to create the table schemas and then used SSIS to move the data.  </p>
  
  <p>Since my existing history table had data in it, I wanted to keep the SysStartDate and SysEndDate fields.  In order to do this, I had to disable SYSTEM_VERSIONING in my Azure SQL database as well as DROP the PERIOD on the table.</p>
  
  <p>The data migration was a success so I re-created my PERIOD on the table but when I tried to enable SYSTEM_VERSIONING with a specified history table, I get the following error:</p>
  
  <blockquote>
    <p>Msg 13573, Level 16, State 0, Line 34<br>
    Setting SYSTEM_VERSIONING to ON failed because history table 'xxxxxHistory'<br> contains overlapping records.</p>
  </blockquote>
  
  <p>I find this odd because the existing tables were originally joined as a temporal table so I don't understand why there would be a conflict now.</p>
  
  <pre><code>ALTER TABLE xxx.xxx 
  ADD PERIOD FOR SYSTEM_TIME(SysStartTime, SysEndTime)    
  
  ALTER TABLE xxx.xxx 
  SET (SYSTEM_VERSIONING = ON (HISTORY_TABLE=xxx.xxxHistory))
  </code></pre>
  
  <p>I expect to get a successful temporal table. Instead, I get the following error: </p>
  
  <blockquote>
    <p>Msg 13573, Level 16, State 0, Line 34<br>
    Setting SYSTEM_VERSIONING to ON failed because history table 'xxxxxHistory'<br> contains overlapping records.</p>
  </blockquote>
  
  <p>I ran the following query to identify the overlaps but I don't get any:</p>
  
  <pre><code>SELECT 
       xxxxKeyNumeric
       ,SysStartTime
       ,SysEndTime
  FROM 
      xxxx.xxxxhistory o
  WHERE EXISTS
  (
      SELECT 
          1 
      FROM 
          xxxx.xxxxhistory o2
      WHERE 
          o2.xxxxKeyNumeric = o.xxxxKeyNumeric
          AND o2.SysStartTime &lt;= o.SysEndTime
          AND o.SysStartTime &lt;= o2.SysEndTime
          AND o2.xxxxPK != o.xxxxPK
  )
  ORDER BY 
      o.xxxxKeyNumeric, 
      o.SysStartTime
  </code></pre>