参考官方文档地址
https://docs.oracle.com/en/database/oracle/oracle-database/23/lnpls/EXIT-statement.html#GUID-66E20B6C-3606-42AD-A7DB-C8EC782B94D8
5.1. WHILE LOOP 使用EXIT语句终止WHILE循环
在这个示例中,当变量a的值大于15时,使用EXIT语句终止WHILE循环。
DECLARE
a NUMBER(2) := 10;
BEGIN
WHILE a < 20 LOOP
DBMS_OUTPUT.PUT_LINE('value of a: ' || a);
a := a + 1;
IF a > 15 THEN
EXIT; -- 终止循环
END IF;
END LOOP;
END;
/
5.2. 带EXIT语句的基本LOOP语句
在这个示例中,基本LOOP语句中的EXIT语句无条件地将控制转移到当前循环的末尾。
In this example, the EXIT statement inside the basic LOOP statement transfers control unconditionally to the end of the current loop.
DECLARE
x NUMBER := 0;
BEGIN
LOOP
DBMS_OUTPUT.PUT_LINE ('Inside loop: x = ' || TO_CHAR(x));
x := x + 1;
IF x > 3 THEN
EXIT;
END IF;
END LOOP;
-- After EXIT, control resumes here
DBMS_OUTPUT.PUT_LINE(' After loop: x = ' || TO_CHAR(x));
END;
/
5.3. 带有EXIT WHEN语句的基本LOOP语句
在这个示例中,当x大于3时,基本LOOP语句中的EXIT WHEN语句将控制权转移到当前循环的末尾。
In this example, the EXIT WHEN statement inside the basic LOOP statement transfers control to the end of the current loop when x is greater than 3.
DECLARE
x NUMBER := 0;
BEGIN
LOOP
DBMS_OUTPUT.PUT_LINE('Inside loop: x = ' || TO_CHAR(x));
x := x + 1; -- prevents infinite loop
EXIT WHEN x > 3;
END LOOP;
-- After EXIT statement, control resumes here
DBMS_OUTPUT.PUT_LINE('After loop: x = ' || TO_CHAR(x));
END;
/