DEV Community

Rajesh Vohra
Rajesh Vohra

Posted on

Beginner-Friendly PL/SQL and JavaScript MLE Examples for Oracle 26ai

The Multilingual Engine (MLE) in Oracle 26ai opens a whole new world for developers by allowing JavaScript to run natively inside the database, right alongside traditional PL/SQL. For beginners, this means you can combine the simplicity of JavaScript with the power of Oracle’s SQL engine to build cleaner logic, faster transformations, and more flexible procedures. The examples below walk through easy, practical use cases—from formatting text and doing basic math to running SQL queries and returning JSON—all designed to help new users quickly understand how PL/SQL and JavaScript work together through MLE.
Here are few simple and easy to understand examples:

1. Dynamic Execution of JavaScript (DBMS_MLE.eval)

Use Case: Print "Hello World" dynamically using JavaScript from PL/SQL.

SET SERVEROUTPUT ON;

DECLARE
l_ctx DBMS_MLE.context_handle_t;
l_jscode CLOB;
BEGIN
-- Create a dynamic execution context
l_ctx := DBMS_MLE.create_context;

-- JavaScript code stored in a CLOB
l_jscode := q'~
    console.log('Hello World, dynamically from JavaScript!');
~';

-- Execute JavaScript dynamically
DBMS_MLE.eval(
    context_handle => l_ctx,
    language_id    => 'JAVASCRIPT',
    source         => l_jscode,
    source_name    => 'DynamicHelloWorld'
);

-- Drop the context to free resources
DBMS_MLE.drop_context(l_ctx);
Enter fullscreen mode Exit fullscreen mode

END;
/

What happens:

A new execution context is created.

JavaScript code is executed dynamically.

Output is printed via DBMS_OUTPUT.

Context is explicitly dropped.

2. Using JavaScript Modules with PL/SQL (MLE Module Calls)

Use Case: Encapsulate reusable logic in a module and call it from PL/SQL.

Step 1: Create a JavaScript Module
CREATE OR REPLACE MLE MODULE hello_module
LANGUAGE JAVASCRIPT AS
function sayHello(name) {
console.log(Hello, ${name} from JS module!);
}
export { sayHello };
/

Step 2: Create a PL/SQL Procedure Call Specification
CREATE OR REPLACE PROCEDURE hello_proc(p_name VARCHAR2)
AS MLE MODULE hello_module
SIGNATURE 'sayHello(text)';
/

Step 3: Call the Procedure in PL/SQL
BEGIN
hello_proc('Alice');
END;
/

What happens:

hello_module stores reusable JS code.

hello_proc exposes JS function to PL/SQL.

Output is printed via DBMS_OUTPUT.

3. PURE Execution for Secure JavaScript

Use Case: Run computational code without accessing database state.

Step 1: Create a PURE JavaScript Module
CREATE OR REPLACE MLE MODULE pure_module
LANGUAGE JAVASCRIPT AS
export function addNumbers(a, b) {
console.log(Sum is: ${a + b});
}
/

Step 2: Create a PURE Environment
CREATE OR REPLACE MLE ENV pure_env
IMPORTS('pure_module' MODULE pure_module) PURE;
/

Step 3: Create a Call Specification Using PURE Environment
CREATE OR REPLACE PROCEDURE add_numbers_proc(p_a NUMBER, p_b NUMBER)
AS MLE MODULE pure_module ENV pure_env
SIGNATURE 'addNumbers(num,num)';
/

Step 4: Execute the Procedure
BEGIN
add_numbers_proc(10, 20);
END;
/

What happens:

Code runs isolated from database objects.

Ideal for running third-party libraries or computations securely.

Only allows input/output operations.

Top comments (1)

Collapse
 
sell_items_b43085827df4a2 profile image
Sell Items

Thanks for making it simple.