OceanBase logo

OceanBase

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

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

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

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

    Download PDF

    OceanBase logo

    The Unified Distributed Database for the AI Era.

    Follow Us
    Products
    OceanBase CloudOceanBase EnterpriseOceanBase Community EditionOceanBase seekdb
    Resources
    DocsBlogLive DemosTraining & Certification
    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.2.1
    iconOceanBase Database
    SQL - V 4.2.1
    SQL
    KV
    • 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

    Cursor execution process

    Last Updated:2023-12-25 08:49:42  Updated
    share
    What is on this page
    Declare a cursor
    Open a cursor
    Fetch the next row
    Close a cursor
    Examples

    folded

    share

    The complete execution process of a cursor consists of the following operations: use the DECLARE statement to declare the cursor, use the OPEN statement to open the cursor, use the FETCH statement to fetch the next row, and use the CLOSE statement to close the cursor.

    Declare a cursor

    The syntax is as follows:

    DECLARE cursor_name CURSOR FOR select_statement
    

    This syntax associates a cursor with a SELECT statement that does not contain the INTO clause. The SELECT statement can retrieve rows to be traversed by the cursor. To fetch rows later, use the FETCH statement. The number of columns retrieved by the SELECT statement must match the number of output variables specified in the FETCH statement.

    The cursor declaration must be prior to the handler declaration but after the variable and condition declaration. A stored program can contain multiple cursor declarations. However, a cursor declared in a specified block must have a unique name.

    In most cases, you can use a cursor with the INFORMATION_SCHEMA table to obtain equivalent information as that obtained by the SHOW statement.

    Open a cursor

    The syntax is as follows:

    OPEN cursor_name
    

    Fetch the next row

    To fetch the next row of the SELECT statement associated with the specified cursor, which must have been opened, and advance the pointer of the cursor, use the following syntax:

    FETCH [[NEXT] FROM] cursor_name INTO var_name [, var_name] ...
    

    If the row exists, the retrieved column is stored in the var_name variable. The number of columns retrieved by the SELECT statement must match the number of output variables specified in the FETCH statement.

    If no more row is available, the value of SQLSTATE will be 02000, which indicates no data. To detect this situation, you can set an exception handler or set a NOT FOUND condition. Note that the SELECT statement or other FETCH statements may also encounter the same situation, which will then cause the execution of the exception handler. To identify the operation that causes this situation, put this operation in its BEGIN END block so that the operation is associated only with its own exception handler.

    Close a cursor

    The syntax is as follows:

    CLOSE cursor_name
    

    If you execute this statement on a cursor that is not opened, an error is returned. If a cursor is not explicitly closed, it will be closed when the execution of the BEGIN END block where this cursor is declared is completed.

    Examples

    obclient> DELIMITER //
    
    obclient> CREATE PROCEDURE hr_curdemo()
       BEGIN
         DECLARE done INT DEFAULT FALSE;
         DECLARE x, y, z INT;
         DECLARE cur1 CURSOR FOR SELECT id,salary FROM hr.emp;
         DECLARE cur2 CURSOR FOR SELECT avg_sal FROM hr.avg;
         DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
    
         OPEN cur1;
         OPEN cur2;
    
         read_loop: LOOP
           FETCH cur1 INTO x, y;
           FETCH cur2 INTO z;
           IF done THEN
             LEAVE read_loop;
           END IF;
           IF y < z THEN
             INSERT INTO hr.low_sal VALUES (x,y);
           ELSE
             INSERT INTO hr.high_sal VALUES (x,z);
           END IF;
         END LOOP;
    
         CLOSE cur1;
         CLOSE cur2;
       END //
    
    Query OK, 0 rows affected
    

    Previous topic

    Overview
    Last

    Next topic

    Limitations on server cursors
    Next
    What is on this page
    Declare a cursor
    Open a cursor
    Fetch the next row
    Close a cursor
    Examples