I recently participated in a webinar, and one guy asked me about the possible approaches to disabling the identity column in SQL Server. Let me summarize the information for you.
To turn off the identity column in SQL, you must set the IDENTITY_INSERT property to OFF.
Let’s look at the complete steps with an example to clarify how to do this and the effect after disabling this option.
Suppose we have a table called Orders with an identity column named OrderID. The screenshot below shows this.
Now, we can turn off the identity column using the below query. Check out the screenshot below for your reference.
Check out: How To Insert Values Into Table With Identity Column In SQL Server
SET IDENTITY_INSERT Orders OFF;
After I turned off the identity column, I encountered the error below when inserting a record and explicit value for the identity column. Check out the screenshot below for your reference.
INSERT INTO Orders (OrderID, OrderName, OrderType) VALUES (4,'finance', 'GST');
“Cannot insert explicit value for identity column in table ‘Orders’ when IDENTITY_INSERT is set to OFF.”
Conclusion
The identity column is turned off by default in SQL Server. Still, if you have turned it on for any of your requirements, it is suggested that you turn it off by setting the IDENTITY_INSERT Orders to OFF using the information mentioned in this article.
You may also like following the articles below.
- How to reset identity column value in SQL Server
- How to check identity column in SQL Server
- How To Turn Off Identity Column In SQL Server
Grey is a highly experienced and certified database expert with over 15 years of hands-on experience in designing, implementing, and managing complex database systems. Currently employed at WEX, USA, Grey has established a reputation as a go-to resource for all things related to database management, particularly in Microsoft SQL Server and Oracle environments. He is a Certified Microsoft SQL Server Professional (MCSE: Data Management and Analytics) and Oracle Certified Professional (OCP), with Extensive database performance tuning and optimization knowledge and a proven track record in designing and implementing high-availability database solutions.