create table tb(ID int identity(100, 2)) --從100開始, 每次增加2
TO marco08(天道酬勤) : 数据表已经建立,就如您的, 种子 为 100,现需要修改为 200 ,如何写代码 ?
--參考老大的方法--創建測試表 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. 將普通列變為identity列 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 --*/
alter table alter column id identity(200,2) --不能這樣修改的講一下老大的思路 1、新建一列,把identity列的值拷貝到新建的列 2、刪除identity列 3、新建一列identity(200, 1) 4、把數據又拷貝到第三步新建的列裏 5、刪除第一步新建的列
TO hhl1213() : 检查语法通过,运行出错应该指明表名吧 : dbcc CHECKIDENT(id,reseed,199)
create table tb(ID int identity(100, 2), Name char(1)) --從100開始, 每次增加2 insert tb(Name) select 'a' union all select 'b' union all select 'c'select ID=identity(int, 200, 1), Name into tb2 from tb --從200開始, 每次增加1drop table tbsp_rename 'tb2', 'tb'select * from tb
DECLARE @bId bit SET @bId = 0 IF OBJECTPROPERTY ( object_id('TableName'),'TableHasIdentity ') = 1 BEGIN SET @bId = 1 SET IDENTITY_INSERT [TableName] ON END UPdate TableName set ID=NewID where ID=OldIDIF @bId = 1 SET IDENTITY_INSERT [TableName] OFF GO
数据表已经建立,就如您的, 种子 为 100,现需要修改为 200 ,如何写代码 ?
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. 將普通列變為identity列
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
--*/
在查询分析器中运行报错 :
服务器: 消息 156,级别 15,状态 1,行 1
在关键字 'identity' 附近有语法错误。
请示代码 !
--不能這樣修改的講一下老大的思路
1、新建一列,把identity列的值拷貝到新建的列
2、刪除identity列
3、新建一列identity(200, 1)
4、把數據又拷貝到第三步新建的列裏
5、刪除第一步新建的列
检查语法通过,运行出错应该指明表名吧 :
dbcc CHECKIDENT(id,reseed,199)
insert tb(Name) select 'a'
union all select 'b'
union all select 'c'select ID=identity(int, 200, 1), Name into tb2 from tb --從200開始, 每次增加1drop table tbsp_rename 'tb2', 'tb'select * from tb
我用修改系统表autoval的方法试了一下,发现不成功,改动autoval不影响种子,郁闷
DBCC CHECKIDENT (考勤, NORESEED)--报告标识
DBCC CHECKIDENT (考勤, RESEED, 4)--强制标识
强制标识,是设置种子从什么数字开始。不是以4递增
服务器: 消息 156,级别 15,状态 1,行 2
在关键字 'dbcc' 附近有语法错误。
帖主想将种子重置吗?
对啊,请赐教 !
TRUNCATE TABLE 表名
记录还保留吗?如果不保留:
TRUNCATE TABLE 表名
SET @bId = 0
IF OBJECTPROPERTY ( object_id('TableName'),'TableHasIdentity ') = 1
BEGIN
SET @bId = 1
SET IDENTITY_INSERT [TableName] ON
END
UPdate TableName set
ID=NewID
where
ID=OldIDIF @bId = 1
SET IDENTITY_INSERT [TableName] OFF
GO
DBCC CHECKIDENT (表名, RESEED,2000)--设置就2000种子会从2001开始递增
dbcc checkident('考勤')--重置当前标识值 (用于标识值错误)
DBCC CHECKIDENT (考勤, NORESEED)--报告标识(用于查看标识值)