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
    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.3.3
    iconOceanBase Database
    SQL - V 4.3.3
    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

    CURSOR expressions

    Last Updated:2024-12-02 03:48:26  Updated
    share
    What is on this page
    Syntax
    Limitations
    Examples

    folded

    share

    A CURSOR expression returns a nested cursor. An expression in this form is equivalent to the PL REF CURSOR and can be passed to a function as a REF CURSOR argument.

    Syntax

    CURSOR (subquery)
    

    When the CURSOR expression is evaluated, a nested cursor is implicitly opened. For example, if a CURSOR expression appears in the select list, a nested cursor is opened each time the query fetches a row.

    A nested cursor is closed only in the following cases:

    • The nested cursor is explicitly closed by the user.

    • The parent cursor is re-executed.

    • The parent cursor is closed.

    • The parent cursor is canceled.

    • An error occurred during the fetch on one of its parent cursors. In this case, the nested cursor is closed as part of the clean-up.

    Limitations

    A CURSOR expression has the following limitations:

    • If the enclosing statement is not a SELECT statement, the nested cursor can only be used as a procedural REF CURSOR argument.

    • If the enclosing statement is a SELECT statement, the nested cursor can also appear in the outermost select list of the query specification or the outermost select list of another nested cursor.

    • Nested cursors cannot appear in views.

    • You cannot perform BIND and EXECUTE operations on nested cursors.

    Examples

    • Use the CURSOR expression in the select list of a query.

      obclient> CREATE TABLE dept(  
          deptno           NUMBER(2,0),  
          dname            VARCHAR(15),  
          location         VARCHAR(20),   
         CONSTRAINT pk_dept PRIMARY KEY(deptno)  
      );
      Query OK, 0 rows affected
      
      obclient> CREATE TABLE emp(  
         empno         NUMBER(4,0),  
         empname       VARCHAR(10),  
         job           VARCHAR(10),  
         mgr           NUMBER(4,0),  
         hiredate      DATE,  
         sal           NUMBER(7,2),  
         comm          NUMBER(7,2),        
         deptno        NUMBER(2,0),   
         CONSTRAINT PK_emp PRIMARY KEY (empno)
      );
      Query OK, 0 rows affected
      
      obclient> INSERT INTO dept VALUES (10,'ACCOUNTING','Los Angeles')
          ,(30,'OPERATIONS','CHICAGO')
          ,(40,'SALES','NEW YORK');
      Query OK, 3 rows affected
      Records: 3  Duplicates: 0  Warnings: 0
      
      obclient> INSERT INTO emp VALUES (1839,'KING','PRESIDENT',null, '04-JAN-20',5000,3,10)
          ,(1698,'BLAKE','MANAGER',1839,'01-MAY-1981',2850,2,30)
          ,(1782,'CLARK', 'MANAGER',1839, '09-JUN-81', 2450,2,10)
          ,(1566,'JONES','MANAGER',1839, '02-APR-81',2975,2,40)
          ,(1788,'SCOTT','ANALYST',1566, '15-JUL-87',3000,1,20)
          ,(1369,'SMITH','CLERK',1902,'17-OCT-80',800,1,20);
      Query OK, 6 rows affected
      Records: 6  Duplicates: 0  Warnings: 0
      
      obclient> SELECT dname, CURSOR(SELECT sal comm FROM emp e WHERE e.deptno= d.deptno)
         FROM dept d ORDER BY dname;
      +------------+-----------------------------------------------------+
      | DNAME      | CURSOR(SELECTSALCOMMFROMEMPEWHEREE.DEPTNO=D.DEPTNO) |
      +------------+-----------------------------------------------------+
      | ACCOUNTING | -1                                                  |
      | OPERATIONS | -1                                                  |
      | SALES      | -1                                                  |
      +------------+-----------------------------------------------------+
      3 rows in set
      
    • Use the CURSOR expression as a function argument. Delete all employees of a department from the emp table, and delete departments with no employees from the dept table.

      obclient> DECLARE
               v_dept_id emp.deptno%TYPE := 10;
           BEGIN
               DELETE FROM emp WHERE deptno=v_dept_id;
               IF SQL%NOTFOUND THEN
                   DELETE FROM dept WHERE deptno=v_dept_id;
               END IF;
      END;
      /
      Query OK, 0 rows affected
      
      obclient> SELECT * FROM emp;
      +-------+---------+---------+------+-----------+------+------+--------+
      | EMPNO | EMPNAME | JOB     | MGR  | HIREDATE  | SAL  | COMM | DEPTNO |
      +-------+---------+---------+------+-----------+------+------+--------+
      |  1369 | SMITH   | CLERK   | 1902 | 17-OCT-80 |  800 |    1 |     20 |
      |  1566 | JONES   | MANAGER | 1839 | 02-APR-81 | 2975 |    2 |     40 |
      |  1698 | BLAKE   | MANAGER | 1839 | 01-MAY-81 | 2850 |    2 |     30 |
      |  1788 | SCOTT   | ANALYST | 1566 | 15-JUL-87 | 3000 |    1 |     20 |
      +-------+---------+---------+------+-----------+------+------+--------+
      4 rows in set
      

    Previous topic

    Column expressions
    Last

    Next topic

    Datetime expressions
    Next
    What is on this page
    Syntax
    Limitations
    Examples