用遞迴方式建立字母表
這個練習要你使用遞迴 CTE 來建立字母表。
要完成這個任務,你需要知道字母 A 到 Z 可以用數值 65 到 90 表示。相對地,A 對應 65,C 對應 67。你可以使用函式 char(number) 將數字轉換成對應的字母。
本練習屬於課程
SQL Server 的階層式與遞迴查詢
練習說明
- 將
number_of_letter初始化為代表字母A的數字。 - 每一步將
number_of_letter加 1,並將上限設為90(也就是Z的值)。 - 從已定義的 CTE 中選取遞迴成員。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
WITH alphabet AS (
SELECT
-- Initialize letter to A
___ AS number_of_letter
-- Statement to combine the anchor and the recursive query
UNION ALL
SELECT
-- Add 1 each iteration
___ + ___
-- Select from the defined CTE alphabet
FROM ___
-- Limit the alphabet to A-Z
WHERE ___ < ___)
SELECT char(number_of_letter)
FROM alphabet;