陋室铭
永远也不要停下学习的脚步(大道至简至易)

 

 

表A

-- 创建结果表
create table #u(LostA int)

declare @minA int,@maxA int
 
set @minA=(select min(ID) from A)
set @maxA=(select max(ID) from A)
  
while(@minA<=@maxA)
begin
  if not exists(select 1 from Awhere ID=@minA)
  begin
    insert into #u(LostA) values(@minA)
  end
  select @minA=@minA+1
end
select * from #u

 或者

 

declare @minA int,@maxA int;
set @minA=(select min(ID) from A);
set @maxA=(select max(ID) from A);

WITH A AS
(
  SELECT @minA AS num
  UNION ALL
  SELECT num+1 FROM A
  WHERE num<@maxA
)
SELECT num FROM A where A.num not in (select ID from A)
OPTION(MAXRECURSION 0) --当指定MAXRECURSION为0时,递归层次无限制,100为系统的默认值

 

posted on 2018-11-08 16:01  宏宇  阅读(560)  评论(0编辑  收藏  举报