Get all missing values between two limits in sql table column -


so trying assign id numbers records being inserted sql server 2005 database table. since these records can deleted, these records assigned first available id in table. example, if have table below, next record entered @ id 4 first available.

    | id | data |     | 1  | ...  |     | 2  | ...  |     | 3  | ...  |     | 5  | ...  | 

the way prefer done build list of available id's via sql query. there, can checks within code of application.

so, in summary, sql query retrieves available id's between 1 , 99999 specific table column.

first build table of n ids.

declare @allpossibleids table (id integer) declare @currentid integer  select @currentid = 1 while @currentid < 1000000 begin     insert @allpossibleids     select @currentid      select @currentid = @currentid+1 end 

then, left join table real table. can select min if want, or limit allpossibleids less max table id

select a.id  @allpossibleids   left outer join yourtable t on a.id = t.id t.id null  

Comments

Popular posts from this blog

php - get table cell data from and place a copy in another table -

javascript - Mootools wait with Fx.Morph start -

php - Navigate throught databse rows -