We got to print ‘*’ 20 times followed by 19 times and so on till we only got to print 1!
delimiter $$
create procedure test()
begin
set @ans = 20;
while(@ans>0) do
select repeat('*',@ans);
set @ans = @ans - 1;
end while;
end $$
delimiter ;
call test();
This is getting the job done but also printing (repeat('*',@ans))
after each respective character line. What am I doing wrong?
2
Answers
Is this what you want :
You can add the alias you want in this exemple its
*
Your stored procedure is performing a select statement 20 times and each select statement has its own result-set, so this is what causes what you said: printing (repeat(‘*’,@ans)) after each respective character line.
Instead, you could use a variable to store the populated strings inside the while loop, then select that variable outside the loop.
see demo