I am asked to create a user defined function in SQL Server to returns the following pattern (for example, if the input = 5):
*****
****
***
**
*
Here is my code:
alter function udf_star (@input int)
returns varchar (200)
as
begin
declare @star int
set @star = @input
declare @space int
set @space = 0
while @star > 0
begin
declare @string varchar (200)
set @string = replicate (' ', @space) + replicate ('*', @star)
set @star = @star - 1
set @space = @space + 1
end
return @string
end
When I execute the function
select dbo.udf_star (5)
it only shows
' *'
(4 spaces + 1 star); can anyone points out how should I correct the syntax?
Thanks in advance!