日期:2014-05-17  浏览次数:21850 次

无法更新标识列 'id'
经常会遇到这样的问题,当一条或多条记录被删除后,id字段就会出现不连续
今天做了个测试,就是想删除数据后,手动更新id,但是最后更新id时出现无法更新标示符的错误提示,还请高手解答。内容如下:

if OBJECT_ID('student','u') is not null drop table student
create table student(
id int not null identity(1,1) primary key,
name varchar(20) not null,
temp int not null
)
go
set identity_insert student on
go
insert into student(id,name,temp) select 1,'张三',111
union all select 2,'李四',222
union all select 3,'王二',333
union all select 4,'麻子',444
select * from student
go
/*删除id为1的记录*/
delete from student where id='1'
select * from student
go
/*把id为1的字段更新为2*/
update student 
set id=1
where id=2
set identity_insert student off
select  * from student

标示符、sql

------解决方案--------------------
标识列不可更新,你要想更新就不要使用标识列,改用触发器或者存储过程实现id的递增
------解决方案--------------------
只能删了再插入,不可以直接更新
------解决方案--------------------
使用 DBCC checkident(表名,reseed,1) 就可以重置啦


------解决方案--------------------
迂回一下,update改为insert和delete

insert into student(id,name,temp) select 1,name,temp from student where id=2
delete from student where id='2'
------解决方案--------------------
无法直接更新标识列,要么你重置标识列,要么另外设置一列,通过代码来保证新列的顺序。
------解决方案--------------------
无法直接更新标识列