The while loop test _mysql of MYSQL Circular statement

Source: Internet
Author: User
Tags goto

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 $$ 
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.