This article is half-done without your Comment! *** Please share your thoughts via Comment ***
Check the below two solutions to draw a triangle without using any loop in SQL Server.
Require Output:
1 2 3 4 5 6 7 |
Triangle ---------- * ** *** **** ***** |
Solution 1:
1 2 3 4 5 6 7 8 9 10 11 12 |
declare @star varchar(10) = '*'; declare @num int = 5; ; with StarCTE as ( select cast(@star as varchar(10)) as Triangle, 1 as cnt union all select cast((Triangle+@star) as varchar(10)) as Triangle, cnt+1 as cnt from StarCTE where cnt < @num ) select Triangle from StarCTE |
Solution 2: Fetch number value from the MASTER..SPT_VALUES table of SQL Server.
1 2 3 4 5 6 7 8 |
SELECT REPLICATE(Star,number) Triangle FROM ( SELECT '*' Star ) b CROSS APPLY ( SELECT DISTINCT number FROM MASTER..SPT_VALUES WHERE number > 0 and number < 6 ) as T |
Please try the different solution for this puzzle and share it via comment...
Leave a Reply