小编典典

从java中的plsql函数获取返回的记录类型

java

我有一个plsql返回记录类型的函数challan_rec

create or replace package xx_bal_api as
    type challan_rec is record (
        challan_number varchar2(40),
        challan_amount number
    );


    FUNCTION get_challan(foo number) return challan_rec;
end;



create or replace package body xx_bal_api as

    FUNCTION get_challan(foo number) return challan_rec
    is
    cr challan_rec;
    begin
        cr.challan_number := '00002154215'; 
        cr.challan_amount := 2265;
        return cr;
    end get_challan;
end;

我想从Java代码中调用此函数,并在Java中获取该记录类型。请指导我如何做。谢谢


阅读 235

收藏
2020-11-30

共1个答案

小编典典

首先让我们从更正您的代码开始。我可以看到您发布的代码无法编译,并且会引发问题。正确的代码如下:

CREATE OR REPLACE PACKAGE xx_bal_api
AS
   TYPE challan_rec IS RECORD
   (
      challan_number   VARCHAR2 (40),
      challan_amount   NUMBER
   );

   TYPE rec IS TABLE OF challan_rec index by pls_integer;

   FUNCTION get_challan (foo NUMBER)
      RETURN rec;
END;
/
CREATE OR REPLACE PACKAGE BODY xx_bal_api
AS
   FUNCTION get_challan (foo NUMBER)
      RETURN rec
   IS
      cr   rec;
   BEGIN
      cr (1).challan_number := '00002154215';
      cr (1).challan_amount := foo;
      RETURN cr;
   END get_challan;
END;

执行此代码时,您可能会遇到类似的问题invalid datatype。这是因为a RECORD仅限在中使用PLSQL

select xx_bal_api.get_challan(2265) from dual;

ORA-00902:无效的数据类型

最好的方法是先创建一个对象的a OBJECT和a TYPE,然后return在您的函数中创建它:

CREATE OR REPLACE TYPE challan_rec IS OBJECT
(
   challan_number VARCHAR2 (40),
   challan_amount NUMBER
);
/    
CREATE OR REPLACE Type rec is table of challan_rec;
/ 
 CREATE OR REPLACE FUNCTION get_challan(foo number) 
    return rec
  is  
    cr rec:=rec();

   begin
        cr.extend();
        cr(1):= challan_rec('00002154215',foo);        
        return cr;
   end get_challan;   
end;

/

执行:

SQL> select get_challan(2265) from dual;
     /
2020-11-30