Show Asterisk Triangle Shapes Using TSQL In SQL Server
In programming, it's a common exercise to show triangle shapes using the asterisk character. Well, this can be achieved using TSQL as well.
Script #1
Output:
Script #2
Output:
Cheers! :)
Script #1
DECLARE @Total1 INT DECLARE @ctrOuter1 INT DECLARE @ctrInner1 INT DECLARE @strPrint1 VARCHAR(100) SET @Total1 = 5 SET @ctrOuter1 = @Total1 SET @strPrint1 = '' WHILE @ctrOuter1 >= 1 --outer loop BEGIN SET @strPrint1 = '' SET @ctrInner1 = 0 WHILE @ctrInner1 <= (@Total1 - @ctrOuter1) BEGIN SET @strPrint1 = @strPrint1 + '*' SET @ctrInner1 = @ctrInner1 + 1 END PRINT @strPrint1 SET @strPrint1 = @strPrint1 + CHAR(13) SET @ctrOuter1 = @ctrOuter1 - 1 END
Script #2
DECLARE @Total INT DECLARE @ctrOuter INT DECLARE @ctrInner INT DECLARE @strPrint VARCHAR(100) SET @Total = 5 SET @ctrOuter = @Total SET @strPrint = '' WHILE @ctrOuter >= 1 --outer loop BEGIN SET @strPrint = '' SET @ctrInner = 1 WHILE @ctrInner <= @ctrOuter BEGIN SET @strPrint = @strPrint + '*' SET @ctrInner = @ctrInner + 1 END PRINT @strPrint SET @strPrint = @strPrint + CHAR(13) SET @ctrOuter = @ctrOuter - 1 END
Cheers! :)
Comments
Post a Comment