{"id":886,"date":"2011-09-27T20:20:07","date_gmt":"2011-09-28T01:20:07","guid":{"rendered":"http:\/\/anvil-of-time.com\/wordpress\/?p=886"},"modified":"2021-03-10T20:57:02","modified_gmt":"2021-03-11T01:57:02","slug":"sql-server-identity-columns-inserting-and-resetting","status":"publish","type":"post","link":"https:\/\/anvil-of-time.com\/ms-sql-server\/sql-server-identity-columns-inserting-and-resetting\/","title":{"rendered":"SQL Server Identity Columns &#8211; Inserting and Resetting"},"content":{"rendered":"<p>So I wanted to test a scenario against a database table which had no rows, even though there were several currently in the table.  Easy you say, just copy the data into a &#8216;backup&#8217; table, delete the rows, run your tests, then copy the data back.  Well not so fast when you have an IDENTITY column defined in the table.<\/p>\n<p>The first step is still simple and straightforward.<\/p>\n<pre name=\"code\" class=\"sql\"> SELECT id\n\t, code\n\t, description \nINTO backup_Widget\nFROM Widget<\/pre>\n<p>This copies the rows from Widget into a new table backup_Widget.  The new table columns have the same datatypes as the source table.<\/p>\n<p>Now remove the rows with<\/p>\n<pre name=\"code\" class=\"sql\">DELETE FROM Widget<\/pre>\n<p>Now if you try to &#8216;return&#8217; the rows from the backup table to the regular table<\/p>\n<pre name=\"code\" class=\"sql\">INSERT INTO Widget (\n\tid\n\t,code\n\t,description)\n\tSELECT id\n\t,code\n\t,description\n\tFROM backup_Widget<\/pre>\n<p>You the an error: <em>&#8220;Cannot insert explicit value for identity column in table &#8216;Widget&#8217; when IDENTITY_INSERT is set to OFF.&#8221;<\/em><\/p>\n<p>Now, if you don&#8217;t care what values are in the IDENTITY column you can just re-run the insert but without the reference to the id column.  Also, if you have sequential IDENTITY values without any gaps you can simply TRUNCATE the table, which resets the IDENTITY seed value, and re-run the insert with an &#8220;ORDER BY id&#8221; clause.<\/p>\n<p>If you have gaps in the IDENTITY values and must restore the data with exactly the same ids you need to do something like this.<\/p>\n<pre name=\"code\" class=\"sql\">set IDENTITY_INSERT dbo.Widget ON\nGO\n\nINSERT INTO Widget (\n\tid\n\t,code\n\t,description)\n\tSELECT id\n\t,code\n\t,description\n\tFROM backup_Widget\nGO<\/pre>\n<p>Now the data is restored with the same IDENTITY values it had originally.<\/p>\n<p>There are a few other commands of interest when dealing with IDENTITY values.<\/p>\n<p>To check the current IDENTITY value for a table:<\/p>\n<pre name=\"code\" class=\"sql\">DBCC CHECKIDENT(Widget)<\/pre>\n<p>Which gives you something like: <em>Checking identity information: current identity value &#8217;12&#8217;, current column value &#8217;12&#8217;<\/em><\/p>\n<p>To &#8216;reseed&#8217; the table which means give the table a new seed value:<\/p>\n<pre name=\"code\" class=\"sql\">DBCC CHECKIDENT(Widget, RESEED, 20)<\/pre>\n<p>Gives you something like: <em>Checking identity information: current identity value &#8217;12&#8217;, current column value &#8217;20&#8217;.<\/em><\/p>\n<p>Since the &#8216;current column value&#8217; shows the last used seed value, the next record inserted into the Widget table will have an id of &#8217;21&#8217; (the default increment being 1).  You can omit the reseed value on the statement and the new seed value will be the maximum value in the IDENTITY column if the current seed value is less than this.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So I wanted to test a scenario against a database table which had no rows, even though there were several currently in the table. Easy you say, just copy the data into a &#8216;backup&#8217; table, delete the rows, run your tests, then copy the data back. Well not so fast when you have an IDENTITY&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[15],"_links":{"self":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/886"}],"collection":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/comments?post=886"}],"version-history":[{"count":5,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/886\/revisions"}],"predecessor-version":[{"id":1983,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/posts\/886\/revisions\/1983"}],"wp:attachment":[{"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/media?parent=886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/categories?post=886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/anvil-of-time.com\/wp-json\/wp\/v2\/tags?post=886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}