--实现代码如下: --创建测试表 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
--改一点:--创建测试表 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
update 表名 set id=id+1 where id >=173然后追加id=173的记录就可以了。
如果楼主要保证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
如果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
--创建测试表
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
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
set xact_abort on --使用事务
begin tranupdate 表名 set id=id+1 where id >=173--然后追加id=173的记录就可以了。insert into 表名 (id,TDateTime) ...commit tran
set xact_abort on --使用事务
begin tranupdate 表名 set id=id+1 where id >=173--然后追加id=173的记录就可以了。insert into 表名 (id,TDateTime) ...commit tran
程序或其他代码中往这个表中追加数据时,需要用Max(ID)+1来处理。
但实际情况在更改了这个表的ID字段属性为非自动增长的时候到更改回自动增长的期间,是不会有其他程序有插入操作的.我担心的问题,数据量很多的话,更改ID字段属性为非自动后再改回来自动增长,会不会要很久的时间?