[英]Reset INCREMENT VALUE For Identity Column using T-SQL Script
要更改增量,需要刪除現有的identity()
列並添加新列。
alter table test drop column Id;
alter table test add Id int identity(100,10);
如果要保留現有值,則需要創建新表,插入帶有identity_insert on
的現有行,刪除舊表,然后重命名新表。
例如:
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演示: http ://rextester.com/XDE9355
收益:
+-----+------+
| id | name |
+-----+------+
| 1 | |
| 2 | |
| 100 | |
| 110 | |
+-----+------+
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.