快速生成基數的輔助表

想生一張26個字母的表,怎樣實現。html

能夠查看到之前的方法《SQL循環26個字母插入到一個表中https://www.cnblogs.com/insus/archive/2010/12/23/1914346.htmlgit

現在可使用CTE來快速生成:api

;WITH alphabet AS
(
    SELECT 65 AS capital
    UNION ALL
    SELECT capital + 1 FROM alphabet WHERE capital < 90
)
SELECT CHAR(capital) FROM alphabet
Source Code

 

結果以下:ide

 

如想生成數字或其它呢?post

之前的辦法《使用CTE生成輔助表(數字或時間)等http://www.javashuo.com/article/p-yyuhzudm-eg.htmlurl

如今解決方案:spa

 

;WITH Digits AS
(
    SELECT 0 AS digit
    UNION ALL
    SELECT digit + 1 FROM Digits where digit < 9
)
SELECT [digit] FROM Digits
Source Code

 

擴展,若是想生成包括10000以內的數字表呢?3d

 

 

;WITH Digits AS
(
    SELECT 0 AS digit
    UNION ALL
    SELECT digit + 1 FROM Digits where digit < 9
)

SELECT kilobit.[digit] * 1000 + hundreds.[digit] * 100 + decade.[digit] * 10 +  unit.[digit] + 1 FROM Digits AS unit
CROSS JOIN  Digits AS decade
CROSS JOIN Digits AS hundreds
CROSS JOIN Digits AS kilobit
Source Code
相關文章
相關標籤/搜索