--创建测试环境 create table A ( ID int )--插入测试数据 declare @i int set @i=1 while @i<10000 begin insert A select @i set @i=@i+2 end--创建聚集索引 create clustered index index_ID on A(ID)--测试 declare @min int, --要查询的ID最小值 @max int, --要查询的ID最大值 @tmp int select @min=10,@max=20declare @tb table(ID int primary key) --生成表变量(用时:0.0167) set @tmp=@min while @tmp<=@max begin insert @tb select @tmp set @tmp=@tmp+1 end--查询(用时.00664+0.0376) select * from @tb where ID not in(select ID from A where ID between @min and @max)--结果 /* ID ----------- 10 12 14 16 18 20(6 row(s) affected) */
請問樓主,還有另一個表嗎?還是隻有一個表呢。
從一個沒有符合條件的數據表中,是查不出任何數據的。
create table A
(
ID int
)--插入测试数据
declare @i int
set @i=1
while @i<10000
begin
insert A select @i
set @i=@i+2
end--创建聚集索引
create clustered index index_ID on A(ID)--测试
declare @min int, --要查询的ID最小值
@max int, --要查询的ID最大值
@tmp int
select @min=10,@max=20declare @tb table(ID int primary key) --生成表变量(用时:0.0167)
set @tmp=@min
while @tmp<=@max
begin
insert @tb select @tmp
set @tmp=@tmp+1
end--查询(用时.00664+0.0376)
select * from @tb where ID not in(select ID from A where ID between @min and @max)--结果
/*
ID
-----------
10
12
14
16
18
20(6 row(s) affected)
*/