script to add and remove auto-increment property from a column

quillbreaker picture quillbreaker · Oct 1, 2010 · Viewed 44.7k times · Source

For a sql script I'm working on, I need to programmatically remove the identity, identity seed, and identity increment for a column in an existing table, then add them back to the table at the end of the script. Does anyone have a reference or an example on how to do this?

Answer

Ivan Ferić picture Ivan Ferić · Oct 1, 2010

You should do this:

SET IDENTITY_INSERT <TableName> ON
-- Do the inserting in the table with name <TableName>
SET IDENTITY_INSERT <TableName> OFF

For more details look in the MSDN.