開始使用免費開始

用遞迴方式建立字母表

這個練習要你使用遞迴 CTE 來建立字母表。

要完成這個任務,你需要知道字母 AZ 可以用數值 6590 表示。相對地,A 對應 65C 對應 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;
編輯並執行程式碼