我建了1个新表,其中有一列是这么定义的:ID int identity(1,1)  这样的话 id列就能自动递增了。 但是当我删除一行,例如第7行,然后紧接着插入一行时,id列显示的并不是7,而是8, 这是为什么啊?  有什么办法可以让新添列的ID不间断??代码如下:
          
create table t2(ID int identity(1,1),MtlID varchar(10),StkID varchar(10),TimeID datetime,Weight float)insert into t2(MtlID,StkID,TimeID,Weight)
select MtlID,StkID,TimeID,Weight                           --插入内容
from t1
delete from t2 where Weight is nullinsert into t2(MtlID,StkID,TimeID,Weight)
select MtlID,StkID,TimeID,Weight                          -- 重新插入删除行,这时就显示为8 而不是7了
from t1 
where id=7

解决方案 »

  1.   

    -->用视图--2000
    create view vtb_2000 as select new_id=(select count(1) from tb where id<=t.id), * from tb as t
    go--2005
    create view vtb_2005 as select new_id=row_number()over(order by id), * from tb as t
    go
      

  2.   

    有两种方法可供参考:
    1.打开identity的开关,语句set identity_insert t2 on
    这时可以在ID列插入你要的值
    2.在删除某列之前取出ID的最大值,然后删该列,再用dbcc checkident('t2',reseed,取出的ID值),这时下次插入记录时,ID是连续的
      

  3.   

    使用一下sql语句可以重设标示值。
    dbcc checkident('表名', RESEED, 100)
      

  4.   

    参:http://blog.csdn.net/happyflystone/archive/2007/11/25/1901966.aspx
      

  5.   

    code=SQL]alter table t2 drop column ID
    alter table t2 add  ID int identity(1,1)[/code]
      

  6.   

    alter table t2 drop column ID
    alter table t2 add  ID int identity(1,1)
      

  7.   

    从后面删,是可以用dbcc checkident,如果中间删掉几条记录,怎么dbcc checkident?
      

  8.   

    dbcc checkident('表名', RESEED, 100)也行....................或者alter table tab drop column PKID,alter table tab add PKID int identity(1,1)
      

  9.   

    我以前做的项目中是这样弄的,楼主不妨参考下,不管删除哪一行的数据,重新插入的数据将自动填补空的,比如你的identity有1、2、3、4、5、6,删除了3、5,插入的时候先填补3的,再插入的时候填补5的,完了后再插入一条数据就是7的了。create table place(
    place_id smallint identity(1,1),
    place_name varchar(50),
    primary key(place_id))create trigger trig_place
    on place
    instead of insert
    as
    declare
      @place_id smallint,
      @place_name varchar(50),
      @min smallint
    select @min=min(identitycol) 
    from place p1
    where identitycol between ident_seed('place') and 32766 
    and not exists(select * 
    from place p2
    where p2.identitycol=p1.identitycol+ident_incr('place'))
    select @place_id=max(place_id) from place
    select @place_name=place_name from inserted
    if @place_id<>@min
    begin
      set @place_id=@min
    end
    if @place_id is null
    begin
      set @place_id=0;
    end
    set identity_insert place on
    insert into place(place_id,place_name) values(@place_id+1,@place_name)
    set identity_insert place off
    go
      

  10.   


    Identity列如果不连续也没有必要,添加比较麻烦
      

  11.   


    同意当然如果你非要连续的话 可以写个小语句先导出数据重置递增列后再导回来INSERT t2bak SELECT * from t2 order by ID
    TRUNCATE TABLE T_code
    INSERT t2 SELECT * from t2bak order by ID
      

  12.   

    INSERT t2bak SELECT * from t2 order by ID 
    TRUNCATE TABLE t2                   --SORRY 表名搞错鸟
    INSERT t2 SELECT * from t2bak order by ID