[SOLVED] how to calculate the sum of the first 100 integers in MySQL with a while loop


This Content is from Stack Overflow. Question asked by wiam leo

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()


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;

call `calcul`.somme()


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.

This Question was asked in StackOverflow by wiam leo and Answered by Rohit Gupta It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?