日期:2014-05-17 浏览次数:20441 次
--> 测试数据:[ta]
if object_id('[ta]') is not null drop table [ta]
create table [ta](id int primary key)
insert [ta]
select 3 union all
select 5 union all
select 6 union all
select 9
select * from [ta]
--> 测试数据:生成连续数据表
if object_id('[tb]') is not null drop table [tb]
select identity(int,1,1) as id into tb from sysobjects a,sysobjects b
insert into ta select min(b.id) from tb b where not exists(select 1 from ta where b.id=id)
insert into ta select min(b.id) from tb b where not exists(select 1 from ta where b.id=id)
select * from ta
/*
id
-----------
1
2
3
5
6
9
(6 行受影响)
create table tb(id int , name varchar(10))
insert into tb values(1,'1')
insert into tb values(3,'3')
insert into tb values(6,'6')
insert into tb values(8,'8')
insert into tb values(9,'9')
go
--1
insert into tb
select min(m.id) + 1 ,'第1次' from
(select * , px = (select count(1) from tb where id < t.id) + 1 from tb t) m,
(select * , px = (select count(1) from tb where id < t.id) + 1 from tb t) n
where m.px = n.px - 1 and m.id <> n.id - 1
select * from tb order by id
/*
id name
----------- ----------
1 1
2 &n