How to find “holes” in a table

后端 未结 10 2196
野趣味
野趣味 2020-12-24 04:28

I recently inherited a database on which one of the tables has the primary key composed of encoded values (Part1*1000 + Part2).
I normalized that column, but I cannot ch

10条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-24 05:00

    The best way is building a temp table with all IDs

    Than make a left join.

    declare @maxId int
    select @maxId = max(YOUR_COLUMN_ID) from YOUR_TABLE_HERE
    
    
    declare @t table (id int)
    
    declare @i int
    set @i = 1
    
    while @i <= @maxId
    begin
        insert into @t values (@i)
        set @i = @i +1
    end
    
    select t.id
    from @t t
    left join YOUR_TABLE_HERE x on x.YOUR_COLUMN_ID = t.id
    where x.YOUR_COLUMN_ID is null
    

提交回复
热议问题