Skip to content

Instantly share code, notes, and snippets.

@karenpayneoregon
Created April 4, 2024 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karenpayneoregon/6a002be0f373916911e5a8fef9b53abf to your computer and use it in GitHub Desktop.
Save karenpayneoregon/6a002be0f373916911e5a8fef9b53abf to your computer and use it in GitHub Desktop.
Useful if there is a need to find gaps in SQL-Server table index.
DECLARE @BrokenTable TABLE (ID INT NOT NULL);
INSERT INTO @BrokenTable VALUES (1);
INSERT INTO @BrokenTable VALUES (3);
INSERT INTO @BrokenTable VALUES (5);
INSERT INTO @BrokenTable VALUES (7);
INSERT INTO @BrokenTable VALUES (9);
WITH CTE
AS (SELECT 1 AS Number
UNION ALL
SELECT Number + 1
FROM CTE
WHERE Number <= 10)
SELECT TOP (5) *
FROM CTE
WHERE Number NOT IN (SELECT ID FROM @BrokenTable)
ORDER BY Number
OPTION (MAXRECURSION 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment