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!

Original post (opens in new tab)

Rate

Share

Share

Rate