Inline JavaScript Procedures
Version | |
Categories |
Inlined JavaScript procedures allow you to embed JavaScript code directly in the CREATE FUNCTION
and CREATE PROCEDURE
statements without the need of creating a module first.
If you want to implement a less complex JavaScript feature quickly, inlined procedures and functions are a good choice. The following example coverts seconds-since-epoch to an Oracle Date.
create or replace function epoch_to_Date (
P_EPOCH number
) return date
as mle language javascript
q'~
let d = new Date(0);
d.setUTCSeconds(P_EPOCH);
return d;
~';
/
select
to_char(
epoch_to_date(1684758614),
'yyyy-mm-dd hh24:mi:ss'
) the_date;
Result
SQL> create or replace function epoch_to_date (
2 P_EPOCH number
3 ) return date
4 as mle language javascript
5 q'~
6 let d = new Date(0);
7 d.setUTCSeconds(P_EPOCH);
8
9 return d;
10 ~';
11 /
Function created.
SQL>
SQL> select
2 to_char(
3 epoch_to_date(1684758614),
4 'yyyy-mm-dd hh24:mi:ss'
5 ) the_date;
THE_DATE
-------------------
2023-05-22 12:30:14
Benefits
Inline JavaScript functions and procedures are a convenient way of exposing functionality in JavaScript to SQL and PL/SQL. You aren’t limited to using built-in JavaScript objects, you are free to perform any manipulation you like. If more complex processing, including importing 3rd party JavaScript modules is required you should use modules and environments instead.