Created
November 3, 2024 20:31
-
-
Save luisdelatorre012/194f4ec193e451474c93a4d26cc5418f to your computer and use it in GitHub Desktop.
missing_indices.sql
This file contains hidden or 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
| -- 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