{"id":842,"date":"2021-04-19T13:45:55","date_gmt":"2021-04-19T13:45:55","guid":{"rendered":"https:\/\/datarebus.com\/?p=842"},"modified":"2021-05-27T08:32:17","modified_gmt":"2021-05-27T08:32:17","slug":"set-identity_insert-on-off","status":"publish","type":"post","link":"https:\/\/datarebus.com\/index.php\/2021\/04\/19\/set-identity_insert-on-off\/","title":{"rendered":"Set Identity_insert ON\/Off"},"content":{"rendered":"\n<p>Assume that all we know why we are using IDENTITY column in a table.<\/p>\n\n\n\n<p>It you are inserting any row and getting error:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>\u201cServer: Msg 8101, Level 16, State 1, Line 2<br>An explicit value for the identity column in table &#8216;productsales&#8217; can only be specified when&nbsp;<em>a column list is used<\/em>&nbsp;and IDENTITY_INSERT is ON.\u201d <\/p><\/blockquote>\n\n\n\n<p><\/p>\n\n\n\n<p>Error message, than you should use set identity_insert on\/off command<\/p>\n\n\n\n<div class=\"wp-container-2 wp-block-group has-colibri-color-4-color has-text-color\"><div class=\"wp-block-group__inner-container\">\n<div class=\"wp-container-1 wp-block-group\"><div class=\"wp-block-group__inner-container\">\n<blockquote class=\"wp-block-quote is-style-default\"><p>USE AdventureWorks2012;&nbsp;<\/p><p>GO&nbsp;<\/p><p><span class=\"has-inline-color has-colibri-color-3-color\">&#8212; Create tool table.&nbsp;<\/span><\/p><p>CREATE TABLE dbo.Tool(&nbsp;<\/p><p>&nbsp;&nbsp; ID INT IDENTITY NOT NULL PRIMARY KEY,&nbsp;&nbsp;<\/p><p>&nbsp;&nbsp; Name VARCHAR(40) NOT NULL&nbsp;<\/p><p>);&nbsp;<\/p><p>GO&nbsp;<\/p><p><span class=\"has-inline-color has-colibri-color-3-color\">&#8212; Inserting values into products table.&nbsp;<\/span><\/p><p>INSERT INTO dbo.Tool(Name)&nbsp;&nbsp;<\/p><p>VALUES (&#8216;Screwdriver&#8217;)&nbsp;<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; , (&#8216;Hammer&#8217;)&nbsp;<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; , (&#8216;Saw&#8217;)&nbsp;<\/p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; , (&#8216;Shovel&#8217;);&nbsp;<\/p><p>GO&nbsp;<\/p><p><span class=\"has-inline-color has-colibri-color-3-color\">&#8212; Create a gap in the identity values.&nbsp;<\/span><\/p><p>DELETE dbo.Tool&nbsp;<\/p><p>WHERE Name = &#8216;Saw&#8217;;&nbsp;<\/p><p>GO&nbsp;<\/p><p>SELECT *&nbsp;&nbsp;<\/p><p>FROM dbo.Tool;&nbsp;<\/p><p>GO&nbsp;<\/p><p><span class=\"has-inline-color has-colibri-color-3-color\">&#8212; Try to insert an explicit ID value of 3;&nbsp;<\/span><\/p><p><span class=\"has-inline-color has-colibri-color-3-color\">&#8212; should return an error:<\/span><\/p><p><span class=\"has-inline-color has-colibri-color-3-color\">&#8212; An explicit value for the identity column in table &#8216;AdventureWorks2012.dbo.Tool&#8217; can only be specified when a column list is used and IDENTITY_INSERT is ON.<\/span><\/p><p>INSERT INTO dbo.Tool (ID, Name) VALUES (3, &#8216;Garden shovel&#8217;);&nbsp;<\/p><p>GO&nbsp;<\/p><p><span class=\"has-inline-color has-colibri-color-3-color\">&#8212; SET IDENTITY_INSERT to ON.&nbsp;<\/span><\/p><p>SET IDENTITY_INSERT dbo.Tool ON;&nbsp;<\/p><p>GO&nbsp;<\/p><p><span class=\"has-inline-color has-colibri-color-3-color\">&#8212; Try to insert an explicit ID value of 3.&nbsp;<\/span><\/p><p>INSERT INTO dbo.Tool (ID, Name) VALUES (3, &#8216;Garden shovel&#8217;);&nbsp;<\/p><p>GO&nbsp;<\/p><p>SELECT *&nbsp;&nbsp;<\/p><p>FROM dbo.Tool;&nbsp;<\/p><p>GO&nbsp;<\/p><p><span class=\"has-inline-color has-colibri-color-3-color\">&#8212; Drop products table.&nbsp;<\/span><\/p><p>DROP TABLE dbo.Tool;&nbsp;<\/p><p>GO&nbsp;<\/p><\/blockquote>\n<\/div><\/div>\n<\/div><\/div>\n\n\n\n<p>As you have seen in example, set identity_insert On command tells SQL Server that you are inserting identity value without wondering what identity no is the next.<\/p>\n\n\n\n<div class=\"wp-block-columns\">\n<div class=\"wp-block-column\" style=\"flex-basis:100%\">\n<h2>But be care that you must write whole column names in your insert script. In other words normal way of inserting is:<\/h2>\n<\/div>\n<\/div>\n\n\n\n<p>Insert into dbo.Tool VALUES (&#8216;Garden shovel&#8217;);&nbsp;<\/p>\n\n\n\n<p><span class=\"has-inline-color has-colibri-color-3-color\">\/* SQL Server assigns next identity no (let\u2019s say 4) and NULL values for others. &nbsp;*\/<\/span><\/p>\n\n\n\n<p>If you want to insert without caring identity, you must specify the identity column name and the value:<\/p>\n\n\n\n<p><span class=\"has-inline-color has-colibri-color-3-color\">&#8212; SET IDENTITY_INSERT to ON.&nbsp;<\/span><\/p>\n\n\n\n<p>SET IDENTITY_INSERT dbo.Tool ON;&nbsp;<\/p>\n\n\n\n<p>GO&nbsp;<\/p>\n\n\n\n<p><span class=\"has-inline-color has-colibri-color-3-color\">&#8212; Try to insert an explicit ID value of 3.&nbsp;<\/span><\/p>\n\n\n\n<p>INSERT INTO dbo.Tool (ID, Name) VALUES (3, &#8216;Garden shovel&#8217;);&nbsp;<\/p>\n\n\n\n<p>GO&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Assume that all we know why we are using IDENTITY column in a table. It you are inserting any row and getting error: \u201cServer: Msg 8101, Level 16, State 1, Line 2An explicit value for the identity column in table &#8216;productsales&#8217; can only be specified when&nbsp;a column list is used&nbsp;and IDENTITY_INSERT is ON.\u201d Error message, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":845,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,16],"tags":[],"_links":{"self":[{"href":"https:\/\/datarebus.com\/index.php\/wp-json\/wp\/v2\/posts\/842"}],"collection":[{"href":"https:\/\/datarebus.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datarebus.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datarebus.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datarebus.com\/index.php\/wp-json\/wp\/v2\/comments?post=842"}],"version-history":[{"count":4,"href":"https:\/\/datarebus.com\/index.php\/wp-json\/wp\/v2\/posts\/842\/revisions"}],"predecessor-version":[{"id":1001,"href":"https:\/\/datarebus.com\/index.php\/wp-json\/wp\/v2\/posts\/842\/revisions\/1001"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/datarebus.com\/index.php\/wp-json\/wp\/v2\/media\/845"}],"wp:attachment":[{"href":"https:\/\/datarebus.com\/index.php\/wp-json\/wp\/v2\/media?parent=842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datarebus.com\/index.php\/wp-json\/wp\/v2\/categories?post=842"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datarebus.com\/index.php\/wp-json\/wp\/v2\/tags?post=842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}