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.6.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.6.0
    iconOceanBase Database
    SQL - V 4.6.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

    Subprograms

    Last Updated:2026-05-07 11:26:24  Updated
    share
    What is on this page
    Subprogram structure

    folded

    share

    A subprogram is a PL unit that contains many SQL and PL statements to solve a specific problem or perform a set of related tasks.

    Applicability

    This topic applies only to OceanBase Database Enterprise Edition. OceanBase Database Community Edition does not support this feature.

    A subprogram can have parameters, whose values are provided by the caller. A subprogram can be a stored procedure or a function. Typically, a stored procedure is used to perform an operation, and a function is used to perform a calculation and return a value.

    A stored subprogram is a subprogram stored in the database. It implements complex logic operations for many different database applications. Stored subprograms are divided into three types:

    • Standalone subprograms, which are created in a schema.

    • Subprograms in packages, which are created in the package body.

    • Nested subprograms, which are created in a PL block.

    Standalone subprograms are convenient for testing program logic, but managing a large number of standalone subprograms is inconvenient. Therefore, after the program logic is determined, standalone subprograms are recommended to be placed in different packages according to business modules.

    Subprograms are an important part of other maintainable features, such as packages and triggers.

    Subprogram structure

    A subprogram starts with a subprogram title, which specifies its name and parameter list (optional).

    The structure of a subprogram is consistent with that of a PL block, including:

    • Declaration part (optional)

      The declaration part includes the declarations of types, constants, variables, exceptions, explicit cursors, and nested subprograms. These items are local to the subprogram and are no longer available after the subprogram execution ends.

    • Execution part (required)

      The execution part includes assignment statements, control statements, and data manipulation statements.

    • Exception handling part (optional)

      The exception handling part includes code to handle exceptions (runtime errors).

    Adding comments to a subprogram improves the readability of the program. Comments can be placed anywhere in the subprogram and will be ignored by the compiler. Single-line comments start with two hyphens (--) and extend to the end of the line. Multi-line comments start with a slash and an asterisk (/*) and end with an asterisk and a slash (*/).

    The structure of a stored procedure is as follows:

    PROCEDURE name [ ( parameter_list ) ]
    { IS | AS }
    [ declarative_part ]
    BEGIN -- executable part begins
    statement; [ statement; ]...
    [ EXCEPTION ]
    exception_handler; [ exception_handler; ]... ]
    END;
    

    The structure of a function is similar to that of a stored procedure, but it includes at least one RETURN clause:

    FUNCTION name [ ( parameter_list ) ] RETURN data_type [ clauses ]
    { IS | AS }
    [ declarative_part ]
    BEGIN
    -- At least one RETURN statement
    statement; [ statement; ]...
    [ EXCEPTION ]
    exception_handler; [ exception_handler; ]... ]
    END;
    

    The code between IS | AS in a stored procedure or function is the declaration of the subprogram, which includes the declaration part, execution part, and exception handling part.

    Previous topic

    PL blocks
    Last

    Next topic

    Error reporting functions
    Next
    What is on this page
    Subprogram structure