Let us assume that there is an Employee table with EmployeeId as the Identity column in which some values are missing. The following batch will show all the missing values in that Identity column. The logic I preferred is to create another table with the same increment as that of the parent table but without any gaps. Finally, subtracting the parent table rows from the newly created table gives us the missed values. Rest of the script is pretty straight forward and self explanatory.
Declare
select
SET
EXEC
while
begin
end
Select
Except
Drop
The above script can be made reusable by making it a stored procedure using Dynamic Sql as shown below. It just accepts table name as a parameter and does the rest of the job.
CREATE
AS
BEGIN
END
Hope this helps.