The MySQL operation also has the cyclic statement operation, the network says has 3 standard cycle way: While loop, loop loop and repeat loop. There is also a nonstandard cycle: goto. In view of the jump of the goto statement will cause the use of confusion, so do not recommend use.
These loop statements are formatted as follows:
While ... Do ... End While
REPEAT ... UNTIL End REPEAT
LOOP ... End LOOP
Goto.
I've only tested the while loop for now:
Delimiter $$//define terminator is $$
drop procedure if exists wk;//delete existing stored procedures create
procedure wk ()//Create new stored procedure
begin
declare i int; Variable declaration
Set i = 1; While
i < one do//loop body
INSERT into user_profile (UID) values (i);
Set i = i +1;
End While;
End $$//Ending definition statement
//Call
delimiter; First, return the Terminator to;
Call WK ();
Delimter:mysql the default delimiter is; Tell the MySQL interpreter whether the command is over and whether MySQL is ready to execute.
The use of delimiter to redefine the Terminator is to not allow the statements in the stored procedure to be output when defined.
The simple syntax for creating a MySQL stored procedure is:
CREATE PROCEDURE Stored Procedure name ([in | out | inout] parameter)
BEGIN
Mysql statement
end
To invoke a stored procedure:
Call stored procedure name ()//name to be appended ()
<span style= "Color:rgb (57, 57, 57); Font-family:verdana, ' Ms Song ', Arial, Helvetica, Sans-serif; font-size:14px; line-height:21px; Background-color:rgb (250, 247, 239); > II, REPEAT cycle </span>
<pre name= "code" class= "HTML" >delimiter//
drop procedure if exists LOOPPC;
CREATE PROCEDURE LOOPPC ()
begin
declare i int;
Set i = 1;
Repeat
INSERT into user_profile_company (UID) values (i+1);
Set i = i + 1;
Until I >= end
repeat;
End//
----Invoke call
LOOPPC ()
Iii. Loop Loop
delimiter $$ drop procedure if exists lopp;
CREATE PROCEDURE Lopp () BEGIN declare I int;
Set i = 1;
Lp1:loop//LP1 for loop body name loop for keyword INSERT into user_profile (UID) values (i);
Set i = i+1; If i > then leave LP1;
Leave the loop body End if; End LOOP; Ending Loop end $$