MySQL advanced road (four) flow control

case structure
# create a stored procedure, based on the incoming results to display class, such as incoming results: 90-100, display A, 80-90, display B, 60-80, display c, otherwise, the display D
the CREATE PROCEDURE test_case (the IN Score the INT)
the BEGIN
the CASE
the WHEN Score> = 90 the AND Score <= 100 THEN the SELECT 'A';
the WHEN Score> = 80 THEN the SELECT 'B';
the WHEN Score> = 60 THEN the SELECT 'C';
the ELSE the SELECT 'D '
the END the CASE;
the END $
the CALL test_case (95) $

# Create a stored procedure, based on the incoming results to display class, such as incoming results: 90-100, display A, 80-90, display B, 60-80, display c, otherwise, the display D
the CREATE PROCEDURE test_ Case (IN score INT)
white the BEGIN
the CASE
the WHEN Score> = 90 the AND Score <= 100 THEN the SELECT 'A';
the WHEN Score> = 80 THEN the SELECT 'B';
the WHEN Score> = 60 THEN the SELECT 'C';
the ELSE the SELECT 'D ';
the END the CASE;
the END $
the CATT test_case (95) $

Second, the loop structure
classification:
the while, Loop, REPEAT
loop control:
iterate similar to continue, continue, the end of this cycle, continue to the next
leave similar break, jump out, the end of the cycle is currently located
1.while
syntax:
[label:] while loop conditions
do
loop;
End while [label];
2.loop
syntax:
[tags:] loop
loop
end loop [label];
can be used to simulate a simple endless loop

  1. repeat
    Syntax:
    [Tags:] repeat
    loop;
    an until loop end conditions
    End repeat [label]
    # is not added loop control statements
    Case: bulk insert, inserted into the admin table according to the number of the plurality of records

CREATE PROCEDURE Pro_whilel (IN insertCount INT)
BEGIN
DECLARE i INT DEFAULT 1;
WHILE i<= insertCount DO
INSERT INTO admin (username, "password') VALUES (CONCAT ('Rose',i), '666');
SET i=i+1;
END WHILE ;
END $
CALL pro_whilel (100)$

# 2 was added leave statement
t Case: bulk insert, according to the number of broadcast into the admin table, multiple records, if the number> 20 stops
the CREATE PROCEDURE test_whilel (the IN insertCount the INT)
the BEGIN
the DECLARE I the INT the DEFAULT. 1;
A: the WHILE I < = insertCount the DO
the INSERT the INTO adnin (username, 'password') the VALUES (CONCAT ( 'xiaohua', I), '0000');
the IF I> = 20 is THEN the LEAVE A;
the END IFI
the SET I = I +. 1;
the END the WHILE A ;
the END $
the CALL test_while1 (100) $

# 3 Add iterate statement
# Case: bulk insert, inserted into the admin table according to the number of multiple records, insert only even-order

CREATE PROCEDURE test_while1(IN insertCount INT)
BEGIN
DECLARE i INT DEFAULT 0;
a:WHILE i<=insertcount DO
SET i=i+1;
IF MOD(i,2)1=0 THEN ITERATE a;
END IF;
INSERT INTO adnin (username, password) VALUES (CONCAT('xiaohua',i), *0000');
END WHILE a;
END $
CALL test while1 (100)$

A known table stringcontent
wherein fields:
ID growth since
content varchar (20)
to insert a specified number of tables, a random string

DROP TABLE IF EXISTS stringcontent ;
CREATE TABLE stringcontent (
id INT PRIMARY KEY AUTO INCREMENT,
content VARCHAR (20)
);

$ DELIMITER
the CREATE PROCEDURE Test randstr INSERT (the IN insertCount the INT)
the BEGIN
DECLARE I the INT 12 is the DEFAULT define a loop variable i, represents the number of insertions
DECLARE STR VARCHAR (26 is) the DEFAULT 'abedefghijklmnopqrstuvwxyz';
DECLARE the INT startIndex the DEFAULT. 1; # represents the starting index
DECLARE len INT DEFAULT 1; # represents the length of the character clipping
the WHILE I <= the DO insertCount
the SET len = the FLOOR (the RAND () (-20 is startIndex +. 1) + 1'd); integer generates a random, representing interception length, 1- (startIndex + 26-. 1)
the SET startIndex = the FLOOR (the RAND () * + 26 is. 1); generating a random integer, representing the start prime primer 1-26
the INSERT the INTO StringContent (Content) the VALUES (the SUBSTR (STR, startIndex, len ));
the SET = I +. 1; Shen loop variable update
the END the WHILE;
the END $

Guess you like

Origin blog.51cto.com/14509987/2434182