【问题标题】:Get the first value from Oracle cursor - Calling From Java Code从 Oracle 游标中获取第一个值 - 从 Java 代码调用
【发布时间】:2021-03-01 07:36:58
【问题描述】:

我创建了一个 oracle 游标以促进并发性。这是我的光标。

create or replace FUNCTION get_unlocked_records RETURN table_to_test%ROWTYPE IS
CURSOR c IS SELECT * FROM table_to_test where status_code = 5 FOR UPDATE SKIP LOCKED;
record_to_get table_to_test%ROWTYPE;
    BEGIN
        OPEN c;
        FETCH c INTO record_to_get;
        CLOSE c;
        RETURN record_to_get;
    END;

当我使用这些命令在 2 个单独的 sql 会话中进行测试时,会出现以下错误。

declare
  record_to_gets table_to_test%ROWTYPE;    
begin
 exec :record_to_gets := get_unlocked_records;
 dbms_output.put_line(record_to_gets);
end;

错误

Error starting at line : 32 in command -
declare
  record_to_gets   table_to_test%ROWTYPE;    
begin
 exec :record_to_gets := get_unlocked_records;
 dbms_output.put_line(record_to_gets);
end;
Error report -
ORA-06550: line 4, column 7:
PLS-00103: Encountered the symbol "" when expecting one of the following:

   := . ( @ % ;
The symbol ";" was substituted for "" to continue.
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

我在这里做的错误是什么?

既然我的最终目标是在java中调用函数并得到结果,那么如何调用这个函数来获取java中的第一条记录呢?

提前致谢。

【问题讨论】:

    标签: oracle plsql oracle11g stored-functions


    【解决方案1】:

    EXEC[UTE] 是一个SQL*Plus 命令,在SQL*Plus 中使用冒号前置变量,但在PL/SQL 中可能会使用EXECUTE IMMEDIATE而在您的情况下不需要这样做,仅使用这样的 assignment 而不预先添加局部变量就足够了:

    DECLARE
      record_to_gets table_to_test%ROWTYPE;   
    BEGIN
      record_to_gets := get_unlocked_records;
      DBMS_OUTPUT.PUT_LINE(record_to_gets.col1);
      DBMS_OUTPUT.PUT_LINE(record_to_gets.col2)
    END;
    /
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-16
      • 1970-01-01
      • 2017-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多