Xmas tree in T-SQL

This is a very simple version of a Xmas tree using T-SQL, also is a good example to learn how recursive CTE works.

So, if you are in front of the computer with little work to do, take a look at the code, maybe you can improve it further!


/* Draw a simple xmas tree using recursion 
   🎄 Happy holidays!              */
WITH XmasTree
AS (
 SELECT 
  CAST(REPLICATE(' ', 16) + '^' AS VARCHAR(50)) AS t,
  0 AS lvl
 UNION ALL
 SELECT 
  CAST(REPLICATE(' ', 15 - lvl) + '/' + REPLICATE('*', 2 * lvl + 1) + '\' AS VARCHAR(50))
  ,n.lvl + 1
 FROM XmasTree n
 WHERE lvl < 16
 )
SELECT t as [Happy Holidays]
FROM XmasTree;

And this is the final result:



HAPPY HOLIDAYS!

Comments

Popular posts from this blog

Install Python on Windows

Checking Azure SQL DB service tier via T-SQL

Quick tip: Zoom in Azure Data Studio