语法结构

label: <construct>
[label]

Labels 是MariaDB和mysql用来定义BEGIN ... END construct结构的 标识符,Label最大长度为16位,可以被`引用。

Labels 有开始部分和结束部分。 Labels have a start part and an end part. The start part must precede the portion of code it refers to, must be followed by a colon (:) and can be on the same or different line. The end part is optional and adds nothing, but can make the code more readable. If used, the end part must precede the construct's delimiter (;). Constructs identified by a label can be nested. Each construct can be identified by only one label.

LEAVE and ITERATE statements can be used to exit or repeat a portion of code identified by a label. LEAVE and ITERATE must be in the same Stored Routine, Trigger or Event which contains the target label.

实例

A simple label which is used to exit a LOOP:

CREATE PROCEDURE `test_sp`()
BEGIN
	`my_label`:
	LOOP
		SELECT 'looping';
		LEAVE `my_label`;
	END LOOP;
	SELECT 'out of loop';
END;

The following label is used to exit a procedure, and has an end part:

CREATE PROCEDURE `test_sp`()
`my_label`:
BEGIN
	IF @var = 1 THEN
		LEAVE `my_label`;
	END IF;
	DO something();
END `my_label`;

Comments

Comments loading...