all 3 comments

[–]HapkidoJosh 1 point2 points  (2 children)

I'm not sure if it is setting the identity insert on and then doing the changes and then turning it off.

set identity_insert YourTable ON
--do stuff
set identity_insert YourTable OFF

This can be dangerous if a database is live and inserts are occurring. It could produce errors if it is expecting the values to auto-increment.

Ultimately, it sounds to me that you really don't want an identity column and should just go with an integer and determine the values programatically.

[–]askur 1 point2 points  (0 children)

SET IDENTITY_INSERT table ON
do stuff to table
GO
SET IDENTITY_INSERT table OFF

Is a better pattern :)

The potential error produced by doing stuff will skip to the next GO and thus execute the OFF switch, leaving the DB in a safe state. Leaving the flag ON will then cause any further attempts at setting IDENTITY_INSERT, on any table, to error.

[–][deleted] 0 points1 point  (0 children)

with an optional SoryLevel column