我正在尝试使用 JdbcTemplate 从 oracle 匿名块读取值

发布于 2025-01-09 14:53:58 字数 698 浏览 0 评论 0原文

我正在尝试使用 JdbcTemplate 从 Oracle 匿名块读取值。这是我的 java 代码:

getJdbcTemplate().queryForObject(sql, Boolean.class);

这是 sql:

DECLARE
    CRS                SYS_REFCURSOR;

BEGIN

    OPEN CRS FOR SELECT CASE
                            WHEN
                                      1 > 0
                                THEN 1
                            ELSE 0
                            END
                 FROM DUAL;
END;

我收到此错误:

 SQL state [99999]; error code [17166]; Cannot perform fetch on a PLSQL statement: next; nested exception is java.sql.SQLException: Cannot perform fetch on a PLSQL statement: next

I am trying to read a value from oracle anonymous block using JdbcTemplate. Here is my java code:

getJdbcTemplate().queryForObject(sql, Boolean.class);

And here is the sql:

DECLARE
    CRS                SYS_REFCURSOR;

BEGIN

    OPEN CRS FOR SELECT CASE
                            WHEN
                                      1 > 0
                                THEN 1
                            ELSE 0
                            END
                 FROM DUAL;
END;

I am receiving this error:

 SQL state [99999]; error code [17166]; Cannot perform fetch on a PLSQL statement: next; nested exception is java.sql.SQLException: Cannot perform fetch on a PLSQL statement: next

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世界如花海般美丽 2025-01-16 14:53:58

公共T queryForObject(String sql, ClassrequiredType) throws DataAccessException 接受一个 SQL 查询,该查询将生成包含单行的结果集,并使用返回结果集中的该行构造所需类型的对象。

代码:

DECLARE
  CRS SYS_REFCURSOR;
BEGIN
  OPEN CRS FOR
    SELECT CASE WHEN 1 > 0 THEN 1 ELSE 0 END FROM DUAL;
END;

打开一个包含单行结果集的游标;但是,游标作为 PL/SQL 匿名块中的局部变量保存,并且永远不会“返回”到调用 PL/SQL 的应用程序代码。

传统的方法是在没有 PL/SQL 包装器的情况下使用 SQL:

String sql = "SELECT CASE WHEN 1 > 0 THEN 1 ELSE 0 END FROM DUAL";
getJdbcTemplate().queryForObject(sql, Boolean.class);

您可以尝试使用 PL/SQL 块中的 DBMS_SQL.RETURN_RESULT ,但这仅在 Oracle 12 中可用,并且没有相同的功能传统 SQL 查询的支持级别,因此它可能不起作用:

DECLARE
  crs SYS_REFCURSOR;
BEGIN
  OPEN crs FOR
    SELECT CASE WHEN 1 > 0 THEN 1 ELSE 0 END FROM DUAL;
  DBMS_SQL.RETURN_RESULT(crs);
END;

public <T> T queryForObject(String sql, Class<T> requiredType) throws DataAccessException takes an SQL query that will generate a result set with a single row and constructs an object of the required type using that row from the returned result set.

The code:

DECLARE
  CRS SYS_REFCURSOR;
BEGIN
  OPEN CRS FOR
    SELECT CASE WHEN 1 > 0 THEN 1 ELSE 0 END FROM DUAL;
END;

Opens a cursor that would contain a result set with a single row; however, the cursor is held as a local variable within the PL/SQL anonymous block and is never "returned" to the application code that calls the PL/SQL.

The traditional way would be to use SQL without the PL/SQL wrapper:

String sql = "SELECT CASE WHEN 1 > 0 THEN 1 ELSE 0 END FROM DUAL";
getJdbcTemplate().queryForObject(sql, Boolean.class);

You could try using DBMS_SQL.RETURN_RESULT from the PL/SQL block but that is only available from Oracle 12 and it does not have the same level of support that traditional SQL queries do so it may not work:

DECLARE
  crs SYS_REFCURSOR;
BEGIN
  OPEN crs FOR
    SELECT CASE WHEN 1 > 0 THEN 1 ELSE 0 END FROM DUAL;
  DBMS_SQL.RETURN_RESULT(crs);
END;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文