如何 使用指令碼將某錶的自增列修改為非自增列?

2022-01-22 12:39:56 字數 1040 閱讀 9312

--建立測試表

[create] table t1(id int identity,a int)

go

--插入記錄

insert t1 values(1)

go

--1. 將identity(標識)列變為普通列

[alter] table t1 add id_temp int

go

update t1 set id_temp=id

[alter] table t1 drop column id

exec sp_rename n't1.id_temp',n'id',n'column'

insert t1 values(100,9)

go

--2. 將普通列變為標識列

[create] table t1_temp(id int,a int identity)

set identity_insert t1_temp on

insert t1_temp(id,a) select * from t1

set identity_insert t1_temp off

drop table t1

go

exec sp_rename n't1_temp',n't1'

insert t1 values(109999)

go

--顯示處理結果

select * from t1

/*--結果:

id a

----------------- -----------

1 1

100 9

109999 10

--*/

我把create 和alter打上才能傳資料,你們測試要去掉才行!

重置SQLSERVER表的自增列,讓自增列重新計數

sql的自增列挺好用,只是開發過程中一旦刪除資料,標識列就不連續了 寫起來 也很鬱悶,所以查閱了一下標識列重置的方法 發現可以分為三種 刪除原表資料,並重置自增列 truncate table tablename truncate方式也可以重置自增字段 重置表的自增欄位,保留資料 dbcc chec...

重置SQLSERVER表的自增列,讓自增列重新計數

sql的自增列挺好用,只是開發過程中一旦刪除資料,標識列就不連續了 寫起來 也很鬱悶,所以查閱了一下標識列重置的方法 發現可以分為三種 刪除原表資料,並重置自增列 truncate table tablename truncate方式也可以重置自增字段 重置表的自增欄位,保留資料 dbcc chec...

重置SQLSERVER表的自增列,讓自增列重新計數

sql的自增列挺好用,只是開發過程中一旦刪除資料,標識列就不連續了 寫起來 也很鬱悶,所以查閱了一下標識列重置的方法 發現可以分為三種 刪除原表資料,並重置自增列 truncate table tablename truncate方式也可以重置自增字段 重置表的自增欄位,保留資料 dbcc chec...