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

    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.1
    iconOceanBase Database
    SQL - V 4.3.1
    Databases
    • OceanBase Database
    • OceanBase Cloud
    • OceanBase Tugraph
    • Interactive Tutorials
    • OceanBase Best Practices
    Tools
    • OceanBase Cloud Platform
    • OceanBase Migration Service
    • OceanBase Developer Center
    • OceanBase Migration Assessment
    • OceanBase Admin Tool
    • OceanBase Loader and Dumper
    • OceanBase Deployer
    • Kubernetes operator for OceanBase
    • OceanBase Diagnostic Tool
    • OceanBase Binlog Service
    Connectors and Middleware
    • OceanBase Database Proxy
    • Embedded SQL in C for OceanBase
    • OceanBase Call Interface
    • OceanBase Connector/C
    • OceanBase Connector/J
    • OceanBase Connector/ODBC
    • OceanBase Connector/NET
    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

    Create a program package

    Last Updated:2026-04-15 08:25:15  Updated
    Share
    What is on this page
    Create a package
    Examples
    Call functions and procedures of a package

    folded

    Share

    A program package consists of a package specification and a package body. The definition of a program package consists of two parts: definition of the package specification and definition of the package body.

    Applicability

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

    The package specification declares the public elements of the package, such as the data types, variables, constants, cursors, subprograms, and exceptions. The package body is the specific implementation of the package specification, and defines the cursors and programs declared in the package specification. The package body can also declare private elements of the package.

    The package specification and package body are compiled separately and stored as two separate objects in the database dictionary views USER_SOURCE, ALL_SOURCE, and DBA_SOURCE.

    Create a package

    The syntax for creating a package specification is as follows:

    CREATE [OR REPLACE] PACKAGE package_name
    [AUTHID {CURRENT_USER | DEFINER}]
    {IS | AS}
    [public_type_declarations[public_type_declarations]...]
    [public_cursor_declarations[public_cursor_declarations]...]
    [public_variable_declarations[public_variable_declarations]...]
    [function_declarations[function_declarations]...]
    [procedure_specifications[procedure_specifications]...]
    
    END [package_name]
    

    The AUTHID CURRENT_USER and AUTHID DEFINER options describe the privileges used by the application to call the function.

    The syntax for creating a package body is as follows:

    CREATE [OR REPLACE] PACKAGE BODY package_name  
    {AS|IS}
      [private_type_declarations[private_type_declarations]...]
      [private_variable_declarations[private_variable_declarations]...]
      [private_function_declarations[function_declarations]...]
      [private_procedure_specifications[procedure_specifications]...]
      [public_cursor_declarations[public_cursor_declarations]...]
      [public_function_declarations[function_declarations]...]
      [public_procedure_specifications[procedure_specifications]...]
    
    BEGIN
       PL Statement
    END [package_name]
    

    Examples

    The following example creates a package named obdemo_pack, which contains the obDeptRec record variable, two functions, and a stored procedure:

    obclient> CREATE TABLE obdept(  
        deptno         NUMBER(10,0),  
        dname          VARCHAR(15),  
        loc            VARCHAR(20)
      );
    Query OK, 0 rows affected
    
    obclient> CREATE OR REPLACE PACKAGE obdemo_pack
         IS
            obDeptRec obdept %ROWTYPE;
            FUNCTION add_obdept(dept_no NUMBER, dept_name VARCHAR2, location VARCHAR2)
              RETURN NUMBER;
            FUNCTION remove_obdept(dept_no NUMBER)
                RETURN NUMBER;
            PROCEDURE query_obdept(dept_no IN NUMBER);
         END obdemo_pack;
         /
    Query OK, 0 rows affected
    

    The following example creates a package body that implements the declared package specification:

    obclient> CREATE OR REPLACE PACKAGE BODY obdemo_pack
            IS
        FUNCTION add_obdept(dept_no NUMBER, dept_name VARCHAR2, location VARCHAR2)
            RETURN NUMBER
            IS
            deptno_remaining EXCEPTION;
               PRAGMA EXCEPTION_INIT(deptno_remaining, -1);  -- -1 is the error code returned when the unique constraint condition is violated.
        BEGIN
            INSERT INTO obdept VALUES(dept_no, dept_name, location);
            IF SQL%FOUND THEN
                RETURN 1;
            END IF;
        EXCEPTION
               WHEN deptno_remaining THEN
                RETURN 0;
            WHEN OTHERS THEN
                RETURN -1;
        END add_obdept;
    
        FUNCTION remove_obdept(dept_no NUMBER)
            RETURN NUMBER
            IS
        BEGIN
        DELETE FROM obdept WHERE deptno=dept_no;
           IF SQL%FOUND THEN
                RETURN 1;
            ELSE
                RETURN 0;
           END IF;
        EXCEPTION
        WHEN OTHERS THEN
            RETURN -1;
        END remove_obdept;
    
        PROCEDURE query_obdept (dept_no IN NUMBER)
            IS
        BEGIN
        SELECT * INTO obDeptRec FROM obdept WHERE deptno=dept_no;
        EXCEPTION
               WHEN NO_DATA_FOUND THEN  
                  DBMS_OUTPUT.PUT_LINE('No department numbered '||dept_no||' in the database.');
               WHEN TOO_MANY_ROWS THEN
                  DBMS_OUTPUT.PUT_LINE('Program runtime error!')
               WHEN OTHERS THEN
                   DBMS_OUTPUT.PUT_LINE(SQLCODE||'----'||SQLERRM);
        END query_obdept;
         BEGIN
                 Null;
         END obdemo_pack;
         /
    Query OK, 0 rows affected
    

    In OceanBase Database, you can create synonyms for program packages. Here is an example:

    obclient> CREATE OR REPLACE SYNONYM syn_pag FOR obdemo_pack;
    Query OK, 0 rows affected
    
    obclient> CALL syn_pag.query_obdept('123');
    Query OK, 0 rows affected
    

    Call functions and procedures of a package

    To call the variables, constants, functions, and methods in a program package, you need to include the package name in the element name and separate the names with a period (.). Specifically, a public element in a package is called in the format of package name.element name.

    The following example calls the functions in the obdemo_pack package to add data to, query, and modify the obdept table, and uses the obDeptRec record variable in the obdemo_pack package to display the query result:

    obclient> DECLARE
             Var NUMBER;
         BEGIN
             Var := obdemo_pack.add_obdept(900,'Administration', 'Beijing');
             IF var =-1 THEN
                 DBMS_OUTPUT.PUT_LINE(SQLCODE||'----'||SQLERRM);
             ELSIF var =0 THEN
                 DBMS_OUTPUT.PUT_LINE('The department record already exists!') ');
             ELSE
                 DBMS_OUTPUT.PUT_LINE('Record added!') ');
                 obdemo_pack.query_obdept(900);
                 DBMS_OUTPUT.PUT_LINE(obdemo_pack.obDeptRec.deptno||'---'||obdemo_pack.obDeptRec.dname||'---'||obdemo_pack.obDeptRec.loc);
                 var := obdemo_pack.remove_obdept(900);
                 IF var =-1 THEN
                     DBMS_OUTPUT.PUT_LINE(SQLCODE||'----'||SQLERRM);
                 ELSIF var=0 THEN
                     DBMS_OUTPUT.PUT_LINE('The department record does not exist!') ');
                 ELSE
                     DBMS_OUTPUT.PUT_LINE('Record deleted!') ');
                 END IF;
             END IF;
         END;
         /
    Query OK, 0 rows affected
    
    Record added!
    900---Administration---Beijing
    Record deleted!
    

    Previous topic

    Overview
    Last

    Next topic

    Overloaded programs in a package
    Next
    What is on this page
    Create a package
    Examples
    Call functions and procedures of a package