skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER
    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 👌


  2. 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.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search