22,210
社区成员
发帖
与我相关
我的任务
分享
--创建测试表
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
--*/
先set identity_insert tableName on
再使用更新语句
再set identity_insert tableName off
--把自增关掉
set identity_insert 表名 on
update ....
--然后关掉
set identity_insert 表名 off
--1、
create table tablename(id int identity(1,1))
go
insert into tablename default values
go
select * from tablename
go
drop table tablename
go
--2、
set identity_insert tablename on
insert tablename(id) select 1
set identity_insert tablename off
set identity_insert tableName on
update ...
set identity_insert tableName off