我可以在 JPA 2 的本机查询中包含多个语句吗

Ryt*_*mic 6 java sql oracle plsql jpa

我想执行以下查询来有条件地删除临时表。

String dropSql = "BEGIN EXECUTE IMMEDIATE 'DROP TABLE " + tmpTblName + "';" +
    " EXCEPTION" +
    " WHEN OTHERS THEN" +
    " IF SQLCODE!=-942 THEN" +
    " RAISE;" +
    " END IF;" +
    " END";
Run Code Online (Sandbox Code Playgroud)

然后将其作为本机查询执行,如下所示:

JPA.em().createNativeQuery(dropSql)
            .executeUpdate();
Run Code Online (Sandbox Code Playgroud)

但是,JPA 不会让我这样做,或者更确切地说,oracle db 不会。显然;以某种方式逃避或误解。

Caused by: java.sql.SQLException: ORA-06550: line 1, column 120:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:

; <an identifier> <a double-quoted delimited-identifier>
The symbol ";" was substituted for "end-of-file" to continue.
Run Code Online (Sandbox Code Playgroud)

我是否必须忍受使用单独的查询来实现我的目标,还是有一些技巧?

Mah*_*kar 5

你在END块最后漏掉了一个分号。

String dropSql = "BEGIN EXECUTE IMMEDIATE 'DROP TABLE " + tmpTblName + "';" +
    " EXCEPTION" +
    " WHEN OTHERS THEN" +
    " IF SQLCODE!=-942 THEN" +
    " RAISE;" +
    " END IF;" +
    " END;";
Run Code Online (Sandbox Code Playgroud)

  • 你是对的。好像 jpa 不一致。当只有一个语句时,末尾不应有分号。 (2认同)