alter table Test drop column ID alter table Test add column ID int identity(1,1)
alter table test drop column id alter table test add id(int identity (1,1))
declare @n int set @n=0update tableb set @n=@n+1,qty=@n
--参考 --创建测试表 CREATE TABLE t1(ID int IDENTITY,A int) GO --插入记录 INSERT t1 VALUES(1) GO--1. 将IDENTITY(标识)列变为普通列 ALTER TABLE t1 ADD ID_temp int GOUPDATE 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 GOEXEC sp_rename N't1_temp',N't1' INSERT t1 VALUES(109999) GO--显示处理结果 SELECT * FROM t1 /*--结果: ID A ----------------- ----------- 1 1 100 9 109999 10 --*/
declare @n int set @n=0update tableb set @n=@n+1,qty=@n支持这个方法!
1樓語法有誤 alter table Test add column ID int identity(1,1)--紅色去掉只用用刪除再新增列,如果要不改變ID列在表的排序,可用企業管理器調整,或新建一個新表再導入
多了个括号 alter table test drop column id alter table test add id int identity (1,1)
alter table Test add column ID int identity(1,1) --这个纠正一下
alter table Test add column ID int identity(1,1)
declare @n int
set @n=0update tableb
set @n=@n+1,qty=@n
--创建测试表
CREATE TABLE t1(ID int IDENTITY,A int)
GO
--插入记录
INSERT t1 VALUES(1)
GO--1. 将IDENTITY(标识)列变为普通列
ALTER TABLE t1 ADD ID_temp int
GOUPDATE 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
GOEXEC sp_rename N't1_temp',N't1'
INSERT t1 VALUES(109999)
GO--显示处理结果
SELECT * FROM t1
/*--结果:
ID A
----------------- -----------
1 1
100 9
109999 10
--*/
set @n=0update tableb
set @n=@n+1,qty=@n支持这个方法!
alter table Test add column ID int identity(1,1)--紅色去掉只用用刪除再新增列,如果要不改變ID列在表的排序,可用企業管理器調整,或新建一個新表再導入
alter table test drop column id alter table test add id int identity (1,1)