Skip to content

Instantly share code, notes, and snippets.

@luisdelatorre012
Created November 3, 2024 20:31
Show Gist options
  • Save luisdelatorre012/194f4ec193e451474c93a4d26cc5418f to your computer and use it in GitHub Desktop.
Save luisdelatorre012/194f4ec193e451474c93a4d26cc5418f to your computer and use it in GitHub Desktop.
missing_indices.sql
-- Assuming table t has one row with lower_bound and upper_bound
WITH Numbers AS (
-- Anchor member: start with the lower bound
SELECT lower_bound AS number
FROM t
UNION ALL
-- Recursive member: increment the number by 1 each time
SELECT number + 1
FROM Numbers
WHERE number + 1 <= (SELECT upper_bound FROM t)
)
-- Select numbers not present in the index column
SELECT number
FROM Numbers
WHERE number NOT IN (SELECT [index] FROM t)
OPTION (MAXRECURSION 0); -- Allows the CTE to recurse beyond the default 100 levels
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment