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

    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.2.0
    iconOceanBase Database
    SQL - V 4.2.0
    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

    COUNT method

    Last Updated:2023-10-31 11:17:12  Updated
    share
    What is on this page
    Usage in varrays
    Usage in nested tables

    folded

    share

    You can use the COUNT method to return the number of elements in a collection, where deleted elements are ignored even if the DELETE method retains placeholders for them.

    Applicability

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

    Usage in varrays

    For a variable-size array (or varray), COUNT is always equivalent to LAST. If the EXTEND or TRIM method is used to increase or decrease the size of a varray, the value of COUNT changes accordingly.

    obclient> DECLARE
      TYPE oblist IS VARRAY(10) OF INTEGER;
      t oblist := OBLIST(2,4,6,8,10);
    
      PROCEDURE print_count_and_last IS
      BEGIN
        DBMS_OUTPUT.PUT('t.COUNT = ' || t.COUNT || ', ');
        DBMS_OUTPUT.PUT_LINE('t.LAST = ' || t.LAST);
      END  print_count_and_last;
    
    BEGIN
      print_count_and_last;
    
      t.EXTEND(2);
      print_count_and_last;
    
      t.TRIM(4);
      print_count_and_last;
    END;
    /
    Query OK, 0 rows affected  
    
    t.COUNT = 5, t.LAST = 5
    t.COUNT = 7, t.LAST = 7
    t.COUNT = 3, t.LAST = 3
    

    The preceding example declares a varray variable and initializes and assigns values to four elements. After EXTEND(3) and TRIM(5) are executed, the COUNT and LAST values of the varray are output.

    Usage in nested tables

    For a nested table, COUNT is equivalent to LAST. The value of COUNT is smaller than that of LAST unless intermediate elements are deleted from the nested table.

    obclient> DECLARE
      TYPE oblist IS TABLE OF INTEGER;
      t oblist := OBLIST(2,4,6,8,10);
    
      PROCEDURE print_count_and_last IS
      BEGIN
        DBMS_OUTPUT.PUT('t.COUNT = ' || t.COUNT || ', ');
        DBMS_OUTPUT.PUT_LINE('t.LAST = ' || t.LAST);
      END  print_count_and_last;
    
    BEGIN
      print_count_and_last;
    
      t.EXTEND(2);     -- Add two null elements at the end.
      print_count_and_last;
    
      t.DELETE(3);     -- Delete the third element.
      print_count_and_last;
    
      FOR i IN 1..8 LOOP
        IF t.EXISTS(i) THEN
          IF t(i) IS NOT NULL THEN
            DBMS_OUTPUT.PUT_LINE('t(' || i || ') = ' || t(i));
          ELSE
            DBMS_OUTPUT.PUT_LINE('t(' || i || ') = NULL');
          END IF;
        ELSE
          DBMS_OUTPUT.PUT_LINE('t(' || i || ') does not exist');
        END IF;
      END LOOP;
    END;
    /
    Query OK, 0 rows affected  
    
    t.COUNT = 5, t.LAST = 5
    t.COUNT = 7, t.LAST = 7
    t.COUNT = 6, t.LAST = 7
    t(1) = 2
    t(2) = 4
    t(3) does not exist
    t(4) = 8
    t(5) = 10
    t(6) = NULL
    t(7) = NULL
    t(8) does not exist
    

    The preceding example declares a nested table variable, initializes and assigns values to five elements, adds two null elements at the end, deletes the third element, and returns the COUNT and LAST values of the nested table. Finally, the states of elements 1 to 8 are output.

    Previous topic

    FIRST and LAST methods
    Last

    Next topic

    LIMIT method
    Next
    What is on this page
    Usage in varrays
    Usage in nested tables