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.3.3

    Download PDF

    OceanBase logo

    The Unified Distributed Database for the AI Era.

    Follow Us
    Products
    OceanBase CloudOceanBase EnterpriseOceanBase Community EditionOceanBase seekdb
    Resources
    DocsBlogWhite PaperLive 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.3.3
    iconOceanBase Database
    SQL - V 4.3.3
    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

    Traverse cursor data by using the FOR loop

    Last Updated:2024-12-02 03:48:27  Updated
    Share
    What is on this page
    Share

    PL provides easier-to-use FOR loop statements to automatically execute the OPEN, FETCH, and CLOSE statements and loop statements of cursors.

    Applicability

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

    When the program enters a loop, the FOR loop statement of the cursor automatically opens the cursor and fetches the first row of cursor data. When the program enters the next loop after processing the fetched data, the FOR loop statement of the cursor automatically fetches the next row of data for processing by the program. After all data rows are fetched from the result set, the loop is ended and the cursor is automatically closed.

    The syntax is as follows:

    FOR index_variable IN cursor_name[value[, value]...] LOOP
        -- Cursor data processing code
    END LOOP;
    

    index_variable is the index variable implicitly declared by the FOR loop statement of the cursor. This index variable does not need to be declared in DECLARE. It is of the RECORD type. Its structure is the same as that of the result set returned by the cursor query statement.

    The program can reference elements in this index variable to read the fetched cursor data. The names of elements in index_variable are the same as the column names in the select list of the cursor query statement. If calculation columns exist in the select list of the cursor query statement, you must specify aliases for these calculation columns so that the column data can be accessed by using the index variable in the FOR loop statement of the cursor.

    Here is an example:

    obclient> CREATE TABLE emp(  
       empno          NUMBER(4,0),  
       empname        VARCHAR(10),  
       job            VARCHAR(10),   
       deptno         NUMBER(2,0),
       salary         NUMERIC  
    );
    Query OK, 0 rows affected  
    
    obclient> INSERT INTO emp VALUES (200,'Jennifer','AD_ASST',1,15000),(202,'Pat','MK_REP',2,12000),
              (119,'Karen','PU_CLERK', 4,10000),(118,'Guy','PU_CLERK', 4,10000),
             (201,'Michael','MK_MAN',3,9000);
    Query OK, 5 rows affected (0.01 sec)
    Records: 5  Duplicates: 0  Warnings: 0
    
    obclient> DECLARE
             CURSOR c_emp IS SELECT empname, salary
                  FROM emp where rownum<5;
          BEGIN
             -- Implicitly open the cursor.
             FOR r_emp IN c_emp LOOP
                 -- Implicitly execute the FETCH statement.
                 DBMS_OUTPUT.PUT_LINE(r_emp.empname||'''s salary is '||r_emp.salary );
              -- Implicitly detect c_emp%NOTFOUND.
             END LOOP;
          END;
         /
    Query OK, 0 rows affected
    
    Jennifer's salary is 15000
    Pat's salary is 12000
    Karen's salary is 10000
    Guy's salary is 10000
    

    When you use the FOR loop statement to traverse cursor data, cursor parameters can also be passed. Here is an example:

    obclient> DECLARE
            CURSOR c_emp (in_job varchar default 'AC_MGR' ) IS
                 SELECT empname, salary
                 FROM emp where job = in_job;
                 v_job  emp.job%TYPE;
         BEGIN
             v_job := 'PU_CLERK';
             FOR r_emp IN c_emp(v_job) LOOP
                 DBMS_OUTPUT.PUT_LINE('DEPT '||v_job||': '||r_emp.empname||'''s salary is '||r_emp.salary );
             END LOOP;
         END;
         /
    Query OK, 0 rows affected
    
    DEPT PU_CLERK: Karen's salary is 10000
    DEPT PU_CLERK: Guy's salary is 10000
    

    PL also allows you to use subqueries in a FOR loop statement to directly define cursors. Here is an example:

    obclient> BEGIN
            FOR r_emp IN (SELECT empname, salary FROM emp) LOOP
                DBMS_OUTPUT.PUT_LINE(r_emp.empname||'''s salary is '||r_emp.salary );
            END LOOP;
         END;
         /
    Query OK, 0 rows affected
    
    Jennifer's salary is 15000
    Pat's salary is 12000
    Karen's salary is 10000
    Guy's salary is 10000
    Michael's salary is 9000
    

    Previous topic

    Cursor variables
    Last

    Next topic

    Modify and drop a cursor
    Next