简体   繁体   中英

Reset INCREMENT VALUE For Identity Column using T-SQL Script

I Need Change Increment Value for Identity Column

For Example I have create table Test Table with Identity Column

Create Table test 
(
 Id Int Identity(1,1)
,Name Varchar(200)
)

Now it is easy to Reseed the start value of Identity Column using

DBCC CheckIdent('TEST',Reseed,100)

But I want to change the Increment value 1 to 10 is there any sql command that will work ..

While changing from SSMS 2016 I get this error

在此输入图像描述

To change the increment you need to drop the existing identity() column and add a new column.

alter table test drop column Id;
alter table test add Id int identity(100,10);

If you want to keep existing values, then you will need to create a new table, insert the existing rows with identity_insert on , drop the old table, and rename the new table.

For example:

create table test (id int identity(1,1), name varchar(200) default '')
insert into test default values
insert into test default values

create table new_test (id int identity(100,10), name varchar(200) default '');

set identity_insert new_test on;
  insert into new_test (id,name)
  select id,name from test
set identity_insert new_test off;

drop table test;

exec sp_rename 'new_test','test';

insert into test default values;
insert into test default values;

select * from test;

rextester demo: http://rextester.com/XDE9355

returns:

+-----+------+
| id  | name |
+-----+------+
|   1 |      |
|   2 |      |
| 100 |      |
| 110 |      |
+-----+------+

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