2017-05-16 36 views
1

我需要更改增量價值標識列復位增量值對於使用T-SQL腳本標識列

對於實例 我已創建表測試表與標識列

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

現在很容易使用

​​

補種標識列的起始值但我想向增加值更改1至10 有任何SQL命令,將工作..

雖然從SSMS 2016改變我得到這個錯誤

enter image description here

+0

相信通過'SSMS'的'UI',設計表,增加標識列增加值到你想要的。保存表格。 –

+0

@WEI_DBA:我嘗試​​過但它不適用於我.. :( – Dhaval

+0

我剛纔那樣做了,它對我有效。 –

回答

0

要改變,你需要刪除現有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 |  | 
+-----+------+ 
+0

Thanks for Replying @ sqlzim但我不想刪除和重新創建表。是否有任何替代此 – Dhaval

+1

@Dhaval不是我所知道的。建議使用SSMS表設計器作爲WEI_DBA建議生成代碼,以便刪除並重新創建此操作的表。 – SqlZim