Search This Blog

Sunday, June 16, 2013

How to find missing Identity id from a sql table?

Below is the sql statement to find the missing identity Id from sql table '#tmp1'
'#tmp1' content
IDName
1Arvind
2Kapil
5Mohit
8Rakesh


SQL Statement
With missing as
(
Select 1 as mins, max(Id) as maxs from #tmp1
UNION ALL
Select mins + 1 , maxs from missing m
--Inner join #tmp1 t ON t.ID = m.mins
where mins < maxs
)
Select * from missing m
left outer join #tmp1 t ON t.ID = m.mins
where t.Id is null

Missing Identity

IDName
3Raj
4Mangesh
6Rajesh
7Ravindra

No comments:

Post a Comment