Oracle PL/SQL 类型未在过程中使用

问题描述 投票:0回答:1

下面给出的是代码。如果我尝试在程序之外使用类型,则会出错。 当我尝试在过程中声明相同的类型时,它工作正常。这是什么问题?

drop type t_tf_order_tbl;
drop type t_tf_order_row;

create or replace TYPE t_tf_order_row as OBJECT
(order_id number,
line_qty number,
total_value number);

create or replace TYPE t_tf_order_tbl as TABLE OF t_tf_order_row;

CREATE OR REPLACE PROCEDURE test_refcur_bulk_collect(p_ref_cursor IN SYS_REFCURSOR)
AS
    v_tbl t_tf_order_tbl := t_tf_order_tbl();
BEGIN

    FETCH p_ref_cursor                    -- run-time error happens here
    BULK COLLECT INTO v_tbl;  
    
    FOR i IN v_tbl.FIRST .. v_tbl.LAST
    LOOP        
        dbms_output.put_line(v_tbl(i).order_id||', '||v_tbl(i).line_qty||', '||v_tbl(i).total_value);
    END LOOP;    
END;
/

Error starting at line : 39 in command -
BEGIN test_refcur_bulk_collect(fn_srf_get_order_rows(100)); END;
Error report -
ORA-06504: PL/SQL: Return types of Result Set variables or query do not match
ORA-06512: at "SCOTT.TEST_REFCUR_BULK_COLLECT", line 6
ORA-06512: at line 1
06504. 00000 -  "PL/SQL: Return types of Result Set variables or query do not match"

现在,如果在程序中使用 TYPES,我不会收到错误。我想了解这里发生了什么?

CREATE OR REPLACE PROCEDURE test_refcur_bulk_collect(p_ref_cursor IN SYS_REFCURSOR)
AS

TYPE t_tf_order_row is record
(order_id number,
line_qty number,
total_value number);

TYPE t_tf_order_tbl is TABLE OF t_tf_order_row;    **--USING THE SAME TYPES INSIDE THE PROC WORKS FINE**

    v_tbl t_tf_order_tbl := t_tf_order_tbl();
BEGIN

    FETCH p_ref_cursor                     --works fine
    BULK COLLECT INTO v_tbl;  
    
    FOR i IN v_tbl.FIRST .. v_tbl.LAST
    LOOP        
        dbms_output.put_line(v_tbl(i).order_id||', '||v_tbl(i).line_qty||', '||v_tbl(i).total_value);
    END LOOP;    
END;
/

Procedure TEST_REFCUR_BULK_COLLECT compiled

exec test_refcur_bulk_collect(fn_srf_get_order_rows(100));
PL/SQL procedure successfully completed.

100, 10, 942.97
100, 9, 177.39
oracle plsql
1个回答
0
投票

您的代码有效 - 您只需传入包含对象的游标(而不是包含三列的游标):

DECLARE
  v_cur SYS_REFCURSOR;
BEGIN
  DBMS_OUTPUT.ENABLE();

  OPEN v_cur FOR
    SELECT t_tf_order_row(1, 2, 3) AS order_row FROM DUAL UNION ALL
    SELECT t_tf_order_row(4, 5, 6) FROM DUAL;

  test_refcur_bulk_collect(v_cur);

  CLOSE v_cur;
END;
/  

输出:

1, 2, 3
4, 5, 6

小提琴

© www.soinside.com 2019 - 2024. All rights reserved.