簡體   English   中英

使用T-SQL腳本重置標識列的INCREMENT VALUE

[英]Reset INCREMENT VALUE For Identity Column using T-SQL Script

我需要更改身份列的增量值

例如,我創建了帶有標識列的表測試表

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

現在很容易使用重新設置Identity Column的起始值

DBCC CheckIdent('TEST',Reseed,100)

但我想將Increment值1更改為10是否有任何sql命令可以工作..

從SSMS 2016更改時,我收到此錯誤

在此輸入圖像描述

要更改增量,需要刪除現有的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM