This article uses examples to illustrate the usage of loop statements (WHILE, REPEAT and LOOP) in MySQL stored procedures. Share with you for your reference, the details are as follows: MySQL provides loop statements that allow us to repeatedly execute a SQL code block based on conditions. There are three loop statements: WHILE, REPEAT, and LOOP. Let's look at them one by one. First, let's look at the syntax of the WHILE statement: WHILE expression DO statements END WHILE The WHILE loop checks the expression at the beginning of each iteration. If expressionevaluates is TRUE, MySQL executes the statements between WHILE and END WHILE until expressionevaluates is FALSE. A WHILE loop is called a pre-test condition loop because it always checks the expression of the statement before executing it. Let’s look at the flow chart: After that, let's try to use the WHILE loop statement in the stored procedure. Let's take a look at an example: DELIMITER $$ DROP PROCEDURE IF EXISTS test_mysql_while_loop$$ CREATE PROCEDURE test_mysql_while_loop() BEGIN DECLARE x INT; DECLARE str VARCHAR(255); SET x = 1; SET str = ''; WHILE x <= 5 DO SET str = CONCAT(str,x,','); SET x = x + 1; END WHILE; SELECT str; END$$ DELIMITER ; In the above test_mysql_while_loop stored procedure, its operation is to repeatedly build the str string until the value of the x variable is greater than 5, and then use the select statement to display the final string. We should note that if the value of the x variable is not initialized, then its default value is NULL. Therefore, the condition in the WHILE loop statement will always be TRUE, and we will have an indefinite loop, which is unpredictable. Without further ado, let's call the test_mysql_while_loopstored stored procedure: CALL test_mysql_while_loop(); Execute the above query statement and get the following results: mysql> CALL test_mysql_while_loop(); +------------+ | str | +------------+ | 1,2,3,4,5, | +------------+ 1 row in set Query OK, 0 rows affected After finishing, let's look at the grammatical structure of the REPEAT loop statement: REPEAT statements; UNTIL expression END REPEAT The above SQL is first executed by MySQL. After that, MySQL will evaluate the expression. If the expression is FALSE, MySQL will repeatedly execute the statement until the expression is TRUE. Because the REPEAT loop statement checks expression after executing statements, the REPEAT loop statement is also called a test-after loop. Let's look at the flow chart: After finishing, let's use the REPEAT loop statement to rewrite the test_mysql_while_loop stored procedure: DELIMITER $$ DROP PROCEDURE IF EXISTS mysql_test_repeat_loop$$ CREATE PROCEDURE mysql_test_repeat_loop() BEGIN DECLARE x INT; DECLARE str VARCHAR(255); SET x = 1; SET str = ''; REPEAT SET str = CONCAT(str,x,','); SET x = x + 1; UNTIL x > 5 END REPEAT; SELECT str; END$$ DELIMITER ; We should note that there is no semicolon (;) in the UNTIL expression. Execute the above query statement and get the following results: mysql> CALL mysql_test_repeat_loop(); +------------+ | str | +------------+ | 1,2,3,4,5, | +------------+ 1 row in set Query OK, 0 rows affected Finally, let's look at an example of using the LOOP statement: CREATE PROCEDURE test_mysql_loop() BEGIN DECLARE x INT; DECLARE str VARCHAR(255); SET x = 1; SET str = ''; loop_label: LOOP IF x > 10 THEN LEAVE loop_label; END IF; SET x = x + 1; IF (x mod 2) THEN ITERATE loop_label; ELSE SET str = CONCAT(str,x,','); END IF; END LOOP; SELECT str; END; The specific functions of the above SQL are as follows:
Execute the above query statement and get the following results: mysql> CALL test_mysql_loop(); +-------------+ | str | +-------------+ | 2,4,6,8,10, | +-------------+ 1 row in set Query OK, 0 rows affected Now let's look at the two key words for the control loop:
Okay, that’s all for this record. Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL stored procedure skills", "MySQL common function summary", "MySQL log operation skills", "MySQL transaction operation skills summary" and "MySQL database lock related skills summary" I hope this article will be helpful to everyone's MySQL database design. You may also be interested in:
|
>>: VUE introduces the implementation of using G2 charts
1. Inline style, placed in <body></body&g...
join() method: connects all elements in an array ...
1. First remotely connect to the server 2. Open S...
tomcat server configuration When everyone is lear...
This article uses examples to describe the add, d...
background When performing a SQL query, I tried t...
Related articles: Beginners learn some HTML tags ...
Table of contents Multi-application deployment 1-...
view What is a view? What is the role of a view? ...
Table of contents 1. Introduction 2. On-demand at...
1. Set firewall rules Example 1: Expose port 8080...
The following is a picture mouse hover zoom effec...
Table of contents Primary key index Create indexe...
Table of contents 1. Bootstrap Grid Layout 2. Ver...
When we use TypeScript, we want to use the type s...