{"id":86527,"date":"2020-03-01T02:58:18","date_gmt":"2020-03-01T02:58:18","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=86527"},"modified":"2020-03-01T02:58:18","modified_gmt":"2020-03-01T02:58:18","slug":"identity-column-vs-etl-process","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/identity-column-vs-etl-process\/","title":{"rendered":"Identity column vs ETL Process"},"content":{"rendered":"<p>Creating <strong>ETL<\/strong> process can pose so many challenges we will only fully discover them when really building the <strong>ETL<\/strong>.<\/p>\n<p>Before you complain about modelling, the example here has no relation to data warehouse modelling. The <strong>ETL<\/strong> in question was a migration from a data source to another. The need for executing multiple times was mainly for a testing purpose.<\/p>\n<p>Let&#8217;s get back a bit and understand what I&#8217;m talking about.<\/p>\n<p>Imagine you are building an <strong>ETL<\/strong>. Importing a lot of data to a set of tables and implementing many rules inside the <strong>ETL<\/strong>. Your <strong>ETL<\/strong> will be executed many times, but the tables need to be always empty when the process starts. As a result your <strong>ETL<\/strong> needs a cleaning task, in the beginning, clearing all the tables.<\/p>\n<p>Some tables have an <em>identity<\/em> field. Since you are clearing the tables and ensuring everything will start again from the same point, you need to reset the <em>identity<\/em> as well.<\/p>\n<p>Probably your first thought is to use <strong>Truncate Table<\/strong> statement. The problem is <strong>Truncate Table<\/strong> is not supported in tables referenced by a foreign key.<\/p>\n<p>Check the example below: The <strong>Truncate Table<\/strong> is blocked by the foreign key.<\/p>\n<div><span style=\"font-family: Courier New;font-size: 10pt\"> <span style=\"color: blue\">CREATE<\/span>\u00a0<span style=\"color: blue\">TABLE<\/span>\u00a0<span style=\"color: maroon\">class<\/span> <br \/>\n\u00a0\u00a0<span style=\"color: maroon\">(<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">id<\/span>\u00a0\u00a0\u00a0\u00a0<span style=\"color: black;font-style: italic\">INT<\/span>\u00a0<span style=\"color: blue\">IDENTITY<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: black\">1<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: black\">1<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: blue\">PRIMARY<\/span>\u00a0<span style=\"color: blue\">KEY<\/span><span style=\"color: silver\">,<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">title<\/span>\u00a0<span style=\"color: black;font-style: italic\">VARCHAR<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: black\">50<\/span><span style=\"color: maroon\">)<\/span> <br \/>\n\u00a0\u00a0<span style=\"color: maroon\">)<\/span> <br \/>\n<span style=\"color: maroon\">go<\/span> <\/p>\n<p><span style=\"color: blue\">CREATE<\/span>\u00a0<span style=\"color: blue\">TABLE<\/span>\u00a0<span style=\"color: maroon\">students<\/span> <br \/>\n\u00a0\u00a0<span style=\"color: maroon\">(<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">id<\/span>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: black;font-style: italic\">INT<\/span>\u00a0<span style=\"color: blue\">IDENTITY<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: black\">1<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: black\">1<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: blue\">PRIMARY<\/span>\u00a0<span style=\"color: blue\">KEY<\/span><span style=\"color: silver\">,<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: blue\">NAME<\/span>\u00a0\u00a0\u00a0\u00a0<span style=\"color: black;font-style: italic\">VARCHAR<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: black\">50<\/span><span style=\"color: maroon\">)<\/span><span style=\"color: silver\">,<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">classid<\/span>\u00a0<span style=\"color: black;font-style: italic\">INT<\/span><span style=\"color: silver\">,<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: blue\">FOREIGN<\/span>\u00a0<span style=\"color: blue\">KEY<\/span>\u00a0<span style=\"color: maroon\">(<\/span><span style=\"color: maroon\">classid<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: blue\">REFERENCES<\/span>\u00a0<span style=\"color: maroon\">class<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: maroon\">id<\/span><span style=\"color: maroon\">)<\/span> <br \/>\n\u00a0\u00a0<span style=\"color: maroon\">)<\/span> <br \/>\n<span style=\"color: maroon\">go<\/span> <\/p>\n<p><span style=\"color: blue\">TRUNCATE<\/span>\u00a0<span style=\"color: blue\">TABLE<\/span>\u00a0<span style=\"color: maroon\">class<\/span>\u00a0 <\/span><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-86529\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2020\/02\/Identity01.png\" alt=\"\" width=\"940\" height=\"87\" \/><\/p>\n<p>You need to use\u00a0<strong>Delete<\/strong> or drop and re-create the constraints. Choosing the simplest solution, let&#8217;s talk about using the <strong>Delete<\/strong>.<\/p>\n<p>Let&#8217;s consider the scenario where your <strong>ETL<\/strong> will need to run many times and need to start with a cleaning task. This scenario, sometimes, is used for staging servers in the middle of a bigger <strong>ETL<\/strong> process. I used for data migration and had to make the execution many times during development, always returning to the start point before executing again.<\/p>\n<p>That&#8217;s why you need a cleaning process, deleting all the records on the tables, but that&#8217;s not enough. The tables have identity fields and you also need to reset the identity seed before executing again the <strong>ETL<\/strong>.<\/p>\n<h2>Reseting the Identity Seed: Regular behaviour<\/p>\n<\/h2>\n<p>Reset an identity seed seems simple:<\/p>\n<div><span style=\"font-family: Courier New;font-size: 10pt\"> <span style=\"color: blue\">DBCC<\/span>\u00a0<span style=\"color: maroon\">checkident<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: red\">&#8216;testTable&#8217;<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: maroon\">reseed<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: black\">1<\/span><span style=\"color: maroon\">)<\/span>\u00a0 <\/span><\/div>\n<p>\nThe problem is the combination of <strong>Delete<\/strong> and the <strong>DBCC CheckIdent<\/strong>. This combination can lead to very strange results. Let&#8217;s see what can happen below:<\/p>\n<p>The first execution will be in an empty table, never used before.<\/p>\n<div><span style=\"font-family: Courier New;font-size: 10pt\"> <span style=\"color: blue\">CREATE<\/span>\u00a0<span style=\"color: blue\">TABLE<\/span>\u00a0<span style=\"color: maroon\">testtable<\/span> <br \/>\n\u00a0\u00a0<span style=\"color: maroon\">(<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">id<\/span>\u00a0\u00a0\u00a0\u00a0<span style=\"color: black;font-style: italic\">INT<\/span>\u00a0<span style=\"color: blue\">IDENTITY<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: black\">1<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: black\">1<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: blue\">PRIMARY<\/span>\u00a0<span style=\"color: blue\">KEY<\/span><span style=\"color: silver\">,<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">price<\/span>\u00a0<span style=\"color: black;font-style: italic\">NUMERIC<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: black\">15<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: black\">2<\/span><span style=\"color: maroon\">)<\/span> <br \/>\n\u00a0\u00a0<span style=\"color: maroon\">)<\/span> <br \/>\n<span style=\"color: maroon\">go<\/span> <\/p>\n<p><span style=\"color: blue\">DELETE<\/span>\u00a0<span style=\"color: maroon\">testtable<\/span> <\/p>\n<p><span style=\"color: blue\">DBCC<\/span>\u00a0<span style=\"color: maroon\">checkident<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: red\">&#8216;testTable&#8217;<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: maroon\">reseed<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: black\">1<\/span><span style=\"color: maroon\">)<\/span> <\/p>\n<p><span style=\"color: blue\">INSERT<\/span>\u00a0<span style=\"color: blue\">INTO<\/span>\u00a0<span style=\"color: maroon\">testtable<\/span> <br \/>\n<span style=\"color: blue\">VALUES<\/span>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">(<\/span><span style=\"color: black\">10<\/span><span style=\"color: maroon\">)<\/span> <\/p>\n<p><span style=\"color: blue\">SELECT<\/span>\u00a0<span style=\"color: silver\">*<\/span> <br \/>\n<span style=\"color: blue\">FROM<\/span>\u00a0\u00a0\u00a0<span style=\"color: maroon\">testtable<\/span>\u00a0<\/p>\n<p><\/span><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-86530\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2020\/02\/Identity02.png\" alt=\"\" width=\"263\" height=\"144\" \/><\/p>\n<p>The following executions will be in a table already used, having passed by a delete and <strong>dbcc checkident<\/strong>. It&#8217;s the same code executed again:<\/p>\n<p>&nbsp;<\/p>\n<div><span style=\"font-family: Courier New;font-size: 10pt\"> <span style=\"color: blue\">DELETE<\/span>\u00a0<span style=\"color: maroon\">testtable<\/span> <\/p>\n<p><span style=\"color: blue\">DBCC<\/span>\u00a0<span style=\"color: maroon\">checkident<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: red\">&#8216;testTable&#8217;<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: maroon\">reseed<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: black\">1<\/span><span style=\"color: maroon\">)<\/span> <\/p>\n<p><span style=\"color: blue\">INSERT<\/span>\u00a0<span style=\"color: blue\">INTO<\/span>\u00a0<span style=\"color: maroon\">testtable<\/span> <br \/>\n<span style=\"color: blue\">VALUES<\/span>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">(<\/span><span style=\"color: black\">10<\/span><span style=\"color: maroon\">)<\/span> <\/p>\n<p><span style=\"color: blue\">SELECT<\/span>\u00a0<span style=\"color: silver\">*<\/span> <br \/>\n<span style=\"color: blue\">FROM<\/span>\u00a0\u00a0\u00a0<span style=\"color: maroon\">testtable<\/span>\u00a0<\/p>\n<p><\/span><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-86531\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2020\/02\/Identity03.png\" alt=\"\" width=\"269\" height=\"140\" \/><\/p>\n<p>As you may notice, even making the <strong>dbcc checkident<\/strong> using the same seed value, the initial identity value in the table becomes different, what can cause all sort of problems for your software.<\/p>\n<p>The root cause of the problem is one statement, <strong>DBCC CheckIdent<\/strong>, behaving in different ways according to how the table is at the moment. Our cleaning script needs a consistent result and it&#8217;s not achieving this.<\/p>\n<p>&nbsp;<\/p>\n<h2>Investigating the root cause<\/h2>\n<p>\nWe will need to search deeper in <strong>SQL Server<\/strong> to discover why the <strong>checkident<\/strong> has these differences and where is <strong>SQL Server<\/strong> storing the current number for the Identity column of a table.<\/p>\n<p>The <strong>DMV<\/strong> <em>sys.identity_columns<\/em> has a column called <strong>LAST_VALUE<\/strong> which contains the last value used for the identity column. Let&#8217;s analyse the value this column stores during the execution of the script.<\/p>\n<div><span style=\"font-family: Courier New;font-size: 10pt\"> <span style=\"color: blue\">DROP<\/span>\u00a0<span style=\"color: blue\">TABLE<\/span>\u00a0<span style=\"color: maroon\">testtable<\/span> <br \/>\n<span style=\"color: maroon\">go<\/span> <\/p>\n<p><span style=\"color: blue\">CREATE<\/span>\u00a0<span style=\"color: blue\">TABLE<\/span>\u00a0<span style=\"color: maroon\">testtable<\/span> <br \/>\n\u00a0\u00a0<span style=\"color: maroon\">(<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">id<\/span>\u00a0\u00a0\u00a0\u00a0<span style=\"color: black;font-style: italic\">INT<\/span>\u00a0<span style=\"color: blue\">IDENTITY<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: black\">1<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: black\">1<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: blue\">PRIMARY<\/span>\u00a0<span style=\"color: blue\">KEY<\/span><span style=\"color: silver\">,<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">price<\/span>\u00a0<span style=\"color: black;font-style: italic\">NUMERIC<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: black\">15<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: black\">2<\/span><span style=\"color: maroon\">)<\/span> <br \/>\n\u00a0\u00a0<span style=\"color: maroon\">)<\/span> <br \/>\n<span style=\"color: maroon\">go<\/span> <\/p>\n<p><span style=\"color: blue\">DELETE<\/span>\u00a0<span style=\"color: maroon\">testtable<\/span> <\/p>\n<p><span style=\"color: blue\">SELECT<\/span>\u00a0<span style=\"color: maroon\">object_id<\/span><span style=\"color: silver\">,<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: blue\">NAME<\/span><span style=\"color: silver\">,<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">column_id<\/span><span style=\"color: silver\">,<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">last_value<\/span> <br \/>\n<span style=\"color: blue\">FROM<\/span>\u00a0\u00a0\u00a0<span style=\"color: maroon\">sys<\/span><span style=\"color: silver\">.<\/span><span style=\"color: maroon\">identity_columns<\/span> <br \/>\n<span style=\"color: blue\">WHERE<\/span>\u00a0\u00a0<span style=\"color: fuchsia;font-style: italic\">Object_name<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: maroon\">object_id<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: silver\">=<\/span>\u00a0<span style=\"color: red\">&#8216;testTable&#8217;<\/span>\u00a0<\/p>\n<p><\/span><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-86532\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2020\/02\/Identity04.png\" alt=\"\" width=\"728\" height=\"143\" \/><\/p>\n<p>\nJust after have been created, the <strong>Last_Value<\/strong> column contains <strong>NULL<\/strong>, even after the delete statement has been executed.<\/p>\n<p>&nbsp;<\/p>\n<div><span style=\"font-family: Courier New;font-size: 10pt\"> <span style=\"color: blue\">DBCC<\/span>\u00a0<span style=\"color: maroon\">checkident<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: red\">&#8216;testTable&#8217;<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: maroon\">reseed<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: black\">1<\/span><span style=\"color: maroon\">)<\/span> <\/p>\n<p><span style=\"color: blue\">SELECT<\/span>\u00a0<span style=\"color: maroon\">object_id<\/span><span style=\"color: silver\">,<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: blue\">NAME<\/span><span style=\"color: silver\">,<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">column_id<\/span><span style=\"color: silver\">,<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">last_value<\/span> <br \/>\n<span style=\"color: blue\">FROM<\/span>\u00a0\u00a0\u00a0<span style=\"color: maroon\">sys<\/span><span style=\"color: silver\">.<\/span><span style=\"color: maroon\">identity_columns<\/span> <br \/>\n<span style=\"color: blue\">WHERE<\/span>\u00a0\u00a0<span style=\"color: fuchsia;font-style: italic\">Object_name<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: maroon\">object_id<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: silver\">=<\/span>\u00a0<span style=\"color: red\">&#8216;testTable&#8217;<\/span>\u00a0<\/p>\n<p><\/span><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-86533\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2020\/02\/Identity05.png\" alt=\"\" width=\"738\" height=\"136\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Even after the execution of the <strong>checkident<\/strong>, the <strong>Last_Value<\/strong> column remains with <strong>NULL<\/strong> value.<\/p>\n<p>Let&#8217;s insert a record:<\/p>\n<div><span style=\"font-family: Courier New;font-size: 10pt\"> <span style=\"color: blue\">INSERT<\/span>\u00a0<span style=\"color: blue\">INTO<\/span>\u00a0<span style=\"color: maroon\">testtable<\/span> <br \/>\n<span style=\"color: blue\">VALUES<\/span>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">(<\/span><span style=\"color: black\">10<\/span><span style=\"color: maroon\">)<\/span> <br \/>\n<span style=\"color: maroon\">go<\/span> <span style=\"color: black\">2<\/span> <\/p>\n<p><span style=\"color: blue\">SELECT<\/span>\u00a0<span style=\"color: silver\">*<\/span> <br \/>\n<span style=\"color: blue\">FROM<\/span>\u00a0\u00a0\u00a0<span style=\"color: maroon\">testtable<\/span>\u00a0<\/p>\n<p><\/span><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-86534\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2020\/02\/Identity06.png\" alt=\"\" width=\"264\" height=\"180\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The <strong>Null<\/strong> value on <strong>Last_Value<\/strong> results in a first identity as 1, which is the exact value set as a seed for the identity.<\/p>\n<p>Let&#8217;s repeat again:<\/p>\n<div><span style=\"font-family: Courier New;font-size: 10pt\"> <span style=\"color: blue\">DELETE<\/span>\u00a0<span style=\"color: maroon\">testtable<\/span> <\/p>\n<p><span style=\"color: blue\">SELECT<\/span>\u00a0<span style=\"color: maroon\">object_id<\/span><span style=\"color: silver\">,<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: blue\">NAME<\/span><span style=\"color: silver\">,<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">column_id<\/span><span style=\"color: silver\">,<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">last_value<\/span> <br \/>\n<span style=\"color: blue\">FROM<\/span>\u00a0\u00a0\u00a0<span style=\"color: maroon\">sys<\/span><span style=\"color: silver\">.<\/span><span style=\"color: maroon\">identity_columns<\/span> <br \/>\n<span style=\"color: blue\">WHERE<\/span>\u00a0\u00a0<span style=\"color: fuchsia;font-style: italic\">Object_name<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: maroon\">object_id<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: silver\">=<\/span>\u00a0<span style=\"color: red\">&#8216;testTable&#8217;<\/span>\u00a0<\/p>\n<p><\/span><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-86535\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2020\/02\/Identity07.png\" alt=\"\" width=\"739\" height=\"146\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>After the delete, the <strong>last_value<\/strong> is 2, the last identity used. We need the <strong>checkident<\/strong>:<\/p>\n<p>&nbsp;<\/p>\n<div><span style=\"font-family: Courier New;font-size: 10pt\"> <span style=\"color: blue\">DBCC<\/span>\u00a0<span style=\"color: maroon\">checkident<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: red\">&#8216;testTable&#8217;<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: maroon\">reseed<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: black\">1<\/span><span style=\"color: maroon\">)<\/span> <\/p>\n<p><span style=\"color: blue\">SELECT<\/span>\u00a0<span style=\"color: maroon\">object_id<\/span><span style=\"color: silver\">,<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: blue\">NAME<\/span><span style=\"color: silver\">,<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">column_id<\/span><span style=\"color: silver\">,<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">last_value<\/span> <br \/>\n<span style=\"color: blue\">FROM<\/span>\u00a0\u00a0\u00a0<span style=\"color: maroon\">sys<\/span><span style=\"color: silver\">.<\/span><span style=\"color: maroon\">identity_columns<\/span> <br \/>\n<span style=\"color: blue\">WHERE<\/span>\u00a0\u00a0<span style=\"color: fuchsia;font-style: italic\">Object_name<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: maroon\">object_id<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: silver\">=<\/span>\u00a0<span style=\"color: red\">&#8216;testTable&#8217;<\/span>\u00a0<\/p>\n<p><\/span><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-86536\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2020\/02\/Identity08.png\" alt=\"\" width=\"738\" height=\"136\" \/><\/p>\n<p>Now we can easily see the difference: When the table is empty the <strong>last_value<\/strong> is <strong>NULL<\/strong> and it continues to be <strong>NULL<\/strong> after the <strong>checkident<\/strong>. However, when the table already had some records included, the <strong>last_value<\/strong> is filled and the <strong>checkident<\/strong> will reseed it to the value we are providing. So, it&#8217;s a difference between <strong>last_value<\/strong> field being\u00a0<strong>NULL<\/strong> or the seed value.<\/p>\n<h2>Solving the problem<\/h2>\n<p>Knowing these details, we can create a query to calculate the next identity value for a table. If the <strong>last_value<\/strong> is <strong>NULL<\/strong> the next value will be the seed value, if <strong>last_value<\/strong> has a value, the next one will be the <strong>last_value<\/strong> + the increment<\/p>\n<p>&nbsp;<\/p>\n<div><span style=\"font-family: Courier New;font-size: 10pt\"> <span style=\"color: blue\">SELECT<\/span>\u00a0<span style=\"color: #ff0080;font-weight: bold\">Isnull<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: fuchsia;font-style: italic\">CONVERT<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: black;font-style: italic\">INT<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: maroon\">last_value<\/span><span style=\"color: maroon\">)<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: silver\">+<\/span>\u00a0<span style=\"color: fuchsia;font-style: italic\">CONVERT<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: black;font-style: italic\">INT<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: maroon\">increment_value<\/span><span style=\"color: maroon\">)<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: fuchsia;font-style: italic\">CONVERT<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: black;font-style: italic\">INT<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: maroon\">seed_value<\/span><span style=\"color: maroon\">)<\/span><span style=\"color: maroon\">)<\/span> <br \/>\n<span style=\"color: blue\">FROM<\/span>\u00a0\u00a0\u00a0<span style=\"color: maroon\">sys<\/span><span style=\"color: silver\">.<\/span><span style=\"color: maroon\">identity_columns<\/span> <br \/>\n<span style=\"color: blue\">WHERE<\/span>\u00a0\u00a0<span style=\"color: fuchsia;font-style: italic\">Object_name<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: maroon\">object_id<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: silver\">=<\/span>\u00a0<span style=\"color: red\">&#8216;testTable&#8217;<\/span>\u00a0 <\/span><\/div>\n<p>&nbsp;<\/p>\n<p>The <strong>Convert<\/strong>s are needed because on this <strong>DMV<\/strong> the columns are of type <strong>sql_variant<\/strong>, so we need to convert them to an integer.<\/p>\n<p>Knowing how to find the correct value we can now fix our cleaning code in order to always have the same result, with the identity starting in 1.<\/p>\n<p>&nbsp;<\/p>\n<div><span style=\"font-family: Courier New;font-size: 10pt\"> <span style=\"color: blue\">DECLARE<\/span>\u00a0<span style=\"color: #8000ff\">@lastValue<\/span>\u00a0<span style=\"color: black;font-style: italic\">INT<\/span> <\/p>\n<p><span style=\"color: blue\">DELETE<\/span>\u00a0<span style=\"color: maroon\">testtable<\/span> <\/p>\n<p><span style=\"color: blue\">SELECT<\/span>\u00a0<span style=\"color: #8000ff\">@lastValue<\/span>\u00a0<span style=\"color: silver\">=<\/span>\u00a0<span style=\"color: maroon\">last_value<\/span> <br \/>\n<span style=\"color: blue\">FROM<\/span>\u00a0\u00a0\u00a0<span style=\"color: maroon\">sys<\/span><span style=\"color: silver\">.<\/span><span style=\"color: maroon\">identity_columns<\/span> <br \/>\n<span style=\"color: blue\">WHERE<\/span>\u00a0\u00a0<span style=\"color: fuchsia;font-style: italic\">Object_name<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: maroon\">object_id<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: silver\">=<\/span>\u00a0<span style=\"color: red\">&#8216;testTable&#8217;<\/span> <\/p>\n<p><span style=\"color: blue\">IF<\/span>\u00a0<span style=\"color: maroon\">(<\/span>\u00a0<span style=\"color: #8000ff\">@lastValues<\/span>\u00a0<span style=\"color: blue\">IS<\/span>\u00a0<span style=\"color: blue\">NULL<\/span>\u00a0<span style=\"color: maroon\">)<\/span> <br \/>\n\u00a0\u00a0<span style=\"color: blue\">DBCC<\/span>\u00a0<span style=\"color: maroon\">checkident<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: red\">&#8216;testTable&#8217;<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: maroon\">reseed<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: black\">1<\/span><span style=\"color: maroon\">)<\/span> <br \/>\n<span style=\"color: blue\">ELSE<\/span> <br \/>\n\u00a0\u00a0<span style=\"color: blue\">DBCC<\/span>\u00a0<span style=\"color: maroon\">checkident<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: red\">&#8216;testTable&#8217;<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: maroon\">reseed<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: black\">0<\/span><span style=\"color: maroon\">)<\/span>\u00a0 <\/p>\n<p>\n<\/span><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Creating ETL process can pose so many challenges we will only fully discover them when really building the ETL. Before you complain about modelling, the example here has no relation to data warehouse modelling. The ETL in question was a migration from a data source to another. The need for executing multiple times was mainly&#8230;&hellip;<\/p>\n","protected":false},"author":50808,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2,1],"tags":[4824,4287,4149],"coauthors":[6810],"class_list":["post-86527","post","type-post","status-publish","format-standard","hentry","category-blogs","category-uncategorized","tag-etl","tag-identity","tag-learn-sql-server"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/86527","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/50808"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=86527"}],"version-history":[{"count":3,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/86527\/revisions"}],"predecessor-version":[{"id":86538,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/86527\/revisions\/86538"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=86527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=86527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=86527"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=86527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}