简体   繁体   中英

Change Increment value for Identity - SQL Server 2005

I would like to change increment value of IDENTITY column in database and I have below restrictions:

  1. Can't drop the column.
  2. Having around 20k rows.

Dropping table and recreate table with changed increment value would be the solution. I don't know the syntax.

Can you please suggest the syntax or other best possible solution?

Thanks in Advance!

If i understand you correctly base on your response to my comment, you backed up the data of the orig table in temp table, then you deleted the orig table and you want to recreate an orig table.

If that is the case , you need the IDENTITY_INSERT to set ON and OFF , because the identity of the table is unique.

The syntax is:

SET IDENTITY_INSERT [TableName] ON -- set to on
-- Put your insert statement here
-- insert the data from backed up temp table to your new table
SET IDENTITY_INSERT [TableName] OFF -- set to off

If you can accept recreating table, there is no magic about the recreating table syntax.

 CREATE TABLE temp_Table
 (
      -- Identity column with new settings
      -- other columns
 );

 INSERT INTO temp_Table
 SELECT -- Columns except identity column
 FROM old_table;

 DROP TABLE old_Table;

 EXEC sp_rename 'temp_Table', 'old_Table';

However, you have to handle foreign key by yourself.

Altering identity column after table creation is not possible.

Instead, reset SEED value using the below command.

DBCC CHECKIDENT('tablename', RESEED, 15)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM