Displaying a Pyramid of Stars Using SQL Query
DECLARE @count INT,@string Varchar(max),@nooflevelsinPyramid INT,@char Char(2)
SET @count = 1
SET @string = ''
SET @nooflevelsinPyramid = 5
Set @char = '*' -- Character that you wanted to display
WHILE (@count <=@nooflevelsinPyramid)
BEGIN
PRINT Replicate(@char,@count)
SET @count = @count + 1
END
output :
*
* *
* * *
* * * *
* * * * *
Scenario 2:
DECLARE @count INT,@string Varchar(max),@nooflevelsinPyramid INT,@char Char(2)
SET @count = 1
SET @string = ''
SET @nooflevelsinPyramid = 10 -- No of levels in pyramid
Set @char = '*' -- Character that you wanted to display
WHILE (@count <=@nooflevelsinPyramid)
BEGIN
SET @string = (select Replicate(' ',@nooflevelsinPyramid-@count) + Replicate(@char,@count))
PRINT @string
SET @count = @count + 1
END
Output :
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *