MySQL Loop statement instance tutorial MySQL while loop test

Source: Internet
Author: User
Tags goto

Operating in the MySQL database also has a circular statement operation, the standard loop mode: While loop, loop loop, and repeat loop.
There is also a non-standard loop: Goto. It is not recommended to use because the jump in goto statements can cause confusion in the mind used.

The format of these loop statements is as follows:
While ... Do ... END while
REPEAT ... UNTIL END REPEAT
LOOP ... END LOOP
Goto.
At the moment I only tested the while loop:
Delimiter $$//definition terminator is $$
drop procedure if exists wk; Delete an existing stored procedure
CREATE PROCEDURE wk ()//Creating a 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 if the command is over and MySQL can execute it.
The purpose of using the delimiter redefinition Terminator here is to not let the statements in the stored procedure be output at the time of definition. (Script Academy Www.jbxue.com)
The simple syntax for creating a MySQL stored procedure is:
CREATE PROCEDURE Stored Procedure name ([in | out | inout] parameter)

BEGIN

Mysql statement

END
To call a stored procedure:
Call stored procedure name ()//name 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); " > Second, 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 >= 20

End repeat;


End//

----Call
Call LOOPPC ()
Third, Loop loop
Delimiter $$
drop procedure if exists lopp;
CREATE PROCEDURE Lopp ()
Begin
declare i int;
Set i = 1;

Lp1:loop//LP1 the loop body name loop as the keyword INSERT into user_profile (UID) values (i);
Set i = i+1;

If i >
Leave LP1; Leave the Loop body
End If;
End LOOP; End Loop
End $$

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.