MSSQL Turn off Identity and Constraint Check

When inserting raw data into a Microsoft SQL database you may need to turn off the primary key check and any other constraints just so you can get your data into the system. The following commands will achieve this.

SET IDENTITY_INSERT [TableName] ON;
ALTER TABLE [TableName] NOCHECK CONSTRAINT all;

To restore all checks back to the way they were

SET IDENTITY_INSERT [TableName] OFF;
ALTER TABLE [TableName] CHECK CONSTRAINT all;

Tags: , , ,

Leave a Reply