How many of these MySQL statement tags have you used?
In the article "A Peek into MySQL Stored Procedure Details" , we briefly introduced the use of stored procedures. The syntax for creating stored procedures includes BEGIN...END. In addition to BEGIN...END, the following statement tags can be used in the storage body of a stored procedure:
[begin_label:] BEGIN
[statement_list]
END [end_label]
[begin_label:] LOOP
statement_list
END LOOP [end_label]
[begin_label:] REPEAT
statement_list
UNTIL search_condition
END REPEAT [end_label]
[begin_label:] WHILE search_condition DO
statement_list
END WHILE [end_label]
Let's explain the above sentences one by one:
begin_label If begin_label is given, it must be followed by a colon (:). And if begin_label is given, it does not necessarily have to be followed by end_label. However, if end_label is given, it must be the same as begin_label.
end_label For this label, end_label cannot be given alone without begin_label.
Another point to note is that for these labels, the labels in each layer must be unique and cannot be the same. And the maximum length of each label string is 16 characters.
Let us take an example.
Example 1
mysql>delimiter //
mysql>CREATE PROCEDURE doiterate(p1 INT)
-->BEGIN
--> label1: LOOP
--> SET p1 = p1 + 1;
--> IF p1 < 10 THEN ITERATE label1; END IF;
--> LEAVE label1;
--> END LOOP label1;
-->END;
mysql>delimiter;
In the above example, label1 is the begin_label and end_label we mentioned above. According to our explanation above, the above example can also be changed to the following form.
Example 2
mysql>delimiter //
mysql>CREATE PROCEDURE doiterate(p1 INT)
-->BEGIN
--> label1: LOOP
--> SET p1 = p1 + 1;
--> IF p1 < 10 THEN ITERATE label1; END IF;
--> LEAVE label1;
--> END LOOP;
-->END;
mysql>delimiter;
Next, let’s take a look at the application of the above-mentioned sentence tags.
1. BEGIN……END
BEGIN ... END is mainly used to write compound statements. A compound statement may contain multiple statements, which are all delimited by BEGIN ... END. Just like the example below.
Example 3
mysql> delimiter //
mysql> create procedure adddata()
--> begin
--> declare n int default 0;
--> while n<100000
--> do
--> insert into proced(name,type) values(' Trace Memory Blog','onmpw');
--> set n = n+1;
--> end while;
--> end
-->//
mysql> delimiter ;
And BEGIN ... END blocks can be nested and can also be given names.
Example 4
mysql> delimiter //
mysql> CREATE PROCEDURE adddata()
-->label1:BEGIN
--> declare n int default 0;
--> while n<100000
--> do
--> insert into proced(name,type) values(‘迹忆博客’,’onmpw’);
--> set n = n+1;
--> end while;
-->END label1
-->//
mysql> delimiter ;
2. LOOP
[begin_label:] LOOP
statement_list
END LOOP [end_label]
LOOP 作用是重复执行statement_list,它可能包含一条或多条执行语句。这些语句都是用分号(;)来界定的,所以之前我们需要先使用delimiter //命令改变客户端命令行的界定符。这个命令在《mysql存储过程细节窥探》中有说过。对于这个标签的使用实例如下。
例五
mysql> delimiter //
mysql>CREATE PROCEDURE doiterate(p1 INT)
-->BEGIN
--> label1: LOOP
--> SET p1 = p1 + 1;
--> IF p1 < 10 THEN
--> ITERATE label1;
--> END IF;
--> LEAVE label1;
--> END LOOP label1;
--> SET @x = p1;
-->END;
mysql> delimiter ;
LOOP的需要借助ITERATE 来重复,而LOOP的循环结束需要有LEAVE来完成,当然也可以使用RETURN。这里的LEAVE我们可以理解成编程语言中的break来跳出循环。RETURN可以理解成编程语言中的return,亦可以跳出循环,不过这个是整个程序的退出。
3. REPEAT
[begin_label:] REPEAT
statement_list
UNTIL search_condition
END REPEAT [end_label]
同样REPEAT重复执行块儿内的statement_list,直到UNTIL的条件不成立的时候结束循环。因此,我们可以看出statement_list至少被执行一次。此语句标签可以理解成编程语言中的do{}while();
使用示例如下。
例六
mysql> delimiter //
mysql> CREATE PROCEDURE dorepeat(p1 INT)
-->BEGIN
--> SET @x = 0;
--> REPEAT
--> SET @x = @x + 1;
--> UNTIL @x > p1
-->END REPEAT;
--> END
--> //
mysql>delimiter ;
mysql> CALL dorepeat(1000);
mysql> SELECT @x;
+---------+
| @x |
+---------+
| 1001 |
+---------+
4. WHILE
[begin_label:] WHILE search_condition DO
statement_list
END WHILE [end_label]
WHILE表示在此块儿内的statement_list被重复执行,只要search_condition为真就会执行。也就是说此WHILE语句标签相当于编程语言中的while(){}语句。
使用示例如下。
例七
mysql> delimiter //
mysql>CREATE PROCEDURE dowhile()
-->BEGIN
--> DECLARE v1 INT DEFAULT 5;
--> label1:WHILE v1 > 0 DO
--> SET v1 = v1 - 1;
--> END WHILE label1;
-->END//
mysql>delimiter ;
以上就是对几种语句标签的简单介绍。
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Two ways to install mysql-5.5.47 on Linux system and manage mysql
Publish Date:2025/04/26 Views:140 Category:MySQL
-
We know that there are generally two ways to install software on the Linux system. One is to use rpm or yum to install, which is convenient and fast; the other is to use the compiled source package. Although this method is more troublesome
Mysql master-slave replication simple configuration
Publish Date:2025/04/26 Views:120 Category:MySQL
-
I'm learning about MySQL master-slave replication recently and want to apply it to a project at work. Since I'm new to it, I don't understand it very well, so I can only share how to make a simple configuration. At the beginning, we configu
Implementation details of MySQL master-slave replication (I) Exploration of the m
Publish Date:2025/04/26 Views:56 Category:MySQL
-
This article mainly discusses the implementation mechanism of master-slave replication, which may not be directly helpful for our actual application, but understanding its principle can achieve twice the result with half the effort for futu
Implementation details of MySQL master-slave replication (II) Exploration from th
Publish Date:2025/04/26 Views:74 Category:MySQL
-
Previously we explored the master server in master-slave replication. Now let's look at how the slave server works in the entire system. In the article Master Server Exploration, we mentioned that three threads are needed in a master-slave
Mysql master-slave replication - what to do if the slave server stops
Publish Date:2025/04/26 Views:97 Category:MySQL
-
You may find this topic a little ridiculous. What can we do if the server stops? Of course, we should restart the service. Restarting the service is no problem. The problem is that a lot of data has been written to the master database durin
MySQL stored procedure details
Publish Date:2025/04/26 Views:163 Category:MySQL
-
A stored procedure can be thought of as encapsulating a SQL statement that we need to process specially into a function. When needed, we only need to call this function to achieve the desired operation. This process can be called a stored p
How to back up a Kubernetes MySQL Operator cluster
Publish Date:2025/04/26 Views:79 Category:MySQL
-
Oracle's MySQL Operator for Kubernetes is a convenient way to automatically configure a MySQL database within a cluster. One of the key features of the Operator is the integrated automatic backup support for increased resiliency. The backup
How to select from multiple tables in MySQL
Publish Date:2025/04/25 Views:58 Category:MySQL
-
This article explains how to use MySQL to query from multiple tables in one script SELECT . Let's demonstrate a situation: SELECT name , price, details, type , FROM food, food_order WHERE breakfast.id = 'breakfast_id' Now, let's imagine FRO
Creating a table from CSV in MySQL
Publish Date:2025/04/25 Views:115 Category:MySQL
-
In this article, we aim to understand how to create a table from CSV in MySQL database. Businesses and organizations must quickly generate tables from large amounts of data. These organizations typically have large CSV files with large amou