Created
April 4, 2024 16:32
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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