34,590
社区成员
发帖
与我相关
我的任务
分享
--假设表tb的col列有这些数据
select b.col
(select col from tb order by col) a
inner join
(select col from tb order by col) b
on a.col=b.col
where a.col<>(b.col-1)
create table tb(id int)
insert into tb select 2 union all select 3 union all select 4 union all select 5
union all select 9union all select 10 union all select 13 union all select 14 union all select 20
go
select * from(
select n=(select count(*) from(select distinct id from sysobjects)t1 where id<t2.id)+1
from (select distinct id from sysobjects)t2
)t3 where not exists(select 1 from tb where id=t3.n)
and n<(select max(id) from tb)
go
drop table tb
/*
n
-----------
1
6
7
8
11
12
15
16
17
18
19
(11 行受影响)
*/
--假设表tb的col列有这些数据
select a.*,b.*
from
(select top 100 percent col from tb order by col) a
inner join
(select top 100 percent col from tb order by col) b
on a.col+1=b.col
create table tb(col int)
insert tb
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 9 union all
select 10 union all
select 13 union all
select 14 union all
select 20
select number
from master..spt_values
where type='p'
and number>=1
and number<=(select max(col) from tb)
and number not in (select col from tb)
number
-----------
1
6
7
8
11
12
15
16
17
18
19
(所影响的行数为 11 行)
create table tb(col int)
insert tb
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 9 union all
select 10 union all
select 13 union all
select 14 union all
select 20
select * from tb
where col<>(select top 1 col from tb order by col)
and col not in
(select b.col from
tb a,tb b
where a.col+1=b.col)
create table tb(col int)
create table #TestTable
(id int,[name] nvarchar(10))
insert #TestTable
select 2 ,'bv' union all
select 3 ,'asd' union all
select 5 ,'bv' union all
select 6 ,'hgfd' union all
select 7 ,'5423' union all
select 11 ,'rew' union all
select 13 ,'er' union all
select 17 ,'t' union all
select 18 ,'tdf'
select top 9999 identity(int,1,1) as id into #t from sys.objects,sys.columns
select t1.id as '不存在' from #t as t1
left join #TestTable t2 on t1.id=t2.id
where t2.id is null and t1.id<(select max(id) from #TestTable)
drop table #T
不存在
-----------
1
4
8
9
10
12
14
15
16
(9 row(s) affected)