begin tran create table tb(id int,name varchar(10)) insert into tb select 1,'a' union all select 2,'b' union all select 6,'f' union all select 7,'g' union all select 8,'h'--显示旧表数据 select * from tb--插入新数据 insert into tb select isnull(min(id),0)+1,'新增1' from tb a where not exists ( select 1 from tb b where id=a.id+1 )insert into tb select isnull(min(id),0)+1,'新增2' from tb a where not exists ( select 1 from tb b where id=a.id+1 )insert into tb select isnull(min(id),0)+1,'新增3' from tb a where not exists ( select 1 from tb b where id=a.id+1 )insert into tb select isnull(min(id),0)+1,'新增4' from tb a where not exists ( select 1 from tb b where id=a.id+1 )--显示新增后的结果 select * from tb order by id rollback tran
--上面的有些错误,如果id=1的记录不存在,不会补充上1 --下面的这个是没问题的:begin tran --创建数据测试环境--表 create table tb(id int,name varchar(10))--为表新增100条记录,如果要测试更多记录,将第一个top 100中的100改为你要的记录数 select top 100 id=identity(int,1,1) into #temp from (select top 100 id from syscolumns) a ,(select top 100 id from syscolumns) b ,(select top 100 id from syscolumns) cinsert into tb select id,cast(id as varchar) from #tempdrop table #temp --随机删除5条记录,如果要改变删除的记录数,将top 5改为你要的记录数 delete from tb where id in(select top 5 id from tb order by newid())--显示删除记录后的结果 select * from tb--新增10条记录,你可以新增更多的记录进行测试,只要修改@i<10中的10为你要新增的记录数 declare @i int set @i=1 while @i<=10 begin insert into tb select case when exists(select 1 from tb where id=1) then (select isnull(min(id)+1,1) from tb a where id not in(select a.id from tb a,tb b where a.id+1=b.id)) else 1 end,'新增值'+cast(@i as varchar) set @i=@i+1 end--显示新增后的结果 select * from tb order by idrollback tran
请问邹建: where not exists ( select 1 from tb b where id=a.id+1 ) 上面这句话是什么意思?它是用来作什么用的?id字段的空缺部分是不确定的,空缺部分的个数也是不确定的,且id字段可能已经被添加了很多值!如果用户一开始删除的id值是5000,5001,5002那要怎样判断?怎样写限制条件?多谢!
1. where not exists ( select 1 from tb b where id=a.id+1 )就是用来判断id是否连续的. 判断的原理是每条记录的下一条的id是否为上条记录的id+1,如果不是,就表明id不连续,就将它提取出来.并将它的id+1做为新增记录的id2. 如果用户一开始删除的id值是5000,5001,5002那要怎样判断?怎样写限制条件?上面的取值方法会自动根据表中的删除情况选择最小一个不连续的ID+1做为新增记录的id
create table tb(id int,name varchar(10))
insert into tb
select 1,'a'
union all select 2,'b'
union all select 6,'f'
union all select 7,'g'
union all select 8,'h'--显示旧表数据
select * from tb--插入新数据
insert into tb
select isnull(min(id),0)+1,'新增1' from tb a
where not exists (
select 1 from tb b
where id=a.id+1
)insert into tb
select isnull(min(id),0)+1,'新增2' from tb a
where not exists (
select 1 from tb b
where id=a.id+1
)insert into tb
select isnull(min(id),0)+1,'新增3' from tb a
where not exists (
select 1 from tb b
where id=a.id+1
)insert into tb
select isnull(min(id),0)+1,'新增4' from tb a
where not exists (
select 1 from tb b
where id=a.id+1
)--显示新增后的结果
select * from tb order by id
rollback tran
--下面的这个是没问题的:begin tran
--创建数据测试环境--表
create table tb(id int,name varchar(10))--为表新增100条记录,如果要测试更多记录,将第一个top 100中的100改为你要的记录数
select top 100 id=identity(int,1,1) into #temp
from (select top 100 id from syscolumns) a
,(select top 100 id from syscolumns) b
,(select top 100 id from syscolumns) cinsert into tb
select id,cast(id as varchar) from #tempdrop table #temp
--随机删除5条记录,如果要改变删除的记录数,将top 5改为你要的记录数
delete from tb where id in(select top 5 id from tb order by newid())--显示删除记录后的结果
select * from tb--新增10条记录,你可以新增更多的记录进行测试,只要修改@i<10中的10为你要新增的记录数
declare @i int
set @i=1
while @i<=10
begin
insert into tb
select case when exists(select 1 from tb where id=1) then
(select isnull(min(id)+1,1) from tb a
where id not in(select a.id from tb a,tb b where a.id+1=b.id))
else 1 end,'新增值'+cast(@i as varchar)
set @i=@i+1
end--显示新增后的结果
select * from tb order by idrollback tran
将:
create table tb(id int,name varchar(10))
改为:
create table tb(id int primary key ,name varchar(10))不然速度会很慢
where not exists (
select 1 from tb b
where id=a.id+1
)
上面这句话是什么意思?它是用来作什么用的?id字段的空缺部分是不确定的,空缺部分的个数也是不确定的,且id字段可能已经被添加了很多值!如果用户一开始删除的id值是5000,5001,5002那要怎样判断?怎样写限制条件?多谢!
where not exists (
select 1 from tb b
where id=a.id+1
)就是用来判断id是否连续的.
判断的原理是每条记录的下一条的id是否为上条记录的id+1,如果不是,就表明id不连续,就将它提取出来.并将它的id+1做为新增记录的id2.
如果用户一开始删除的id值是5000,5001,5002那要怎样判断?怎样写限制条件?上面的取值方法会自动根据表中的删除情况选择最小一个不连续的ID+1做为新增记录的id