Overview
A function is a subprogram defined in a database that you can call from SQL statements. If the built-in functions do not meet your needs, you can create new user-defined functions in OceanBase Developer Center (ODC). User-defined functions allow you to perform specific calculations and operations. They also reduce repetitive code and improve program readability.
A function is a procedural database object similar to a stored procedure. Like stored procedures, functions are code snippets composed of SQL and procedural statements that can be called by applications and other SQL statements.
The differences between user-defined functions and stored procedures are as follows:
A function must return exactly one value, which makes it ideal for processing data and returning a single result. A stored procedure can return zero or more values, which makes it suitable for operations such as bulk inserts and batch updates.
You can call a function directly with a
SELECTstatement. You must use aCALLstatement to call a stored procedure.
As shown in the figure above, the process of creating a function involves the following 6 steps:
2. Select a data type for the return value.
4. Confirm the function information.
5. Edit the function statement.
6. Finish creating the function.
This topic provides an example of how to create an employee function named `function_emp` in ODC. The `function_emp` function includes an `id` parameter of the `INT` type and retrieves an employee's name based on the specified ID.
The data in this topic is for demonstration purposes only. You must replace it with your own data.
Procedure
Step 1: Enter a function name
In OceanBase Developer Center (ODC), click a connection name to open the connection. In the navigation pane on the left, click the Functions tab to view the list of functions. To create a function, click + in the upper-right corner of the function list, or click New in the top navigation bar of ODC.

Step 2: Select a data type for the return value
For more information about data types, see OceanBase Database Reference Guide.
Step 3: Add parameters
Parameters are the information passed to a function when it is called. You need to specify the following parameter information:
In Oracle mode, specify the Name, Mode, Type, and Default Value for each parameter.
In MySQL mode, specify the Name, Type, and Length for each parameter.
The following three operations are available for adding a function:
Feature
Description
Pop-up guide menu
Use the pop-up guide menu to add, delete, or move parameters up or down.
Click the row number
Click a row number to select the entire row. A context menu appears with options to delete or move the row.
Click and select a row number to drag the entire row to a new position.
Right-click operations
Drag to select an entire row. Then, right-click to copy the row or move it down.
Select a cell and right-click to copy it.
The Mode that you specify under Parameters defines the parameter type.
MySQL mode does not support setting parameter modes. Oracle mode supports three parameter modes: IN (input), OUT (output), and INOUT (input/output).
Parameter
Type
IN
An input parameter. When the function is called, the value of the parameter is passed into the function for use during execution.
OUT
An output parameter. When the function is called, its initial value is ignored. The function can modify the parameter, and the new value is returned to the caller.
INOUT
An input/output parameter. It has the features of both an input parameter and an output parameter.
NoteIn Oracle mode, both functions and stored procedures support IN, OUT, and INOUT parameters. In MySQL mode, functions only support IN parameters, but stored procedures support IN, OUT, and INOUT parameters.
Step 4: Confirm the function creation
Click OK. The SQL creation confirmation page appears.
Step 5: Edit the function statement

On the SQL creation confirmation page, edit the function statement.
The toolbar on the editing page provides the following features:
Feature |
Description |
Format |
Formats the selected SQL statement or all statements in the current SQL window. This includes actions such as indenting, adding line breaks, and highlighting keywords. |
Find/Replace |
Searches the script for specified text. You can then replace the found text with new text. |
Undo |
Reverts the script to its state before the last operation. |
Redo |
After you use Undo, this re-applies the undone operation. |
Change Case |
Changes the case of the selected text. Options are ALL UPPERCASE, all lowercase, and Initial Caps. |
Indent |
Adds or removes indentation for the selected text. Options are Add Indent and Remove Indent. |
Comment |
Adds or removes comment syntax for the selected text. Options are Add Comment and Remove Comment. |
IN Value Conversion |
Converts a space-separated list, such as `A B`, to the format `('A','B')`. |
On the SQL creation confirmation page, you can edit the function statement. The syntax is as follows:
CREATE FUNCTION <function_name> ([ <parameter1> <type1> [ , <parameter2> <type2>] ] …)
RETURNS <type>
<function_body>
The following table describes the parameters:
Parameter |
Description |
<function_name> |
The name of the user-defined function.
Important
A user-defined function cannot have the same name as a stored procedure. |
<parameter><type> |
The parameters of the user-defined function. A parameter has only a name and a type. You cannot specify the `IN`, `OUT`, or `INOUT` keywords. |
RETURNS <type> |
Declares the data type of the function's return value.
Important
If a `RETURN VALUE` statement includes a `SELECT` statement, the `SELECT` statement must return a single row with a single column. |
<function_body> |
The main part of the function, also called the function body. The function body must include a |
Example:
CREATE FUNCTION `function_emp` ( `id` int(45)) RETURNS VARCHAR(300)
-- Function body begins
BEGIN
-- Declare a variable
DECLARE
a VARCHAR(300);
-- Assign a value to the variable
SELECT
name INTO a
FROM
employee
WHERE
emp_no = id;
-- Return value
RETURN a;
-- Function body ends
END
Step 6: Finish creating the function
Click Create. After the function is created, you can call it using the SELECT keyword, in the same way as you call a built-in function.
In the navigation pane on the left, you can right-click a function name in the function list to open a context menu. The menu provides options to manage the object, including View, New, Edit, Compile, Debug, Run, Download, Delete, and Refresh.
For more information, see Create a function and Introduction to common features.
Syntax:
SELECT <function_name> ([<parameter> [,...]])
Example:
SELECT function_emp(2);
MySQL mode

Oracle mode
ImportantIn Oracle mode, if a function includes an `OUT` parameter, you must call it using the
CALLstatement.A yellow
icon in the function list indicates an error or a warning.