I saw the range table creation script and think that we can create it with another twist by utilizing Izik-Ben-Gan "Sequential Number table function" without using loop.
Result expected
RangeStart RangeEnd
AA AB
AB AC
....
The final solution be like one line of query.
WITH
L0 AS (
SELECT CHAR(n) AS Alphabet FROM dbo.[fn_nums](90) WHERE n >=65)
SELECT
A.Alphabet + B.Alphabet AS RangeStart,
A.Alphabet + char(ascii(B.Alphabet)+1) AS RangeEnd
FROM L0 AS A, L0 AS B
WHERE
A.Alphabet + B.Alphabet <> 'ZZ'
ORDER BY
A.Alphabet + B.Alphabet
Basically the first CTE
L0 AS (SELECT CHAR(n) AS Alphabet FROM dbo.[fn_nums](90) WHERE n >=65)
Will give us 'A' to 'Z' and then we cross join itself with the second column is the rangeEnd with second alphabet is the next alphabet of the first column. (It's easier just try to match the result to the query)
But before that can run we'll need to create THE function
CREATE FUNCTION [dbo].[fn_nums](@n AS BIGINT) RETURNS TABLE
AS
RETURN
WITH
L0 AS(SELECT 1 AS c UNION ALL SELECT 1),
L1 AS(SELECT 1 AS c FROM L0 AS A, L0 AS B),
L2 AS(SELECT 1 AS c FROM L1 AS A, L1 AS B),
L3 AS(SELECT 1 AS c FROM L2 AS A, L2 AS B),
L4 AS(SELECT 1 AS c FROM L3 AS A, L3 AS B),
L5 AS(SELECT 1 AS c FROM L4 AS A, L4 AS B),
Nums AS(SELECT ROW_NUMBER() OVER(ORDER BY c) AS n FROM L5)
SELECT n FROM Nums WHERE n <= @n;
This function will return a table result with a single column n and start from 1 to the parameter @n super fast. Very useful function check out Inside TSQL querying book by Izik Ben Gan.
James