Introduction to Dynamic Execution

Anonymous JavaScript code snippets can be executed via the DBMS_MLE PL/SQL package.

The procedure DBMS_MLE.eval() is used to execute dynamic MLE snippets. The procedure takes the following arguments:

Argument Name Type Optional?
CONTEXT_HANDLE RAW(16) N
LANGUAGE_ID VARCHAR2(64) N
SOURCE CLOB N
RESULT CLOB Y
SOURCE_NAME VARCHAR2 Y

The argument SOURCE_NAME is optionally used to provide a name for the otherwise randomly-named JavaScript code block.

JavaScript code can be provided inline with PL/SQL as shown in the following code:

SET SERVEROUTPUT ON;

DECLARE
    l_ctx DBMS_MLE.context_handle_t;
    l_jscode CLOB;
BEGIN
    l_ctx := DBMS_MLE.create_context;
    l_jscode := q'~
        console.log('Hello World, this is DBMS_MLE')
    ~';
    DBMS_MLE.eval(
        context_handle => l_ctx,
        language_id => 'JAVASCRIPT',
        source => l_jscode,
        source_name => 'My JS Snippet'
    );
END;
/

Executing this example will result in the following being printed:

Hello World, this is DBMS_MLE

The code provided above demonstrates the following concepts of invoking JavaScript code dynamically:

  • An execution context must be explicitly created

  • JavaScript code is provided as a Character Large Object (CLOB) or VARCHAR2 variable

  • The context must be explicitly evaluated

Both PL/SQL and JavaScript are present when you execute JavaScript dynamically. The code snippets provided are not reusable outside of their namespace. The output of the call to console.log is passed to DBMS_OUTPUT for printing on the screen.

See Also: