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

    Syntax of PL stored programs

    Last Updated:2024-12-02 03:48:26  Updated
    share
    What is on this page
    BEGIN ... END block
    DECLARE statement
    Statement labels

    folded

    share

    The syntax of a PL stored program contains three parts: the BEGIN ... END block, the DECLARE statement, and the statement label.

    BEGIN ... END block

    The BEGIN ... END block supports compound statements and is used to write stored programs, including stored procedures, stored functions, and triggers.

    The BEGIN ... END block can contain multiple statements that are enclosed by using the BEGIN and END keywords. The syntax is as follows:

    [begin_label:] BEGIN
        [statement_list]
    END [end_label]
    

    In the preceding syntax, statement_list specifies a list of one or more statements to be executed. Each statement in the list must end with a semicolon (;). statement_list is optional. Therefore, the empty compound statement BEGIN END is valid.

    BEGIN ... END blocks can be nested. A BEGIN ... END block can also be labeled. For more information, see Statement labels.

    The default delimiter of PL stored programs is "/". You can also use the delimiter command to define a delimiter so that the entire definition is transferred to the server as a single statement. A delimiter can consist of a single character or multiple characters. Do not use the backslash (\) as a delimiter because it is usually considered as an escape character. Here is an example:

    DELIMITER $$
    
    DELIMITER //
    

    Note

    When you use OceanBase Database for development, you must define a delimiter. If the internal definition of a PL block does not contain statements separated by semicolons (;), no delimiter is required.

    DECLARE statement

    The DECLARE statement is used to define local variables, exception handlers, and cursors for a program. For more information, see Variables in stored programs, Exception handling, and Cursors.

    DECLARE can be used only in a BEGIN ... END block and must be used at the beginning of the block or prior to other statements.

    Sequence of declarations:

    • Cursor declarations must be prior to exception handler declarations.

    • Variable and condition declarations must be prior to the cursor or exception handler declarations.

    Statement labels

    Labels can be used in the LOOP, REPEAT, and WHILE statements of a BEGIN ... END block. The syntax is as follows:

    [begin_label:] BEGIN
        [statement_list]
    END [end_label]
    
    [begin_label:] LOOP
        statement_list
    END LOOP [end_label]
    
    [begin_label:] REPEAT
        statement_list
    UNTIL search_condition
    END REPEAT [end_label]
    
    [begin_label:] WHILE search_condition DO
        statement_list
    END WHILE [end_label]
    

    Observe the following rules for using the labels of these statements:

    • begin_label must be followed with a colon (:).

    • begin_label can be provided without end_label. If end_label is provided, it must be the same as begin_label.

    • end_label cannot be provided without begin_label.

    • Labels of the same nesting level must be unique.

    • A label can contain a maximum of 16 characters in length.

    To reference a built label, use the ITERATE or LEAVE statement. The following example uses a labeled statement to continue iteration or end the loop:

    obclient> DELIMITER //
    
    obclient> CREATE PROCEDURE doiterate()
        BEGIN
          DECLARE p1 INT DEFAULT 0;
          label1: LOOP
            SET p1 = p1 + 1;
            IF p1 < 10 THEN ITERATE label1; END IF;
            LEAVE label1;
          END LOOP label1;
        END //
    Query OK, 0 rows affected
    

    For more information about the declaration of in-block exception handlers, see DECLARE ... HANDLER.

    Previous topic

    PL architecture
    Last

    Next topic

    Overview
    Next
    What is on this page
    BEGIN ... END block
    DECLARE statement
    Statement labels