可以。用set identity_insert 表名 onset identity_insert 表名 off

解决方案 »

  1.   

    --实现代码如下:
    --创建测试表
    create table tb (ID int IDENTITY (1, 1) not null primary key ,TDateTime datetime null)
    GOset identity_insert tb on--追加测试数据
    insert into tb(id,TDateTime)
    select 171,'2006-9-23 18:06:56' union all
    select 172,'2006-9-23 18:07:24' union all
    select 173,'2006-9-23 18:07:32' union all
    select 174,'2006-9-23 18:07:45' set identity_insert tb off
    --将ID>172的追加到临时表中
    create table #tp(ID int ,TDateTime datetime)insert into #tp(id,TDateTime) select id,TDateTime from tb where id>=172
    go
    delete tb where id>=172set identity_insert tb oninsert into tb(id,TDateTime) select 172,'2006-9-23 18:07:25'--更新#tp中的ID=ID+1
    update #tp set ID=ID+1insert into tb(id,TDateTime) select * from #tp--追加回原表set identity_insert tb offselect * from tb--删除测试表
    drop table tb,#tp
      

  2.   

    --改一点:--创建测试表
    create table 表名 (ID int IDENTITY (1, 1) not null primary key ,TDateTime datetime null)
    GOset identity_insert 表名 on--追加测试数据
    insert into 表名(id,TDateTime)
    select 171,'2006-9-23 18:06:56' union all
    select 172,'2006-9-23 18:07:24' union all
    select 173,'2006-9-23 18:07:32' union all
    select 174,'2006-9-23 18:07:45' set identity_insert 表名 off
    --将ID>172的追加到临时表中
    create table #tp(ID int ,TDateTime datetime)
    insert into #tp(id,TDateTime) select id,TDateTime from 表名 where id>172delete 表名 where id>172set identity_insert 表名 oninsert into 表名(id,TDateTime) select 173,'2006-9-23 18:07:25'--更新#tp中的ID=ID+1
    update #tp set ID=ID+1--追加回原表
    insert into 表名(id,TDateTime) select * from #tp
    set identity_insert 表名 offselect * from 表名--删除测试表
    drop table 表名,#tp
      

  3.   

    对于自动编号字段,即使设置set identity_insert 表名 on,也不能直接update,需要借助临时表实现。数据量大的时候,当然不适合这么做。
      

  4.   

    update 表名 set id=id+1 where id >=173然后追加id=173的记录就可以了。
      

  5.   

    如果楼主要保证ID可以插入-->如果楼主要保证ID连续,而且中间可以插入
      

  6.   

    --指定当 Transact-SQL 语句产生运行时错误时,Microsoft® SQL Server是否自动回滚当前事务。
    set xact_abort on --使用事务
    begin tranupdate 表名 set id=id+1 where id >=173--然后追加id=173的记录就可以了。insert into  表名 (id,TDateTime)   ...commit tran
      

  7.   

    首先,原来的表中id是自动编号类型的,如果需要保证ID连续,而且中间可以插入,则,把ID的数据类型改为非自动编号,处理起来更简单。但这样又带来一个问题,程序或其他代码中往这个表中追加数据时,需要用Max(ID)+1来处理。所以,要根据你的具体情况处理。
      

  8.   

    如果ID不是自动编号类型的字段,则中间插一条记录的ID,处理起来要简单 多了,如:--指定当 Transact-SQL 语句产生运行时错误时,Microsoft® SQL Server是否自动回滚当前事务。
    set xact_abort on --使用事务
    begin tranupdate 表名 set id=id+1 where id >=173--然后追加id=173的记录就可以了。insert into  表名 (id,TDateTime)   ...commit tran
      

  9.   

    wangtiecheng说的不错,
    程序或其他代码中往这个表中追加数据时,需要用Max(ID)+1来处理。
    但实际情况在更改了这个表的ID字段属性为非自动增长的时候到更改回自动增长的期间,是不会有其他程序有插入操作的.我担心的问题,数据量很多的话,更改ID字段属性为非自动后再改回来自动增长,会不会要很久的时间?