创建 Oracle SQL Java 函数不起作用

Creating Oracle SQL Java Function does not work

当尝试创建 oracle SQL java 函数以生成随机 UUID 时 in this Whosebug answer,我收到以下错误:

sql> CREATE or REPLACE FUNCTION random_uuid
   RETURN VARCHAR2
   AS LANGUAGE JAVA NAME 'java.util.UUID.randomUUID() return java.lang.String'
[2019-01-29 09:28:29] [99999][17110] Warning: execution completed with warning
[2019-01-29 09:28:29] completed in 23 ms
[2019-01-29 09:28:29] 3:78:PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
[2019-01-29 09:28:29] ;
[2019-01-29 09:28:29] The symbol ";" was substituted for "end-of-file" to continue.

即使这只是一个警告,之后调用它也不起作用:

sql> select random_uuid() from dual
[2019-01-29 09:36:28] [65000][6575] ORA-06575: Package or function RANDOM_UUID is in an invalid state

我使用 IntelliJ IDEA 的数据库控制台来执行脚本。怎么了?

编辑:我的原始脚本末尾包含分号:

create or replace function random_uuid return varchar2 as
  language java
  name 'java.util.UUID.randomUUID() return String';

添加“;”在 String 之后没有帮助:

create or replace function random_uuid return varchar2 as
  language java
  name 'java.util.UUID.randomUUID() return String;';

由于问题似乎是 IDE 无法识别语句终止的位置,那么您可以尝试将其包装在 PL/SQL 匿名块中并使用 EXECUTE IMMEDIATE 强制它正确解析语句:

BEGIN
  EXECUTE IMMEDIATE 'CREATE OR REPLACE FUNCTION RandomUUID RETURN VARCHAR2 AS LANGUAGE JAVA NAME ''java.util.UUID.randomUUID() return java.lang.String'';';
END;
/