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

    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.4.2
    iconOceanBase Database
    SQL - V 4.4.2
    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

    Scope of procedures

    Last Updated:2026-04-02 06:23:58  Updated
    share
    What is on this page
    Scope and rules for handlers
    Examples

    folded

    share

    Stored procedures can include handlers that are called when certain conditions occur within the procedure. The applicability of each handler depends on its position in the procedure definition and the one or more conditions it handles.

    Scope and rules for handlers

    The following rules apply to the scope of handlers in OceanBase Database:

    • Handlers declared in a BEGIN ... END block apply only to SQL statements that follow the handler declaration in the block. If a handler itself raises a condition, it cannot handle that condition, and no other handlers can be declared in the block. In the following example, handlers h1 and h2 apply to conditions raised by statements st1 and st2, respectively, but not to conditions raised within the bodies of h1 or h2.

      BEGIN -- outer block
        DECLARE EXIT HANDLER FOR ...;  -- handler h1
        DECLARE EXIT HANDLER FOR ...;  -- handler h2
        st1;
        st2;
      END;
      
    • A handler is valid only within the block in which it is declared and cannot be activated by a condition that occurs outside of that block. In the following example, handler h1 is valid within the scope of st1 in the inner block, but not within the scope of st2 in the outer block:

      BEGIN -- outer block
        BEGIN -- inner block
          DECLARE EXIT HANDLER FOR ...;  -- handler h1
          st1;
        END;
        st2;
      END;
      
    • A handler can be specific or general. A specific handler is associated with an error code, SQLSTATE value, or condition name. A general handler is associated with a condition in the SQLWARNING, SQLEXCEPTION, or NOT FOUND category. Condition specificity is related to condition priority.

      You can declare multiple handlers with different specificities in different scopes. For example, an outer block may have a specific error code handler, while an inner block may have a general SQLWARNING handler. Alternatively, a single block may have both specific error code and general SQLWARNING handlers.

    The activation of a handler depends not only on its scope and condition value but also on other handlers. When a condition occurs in a stored program, the server searches for applicable handlers in the current scope (the current BEGIN ... END block). If no applicable handler is found, it continues searching in the next outer scope (block). When the server finds one or more applicable handlers in the specified scope, it selects the handler based on condition priority. The condition priorities are as follows:

    • A handler for an error code has higher priority than a handler for an SQLSTATE value.

    • A handler for an SQLSTATE value has higher priority than a general handler for SQLWARNING, SQLEXCEPTION, or NOT FOUND.

    • A SQLEXCEPTION handler has higher priority than an SQLWARNING handler.

    • Multiple applicable handlers with the same priority can exist. For example, a statement can generate multiple warnings with different error codes, each with a specific handler. In this case, the server's choice of which handler to activate is indeterminate and may vary based on the situation.

    If multiple applicable handlers exist in different scopes, the handler in the local scope has higher priority than the handler in the outer scope, even if the outer scope handler has a higher priority for the specified condition.

    If no applicable handler exists when a condition occurs, the following actions are taken based on the condition's category:

    • For an SQLEXCEPTION condition, the stored program terminates at the statement that raised the condition, similar to using an EXIT handler. If the program is called by another stored program, the calling program uses its own selection rules to handle the condition.

    • For an SQLWARNING condition, the program continues execution, similar to using a CONTINUE handler.

    • For a NOT FOUND condition, if the condition is raised normally, the action is CONTINUE. If the condition is raised by SIGNAL or RESIGNAL, the action is EXIT.

    Examples

    Examples 1, 2, 3, and 4 show how the selection rules for handlers apply.

    Example 1 shows a procedure that contains two handlers: one for the specified SQLSTATE value ('42S02') that is generated when an attempt is made to drop a nonexistent table, and one for the general SQLEXCEPTION category.

    -- Example 1
    CREATE PROCEDURE proc1()
    BEGIN
      DECLARE CONTINUE HANDLER FOR SQLSTATE '42S02'
        SELECT 'SQLSTATE handler was activated' AS msg;
      DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
        SELECT 'SQLEXCEPTION handler was activated' AS msg;
    
      DROP TABLE test.tbl1;
    END;
    

    In Example 1, both handlers are declared in the same block and have the same scope. However, the SQLSTATE handler has priority over the SQLEXCEPTION handler. Therefore, if the table tbl1 does not exist, the DROP TABLE statement generates a condition that activates the SQLSTATE handler. As shown below:

    obclient> CALL proc1();
    +--------------------------------+
    | msg                            |
    +--------------------------------+
    | SQLSTATE handler was activated |
    +--------------------------------+
    1 row in set
    

    Example 2 shows a procedure that contains the same two handlers as in Example 1. However, the DROP TABLE statement and the SQLEXCEPTION handler are declared in an inner block.

    -- Example 2
    CREATE PROCEDURE proc2()
    BEGIN -- outer block
        DECLARE CONTINUE HANDLER FOR SQLSTATE '42S02'
          SELECT 'SQLSTATE handler was activated' AS msg;
      BEGIN -- inner block
        DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
          SELECT 'SQLEXCEPTION handler was activated' AS msg;
    
        DROP TABLE test.tbl1; -- occurs in the inner block
      END;
    END;
    

    In Example 2, the handler that is closer to the location where the condition occurs is activated first. Therefore, the SQLEXCEPTION handler is activated, even though it is more general than the SQLSTATE handler. As shown below:

    obclient> CALL proc2();
    +------------------------------------+
    | msg                                |
    +------------------------------------+
    | SQLEXCEPTION handler was activated |
    +------------------------------------+
    1 row in set
    

    Example 3 shows a procedure that contains one handler declared in a block that is within the scope of the DROP TABLE statement.

    -- Example 3
    CREATE PROCEDURE proc3()
    BEGIN -- outer block
      DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
        SELECT 'SQLEXCEPTION handler was activated' AS msg;
      BEGIN -- inner block
        DECLARE CONTINUE HANDLER FOR SQLSTATE '42S02'
          SELECT 'SQLSTATE handler was activated' AS msg;
      END;
    
      DROP TABLE test.tbl1; -- occurs in the outer block
    END;
    

    In Example 3, the SQLEXCEPTION handler is activated because the SQLSTATE value is not in the scope of the condition generated by the DROP TABLE statement. As shown below:

    obclient> CALL proc3();
    +------------------------------------+
    | msg                                |
    +------------------------------------+
    | SQLEXCEPTION handler was activated |
    +------------------------------------+
    1 row in set
    

    Example 4 shows a procedure that contains two handlers declared in a block that is within the scope of the DROP TABLE statement.

    -- Example 4
    CREATE PROCEDURE proc4()
    BEGIN -- outer block
      BEGIN -- inner block
        DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
          SELECT 'SQLEXCEPTION handler was activated' AS msg;
        DECLARE CONTINUE HANDLER FOR SQLSTATE '42S02'
          SELECT 'SQLSTATE handler was activated' AS msg;
      END;
    
      DROP TABLE test.tbl1; -- occurs in the outer block
    END;
    

    In Example 4, neither handler is activated because they are not in the scope of the DROP TABLE statement. The condition generated by the statement is not handled and the procedure terminates with an error. As shown below:

    obclient> CALL proc4();
    
    ERROR 1051 (42S02): Unknown table 'test.tbl1'
    

    Previous topic

    SIGNAL
    Last

    Next topic

    Diagnostic areas
    Next
    What is on this page
    Scope and rules for handlers
    Examples