The three MySQL circulating while, loop, repeat seeking 1-n and
the first while loop:
/ * Grammar while loop:
while conditions DO
loop;
End while;
* /
Create Procedure SUM1 (A int)
the begin
DECLARE SUM int default 0; - default is to specify a default value of the variable
DECLARE default I int. 1;
the while I <a = the DO - cycle start
SET SUM = SUM + I;
SET I = I +. 1;
End the while; - loop end
select sum; - output
end;
- executes a stored procedure
Call SUM1 (100);
- delete the stored procedure
drop procedure if exists sum1;
The second loop loop:
/ * loop loop syntax:
loop_name: loop
IF condition THEN - when the condition is satisfied to leave the cycle
leave loop_name; - and break almost all the lecture ended
End IF;
End loop;
* /
the Create Procedure sums (A int)
the begin
DECLARE SUM int default 0;
DECLARE default I int. 1;
loop_name: loop - cycle start
IF I> a the then
Leave loop_name; - Analyzing the end condition is satisfied like the java boeak loop
end IF;
SET SUM = SUM I +;
SET I = I +. 1;
end loop; - end of the loop
select sum; - output
end;
- executes a stored procedure
Call sums (100);
- delete the stored procedure
drop procedure if exists sums;
The third repeat loop:
/ * repeat loop syntax
repeat
loop
until the condition
End repeat;
* /
Create Procedure SUMn (A int)
the begin
DECLARE SUM int default 0;
DECLARE default I int. 1;
repeat - cycle start
set sum = sum I +;
SET I = I +. 1;
an until I> end A REPEAT; - end of the cycle
select sum; - the output
end;
- execute a stored procedure
Call SUMn (100);
- delete the stored procedure
drop procedure if exists sumn;