Finding the intervals
Follow two efficient solutions, obtained in the article Solving Gaps and Islands with Enhanced Window Functions.
The first solution caters to various versions of SQL Server.
-- código #1
SELECT col1 + 1 AS rangestart,
(SELECT MIN(B.col1)
FROM dbo.T1 AS B
WHERE B.col1 > A.col1) - 1 AS rangeend
FROM dbo.T1 AS A
WHERE NOT EXISTS (SELECT *
FROM dbo.T1 AS B
WHERE B.col1 = A.col1 + 1)
AND col1 < (SELECT MAX(col1) FROM dbo.T1);
In code #1, replace col1
the name of the column containing the numbering and T1
by table name.
There is another suggestion, even more efficient, that works from the 2012 version (inclusive) of SQL Server. Uses the window Function LEAD().
-- código #2
WITH C AS (
SELECT col1 AS cur, LEAD(col1) OVER(ORDER BY col1) AS nxt
FROM dbo.T1
)
SELECT cur + 1 AS rangestart, nxt - 1 AS rangeend
FROM C
WHERE nxt - cur > 1;
In code #2, replace col1
the name of the column containing the numbering and T1
by table name.
What is the cause of the intervals?
There could be multiple motives. For small intervals it is necessary to look for the cause in the application and in the accesses made directly to the table by the users. For larger ranges (usually multiples of 1000), one possibility is that the cause is directly linked to how the IDENTITY is implemented in SQL Server. The documentation itself reads "SQL Server Might cache Identity values for performance reasons and some of the Assigned values can be Lost During a database Failure or server Restart. This can result in gaps in the Identity value upon Insert”.
Attention to the excerpt "This can result in gaps"!
As a solution, still in the same documentation is quoted that "If gaps are not acceptable then the application should use its Own Mechanism to generate key values". That is, it is a fact that IDENTITY is unreliable to generate consecutive numerical sequences without intervals.
Deepening on the theme gaps and Islands
For those interested in knowing more about the classic problem of gaps and Islands (ranges and islands), here are some selected articles:
Which
database
are using?– Marcelo de Andrade
SQL Server. I changed the title.
– vinibrsl
Just out of curiosity, what would be the reason for that?
– Jeferson Almeida
You can attach the
DDL
of this table?– Marcelo de Andrade
In the logic of my application, the user cannot delete lines, only inactivate them. Some users have deleted lines manually. Another reason would be to identify some banks that are corrupted, that have these gaps for some reason.
– vinibrsl
Just do not delete table values
– Renato Junior
Does this refer to sorting a SELECT? Is it in a query? Or is it saying that it is appearing like this in some manager software? Can you explain exactly where you’re showing up like this?
– Guilherme Nascimento
Does not refer to ordering. Missing lines have been deleted or the database has been corrupted.
– vinibrsl
@vnbrs is hard to understand, in question you added
...
, I assumed there were more things, now if it gets corrupted then the problem is quite different. This is when you do a SELECT, right? So just try this:SELECT id FROM minha_tabela ORDER BY id;
and say whether "loser" appears or not.– Guilherme Nascimento
I exemplified in the question. I have tables with more than 100 thousand records, it is difficult to identify in the eye :P
– vinibrsl
So what you mean is that the "holes do exist" in fact, and you just want to identify which Ids are left without registration? Or do you want to know why these Ids supposedly disappeared?
– Guilherme Nascimento
I want to know what ID the gap started. The question is not why these Ids have disappeared.
– vinibrsl
@vnbrs yes, now it has begun to clear, is that you said, "should not", it leads to a totally different understanding. By your last comment what you want to know is in which the "first" ID where began these "holes", at least that’s what led to understand.
– Guilherme Nascimento
Just call the table "asphalt".
– Oralista de Sistemas