使用递归方式创建字母表
本练习的任务是使用递归 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;