日期:2014-05-18  浏览次数:20627 次

SQLServer中的自增变量
将表的ID字段设为自增变量类型
现在插入一条纪录,ID为1,然后将该条纪录删除,再插入,这时显示ID为3
我想让这时插入的ID为2(连续的),可以吗?
谢谢!

------解决方案--------------------
create table #Test(id int identity(1,1), c2 varchar(10))
insert #Test
select '当前标识1 ' union all
select '当前标识2 ' union all
select '当前标识3 '
select * from #Test

delete #Test where id = 3

dbcc checkident( '#Test ', reseed, 2)

insert #Test select '当前标识3 '
select * from #Test
drop table #Test

------解决方案--------------------
如果你的數據較少的話,有一個很簡單的方法


create trigger aa on t
after delete
as
alter table t
drop column id

alter table t
add id int identity(1,1)
------解决方案--------------------

set identity_insert on 语句
不过要人工插入,
如果要自动插入,不用identity列,在插入触发器中实现