OceanBase logo

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

Product Overview
DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Resources

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS

OceanBase Cloud

OceanBase Database

Tools

Connectors and Middleware

QUICK START

OceanBase Cloud

OceanBase Database

BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Company

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

International - English
中国站 - 简体中文
日本 - 日本語
Sign In
Start on Cloud

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

Product Overview
DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS
OceanBase CloudOceanBase Database
ToolsConnectors and Middleware
QUICK START
OceanBase CloudOceanBase Database
BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

Start on Cloud
编组
All Products
    • Databases
    • iconOceanBase Database
    • iconOceanBase Cloud
    • iconOceanBase Tugraph
    • iconInteractive Tutorials
    • iconOceanBase Best Practices
    • Tools
    • iconOceanBase Cloud Platform
    • iconOceanBase Migration Service
    • iconOceanBase Developer Center
    • iconOceanBase Migration Assessment
    • iconOceanBase Admin Tool
    • iconOceanBase Loader and Dumper
    • iconOceanBase Deployer
    • iconKubernetes operator for OceanBase
    • iconOceanBase Diagnostic Tool
    • iconOceanBase Binlog Service
    • Connectors and Middleware
    • iconOceanBase Database Proxy
    • iconEmbedded SQL in C for OceanBase
    • iconOceanBase Call Interface
    • iconOceanBase Connector/C
    • iconOceanBase Connector/J
    • iconOceanBase Connector/ODBC
    • iconOceanBase Connector/NET
icon

OceanBase Database

SQL - V4.4.2

    Download PDF

    OceanBase logo

    The Unified Distributed Database for the AI Era.

    Follow Us
    Products
    OceanBase CloudOceanBase EnterpriseOceanBase Community EditionOceanBase seekdb
    Resources
    DocsBlogLive DemosTraining & CertificationTicket
    Company
    About OceanBaseTrust CenterLegalPartnerContact Us
    Follow Us

    © OceanBase 2026. All rights reserved

    Cloud Service AgreementPrivacy PolicySecurity
    Contact Us
    Document Feedback
    1. Documentation Center
    2. OceanBase Database
    3. SQL
    4. V4.4.2
    iconOceanBase Database
    SQL - V 4.4.2
    Databases
    • OceanBase Database
    • OceanBase Cloud
    • OceanBase Tugraph
    • Interactive Tutorials
    • OceanBase Best Practices
    Tools
    • OceanBase Cloud Platform
    • OceanBase Migration Service
    • OceanBase Developer Center
    • OceanBase Migration Assessment
    • OceanBase Admin Tool
    • OceanBase Loader and Dumper
    • OceanBase Deployer
    • Kubernetes operator for OceanBase
    • OceanBase Diagnostic Tool
    • OceanBase Binlog Service
    Connectors and Middleware
    • OceanBase Database Proxy
    • Embedded SQL in C for OceanBase
    • OceanBase Call Interface
    • OceanBase Connector/C
    • OceanBase Connector/J
    • OceanBase Connector/ODBC
    • OceanBase Connector/NET
    SQL
    KV
    • V 4.6.0
    • V 4.4.2
    • V 4.3.5
    • V 4.3.3
    • V 4.3.1
    • V 4.3.0
    • V 4.2.5
    • V 4.2.2
    • V 4.2.1
    • V 4.2.0
    • V 4.1.0
    • V 4.0.0
    • V 3.1.4 and earlier

    Loop control

    Last Updated:2026-04-14 12:43:57  Updated
    Share
    What is on this page
    Basic LOOP
    EXIT statement
    EXIT WHEN statement
    CONTINUE statement
    CONTINUE WHEN statement
    FOR LOOP statement
    WHILE LOOP statement

    folded

    Share

    Loop statements are used to repeatedly execute a set of operations.

    Applicability

    This topic applies only to OceanBase Database Enterprise Edition. OceanBase Database Community Edition provides only MySQL-compatible mode.

    The LOOP statement has the following four forms:

    • Basic LOOP

    • FOR LOOP

    • FOR LOOP for cursors

    • WHILE LOOP

    The following two statements are used to exit the entire loop:

    • EXIT

    • EXIT WHEN

    The following two statements are used to exit the current iteration of the loop:

    • CONTINUE

    • CONTINUE WHEN

    The EXIT, EXIT WHEN, CONTINUE, and CONTINUE WHEN statements can be used to exit a loop. These statements can be placed anywhere in the loop. We recommend that you use these statements to exit a loop instead of the GOTO statement. The EXIT statement transfers control to the end of the LOOP statement. The CONTINUE statement exits the current iteration of the loop and transfers control to the start of the next iteration. You can add an optional WHEN clause to the EXIT and CONTINUE statements. In the WHEN clause, you can specify a condition.

    If a loop contains multiple nested loops, we recommend that you label the loops to improve readability.

    Basic LOOP

    The basic LOOP statement repeatedly executes a sequence of statements. The basic LOOP statement has the following syntax:

    [ label ] LOOP
        statements
    END LOOP [ label ];
    

    In the LOOP statement, the statements clause specifies the statements to be executed repeatedly. To avoid an infinite loop, the LOOP and END LOOP statements must contain at least one EXIT statement. Otherwise, the LOOP statement will execute repeatedly without stopping.

    The EXIT statement can have an optional WHEN clause. When the condition in the WHEN clause is TRUE, the EXIT statement is executed, and control is transferred to the statement after the END LOOP statement.

    EXIT statement

    The EXIT statement is used to unconditionally exit the current loop.

    Here is an example:

    obclient> DECLARE
        cnt NUMBER := 0;
    BEGIN
        LOOP
            DBMS_OUTPUT.PUT_LINE ('INSIDE: ' || TO_CHAR(cnt));
            cnt := cnt + 1;
            IF cnt > 5 THEN
             EXIT;
            END IF;
         END LOOP;
         DBMS_OUTPUT.PUT_LINE('OUTSIDE: ' || TO_CHAR(cnt));
    END;
    /
    Query OK, 0 rows affected
    
    INSIDE: 0
    INSIDE: 1
    INSIDE: 2
    INSIDE: 3
    INSIDE: 4
    INSIDE: 5
    OUTSIDE: 6
    

    EXIT WHEN statement

    The EXIT WHEN statement is used to exit the current loop when the condition in the WHEN clause is TRUE.

    Here is an example:

    obclient> DECLARE
        cnt NUMBER := 0;
    BEGIN
        LOOP
           DBMS_OUTPUT.PUT_LINE ('INSIDE: ' || TO_CHAR(cnt));
           cnt := cnt + 1;
           EXIT WHEN cnt >5;
         END LOOP;
         DBMS_OUTPUT.PUT_LINE('OUTSIDE: ' || TO_CHAR(cnt));
    END;
    /
    Query OK, 0 rows affected
    
    INSIDE: 0
    INSIDE: 1
    INSIDE: 2
    INSIDE: 3
    INSIDE: 4
    INSIDE: 5
    OUTSIDE: 6
    

    CONTINUE statement

    The CONTINUE statement is used to exit the current iteration of the loop. That is, the code from the CONTINUE statement to the end of the loop is skipped, and the loop continues from the beginning of the next iteration.

    obclient> DECLARE
       cnt NUMBER := 0;
    BEGIN
        LOOP
           cnt := cnt + 1;
           IF cnt < 4 THEN
             CONTINUE;
           END IF;
        DBMS_OUTPUT.PUT_LINE ('INSIDE: ' || TO_CHAR(cnt));
           EXIT WHEN cnt >5;
        END LOOP;
        DBMS_OUTPUT.PUT_LINE('OUTSIDE: ' || TO_CHAR(cnt));
    END;
    /
    Query OK, 0 rows affected
    
    INSIDE: 4
    INSIDE: 5
    INSIDE: 6
    OUTSIDE: 6
    

    CONTINUE WHEN statement

    The CONTINUE WHEN statement is also used to exit the current iteration of the loop. However, the CONTINUE WHEN statement is executed only when the condition in the WHEN clause is TRUE.

    Here is an example:

    obclient>DECLARE
       cnt NUMBER := 0;
    BEGIN
        LOOP
           cnt := cnt + 1;
           CONTINUE WHEN cnt < 4;
           DBMS_OUTPUT.PUT_LINE ('INSIDE: ' || TO_CHAR(cnt));
           EXIT WHEN cnt >5;
         END LOOP;
         DBMS_OUTPUT.PUT_LINE('OUTSIDE: ' || TO_CHAR(cnt));
    END;
    /
    Query OK, 0 rows affected
    
    INSIDE: 4
    INSIDE: 5
    INSIDE: 6
    OUTSIDE: 6
    

    FOR LOOP statement

    The FOR LOOP statement is used to repeatedly execute a set of statements for a range of index values. The syntax is as follows:

    [ label ] FOR index IN [ REVERSE ] lower_bound..upper_bound LOOP
        statements
    END LOOP [ label ];
    

    In the following example, the index variable starts from the lower_bound value and increments to the upper_bound value when the REVERSE clause is not specified. If the REVERSE clause is specified, the index variable starts from the upper_bound value and decrements to the lower_bound value.

    obclient> BEGIN
        FOR i IN 1..5 LOOP
            DBMS_OUTPUT.PUT_LINE (i);
        END LOOP;
    END;
    /
    Query OK, 0 rows affected
    
    1
    2
    3
    4
    5
    
    obclient>BEGIN
       FOR i IN REVERSE 1..5  LOOP
          DBMS_OUTPUT.PUT_LINE (i);
       END LOOP;
    END;
    /
    Query OK, 0 rows affected
    5
    4
    3
    2
    1
    

    The index variable in the FOR LOOP statement is implicitly declared as a PLS_INTEGER variable local to the loop. The statements in the loop can read the value of the index variable, but cannot modify it. Statements outside the loop cannot reference the index variable. After the FOR LOOP statement is executed, the index variable is undefined. The index variable is also called the loop counter.

    The lower and upper bounds of the FOR LOOP statement can be a numeric literal, a numeric variable, or a numeric expression. If the bounds do not have numeric values, a predefined exception VALUE_ERROR is raised by PL.

    WHILE LOOP statement

    The WHILE LOOP statement is another form of loop control. It repeatedly executes a sequence of statements as long as the condition is TRUE. If the condition is FALSE, the loop is exited. The syntax is as follows:

    [ label ] WHILE condition LOOP
        statements
    END LOOP [ label ];
    

    The loop continues as long as the condition is TRUE, until the condition is FALSE or NULL.

    obclient> DECLARE
        cnt NUMBER := 0;
    BEGIN
        WHILE cnt < 3 LOOP
           DBMS_OUTPUT.PUT_LINE (cnt);
           cnt :=  cnt + 1;
        END LOOP;
    END;
    /
    Query OK, 0 rows affected
    
    0
    1
    2
    

    Previous topic

    Conditional control
    Last

    Next topic

    Sequential control
    Next
    What is on this page
    Basic LOOP
    EXIT statement
    EXIT WHEN statement
    CONTINUE statement
    CONTINUE WHEN statement
    FOR LOOP statement
    WHILE LOOP statement