ID 从 1 到 50000,但实际数据只有1W多条..我现在要取出所有的不存在的ID.请问要怎么写SQL语句?

解决方案 »

  1.   

    --臨時表
    declare @t table(ID int)
    declare @i int
    select @i=1
    while @i<=50000
       begin
         insert @t select @i
         set @i=@i+1
       end
    select ID from tb t
    where not exists(select 1 from @t where id=t.id)
      

  2.   

    SELECT TOP 50000 id = IDENTITY(int, 1, 1) INTO # FROM syscolumns a, syscolumns b ,syscolumns c
    select
      id
    from
      tb t
    where
      not exists(select 1 from # where id=t.id)
      

  3.   

    select number,TB.id
    from Master..spt_vlues A
    left join TB on A.number = TB.id
    ID 为null的就是没有的。
      

  4.   

    Master..spt_vlues 只能记录2048条
      

  5.   


    with cte as
    (select row_number() over(order by a.id) num from sysobjects a,sysobjects b,sysobjects c)
    select num from cte a where  num<=50000 and not exists
    (select 1 from tb where num=id )