从 Oracle 游标获取第一个值 - 从 Java 代码调用

Get the first value from Oracle cursor - Calling From Java Code

我创建了一个 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中的第一条记录呢?

提前致谢。

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;
/