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 & Certification
    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

    CREATE FUNCTION

    Last Updated:2026-05-07 11:26:24  Updated
    share
    What is on this page
    Purpose
    Prerequisites
    Syntax
    Semantics
    Examples

    folded

    share

    Purpose

    The CREATE FUNCTION statement is used to create or replace standalone functions or call specifications. A standalone function is a stored function (subprogram that returns a single value) in the database.

    Applicability

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

    Notice

    A standalone function created by using the CREATE FUNCTION statement is different from a function declared and defined in a PL block or package.

    Prerequisites

    You must have the CREATE FUNCTION system privilege to create or replace a standalone function in the current schema. You must have the CREATE ANY FUNCTION system privilege to create or replace a standalone function in another user's schema.

    Syntax

    The syntax of create_function is as follows:

    CREATE [ OR REPLACE ]
    FUNCTION plsql_function_source
    

    The syntax of plsql_function_source is as follows:

    [ schema. ] function_name
      [ ( parameter_declaration [, parameter_declaration]... ) ]
      RETURN datatype [ sharing_clause ]
      [ invoker_rights_clause
        | accessible_by_clause
        | DETERMINISTIC
        | parallel_enable_clause
        | result_cache_clause
        | default_collation_clause
        | pipelined_clause
        ]...
      { { AGGREGATE | PIPELINED }  USING [ schema. ] implementation_type
      | [ PIPELINED ] { IS | AS } { [ declare_section ] body
                                  | call_spec
                                  }
      } ;
    

    Semantics

    Syntax Keyword or syntax node Description
    create_function OR REPLACE Recreates the function (if it exists) and recompiles it. Users who were granted the function privilege before the function is redefined can still access the function without re-granting the privilege. If any function-based indexes depend on the function, the database marks the indexes as DISABLED.
    plsql_function_source schema The name of the schema in which the function is located. The default value is the current user's schema.
    plsql_function_source function_name The name of the function to be created.
    plsql_function_source RETURN datatype The data type of the function's return value. The return value can be any data type supported by PL.
    plsql_function_source body The executable part required for the function, and the exception handling part (optional).
    plsql_function_source declare_section The optional declaration part of the function. Declarations are local to the function and can be referenced in the body, but they no longer exist after the function completes execution.
    plsql_function_source DETERMINISTIC Indicates that the function is deterministic, meaning it always returns the same result for the same input parameters. This is important for optimizing query performance.
    plsql_function_source parallel_enable_clause Indicates that the function can be safely executed in parallel queries. When this feature is specified, the function must meet conditions such as being deterministic, not modifying the database state, and not depending on session state.

    Examples

    Create a function named get_salary_by_dept.

    CREATE OR REPLACE FUNCTION get_salary_by_dept(
           v_in_dept_id NUMBER,
           v_out_emp_count OUT NUMBER)
           RETURN NUMBER
       IS
           v_sum NUMBER;
       BEGIN
           SELECT SUM(salary), count(*) INTO v_sum, v_out_emp_count
               FROM employees WHERE deptno=v_in_dept_id;
           RETURN v_sum;
       EXCEPTION
          WHEN NO_DATA_FOUND THEN
             DBMS_OUTPUT.PUT_LINE('Dept id '||v_in_dept_id||' not found');
          WHEN OTHERS THEN
             DBMS_OUTPUT.PUT_LINE('ERR: '||SQLCODE||': '||SQLERRM);
       END get_salary_by_dept;
    /
    

    Create a function that can be safely executed in parallel queries.

    CREATE OR REPLACE FUNCTION sub_query_result(input VARCHAR)
    DETERMINISTIC
    PARALLEL_ENABLE
    RETURN VARCHAR
    IS
        result tp_sys_para.para_value%type;
    BEGIN
        SELECT tp.para_value INTO result
        FROM tp_sys_para tp
        WHERE tp.para_name_e = input;
        RETURN result;
    END;
    /
    

    Previous topic

    ALTER TYPE
    Last

    Next topic

    CREATE PACKAGE
    Next
    What is on this page
    Purpose
    Prerequisites
    Syntax
    Semantics
    Examples