Home > front end >  how to calculate the sum of the first 100 integers in MySQL with a while loop
how to calculate the sum of the first 100 integers in MySQL with a while loop

Time:09-20

I must find the sum of the first 100 natural numbers. 1 2 3 ... n, and so on. While using "while loops".

delimiter $

create procedure `calcul`.somme()

begin

declare s int default 0;
declare i int default 1;

while(i<=100) do
    set s := s i;
    set i := i 1;
end while;
select s;

end;
$
call `calcul`.somme()

when I call the somme() I'm getting this error -->

call calcul.somme()= Error Code: 2013. Lost connection to MySQL server during query

CodePudding user response:

The query is taking too long and the engine is timing out.

You can edit the SQL Editor preferences in MySQL Workbench:

  1. In the application menu, select Edit > Preferences > SQL Editor.
  2. Look for the MySQL Session section and increase the DBMS connection read time out value.
  3. Save the settings, quite MySQL Workbench and reopen the connection.

CodePudding user response:

use calcul;
delimiter $
create procedure somme()
begin


declare s int default(0);
declare i int default(1);

    while(i<=100) do
    set s := s i;
    set i := i 1;
    end while;
select s;
end;
$
CALL somme()

thanks, the Problem solved

  • Related