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

2

26

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?

Ob answered 1/10, 2010 at 17:30 Comment(2)
Are you asking this because you need to insert some specific values into an identity column?Oleta
Yes, ported in from another database over a linked server.Ob
E
45

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.

Excellent answered 1/10, 2010 at 17:37 Comment(2)
But where we added column_name on which we want to set identity ? @Ivan FerićFilberto
You don't need to specify any column name - this will enable identity insert on the tableAmmonal
D
8

Yes, you just do this:

SET IDENTITY_INSERT [TABLE] ON

And then back on:

SET IDENTITY_INSERT [TABLE] OFF

This will allow you to enter manual data in the identity column.

http://msdn.microsoft.com/en-us/library/ms188059.aspx

Danettedaney answered 1/10, 2010 at 17:35 Comment(3)
You should first set it to ON, and in the end to OFF. With ON it signalizes that you are about to do manual inserts on identity column.Ammonal
Thanks, a little Friday morning aixelsyd. :)Danettedaney
You still have it the other way around.Ammonal

© 2022 - 2024 — McMap. All rights reserved.