如何获取数组中的存储过程输出值

时间:2019-11-13 07:44:46

标签: arrays oracle plsql

我正在创建一个有关获取输出值的过程。输出值在另一个过程中调用,并存储在数组中。

procedure get_value_temp
(
v_name_id in out varchar2,
v_event_id out  varchar2
)
is
begin  
             select s.name_id,d.event_id into v_name_id ,v_event_id from aokk_kala_appreg_stage s inner join aokk_kala_appreg_det d
            on s.name_id=d.name_id where s.name_id=v_name_id;
            --return;
 end;

以下是另一个过程。

 declare
 V_ARRAY            MYTABLETYPE;
name_id  varchar2(100);
 BEGIN
V_ARRAY:=get_value_temp(220);--get output value and store in array.
 FOR I IN V_ARRAY.FIRST .. V_ARRAY.LAST
                    loop
                        dbms_output.put_line(V_ARRAY(I));
                    END LOOP;

end;

请帮助我了解如何在数组中获取存储过程的输出值。

2 个答案:

答案 0 :(得分:0)

例如,假设您有此表:

create table myTab(n number, v varchar2(100));
insert into myTab(n, v) values (1, 'one');
insert into myTab(n, v) values (2, 'two');
insert into myTab(n, v) values (2, 'TWO');

您可以构建类似以下的过程

create or replace procedure myProc (p_n IN number, po_mytabletype OUT mytabletype) is    
begin
    select v
    bulk collect into po_mytabletype
    from myTab
    where n = p_n;
end;

如果您这样称呼:

declare
    v_mytabletype    mytabletype;
begin
    myProc(2, v_mytabletype);
    for i in v_mytabletype.first .. v_mytabletype.last loop
        dbms_output.put_line (v_mytabletype(i));
    end loop;
end; 

您得到:

two
TWO

答案 1 :(得分:0)

根据我对您的要求的理解,这是示例代码演示,如果不符合您的要求,请分享更多详细信息。

CREATE OR REPLACE PROCEDURE get_value_temp(v_name_id IN VARCHAR2, 
                                           o_events_for_nameid OUT sys_refcursor) IS
BEGIN 
  open o_events_for_nameid 
   for select s.event_id from 
  (SELECT 'A' name_id, '1' event_id FROM dual UNION 
   SELECT 'A' name_id, '2' event_id FROM dual UNION 
   SELECT 'C' name_id, ' 3' event_id FROM dual UNION 
   SELECT 'D' name_id, '4' event_id FROM dual UNION
   SELECT 'D' name_id, '5' event_id FROM dual UNION 
   SELECT 'E' name_id, '6' event_id FROM dual) s WHERE s.name_id = v_name_id;
   END;
   / 

drop type mytabletype;
/
CREATE OR REPLACE TYPE mytabletype AS TABLE OF VARCHAR2(200); -- this is nested table definition 
/

现在测试代码...

DECLARE
  v_nested_table             mytabletype :=  mytabletype();
  o_events_for_nameid        SYS_REFCURSOR;

BEGIN
  get_value_temp(v_name_id           => 'A',
                 o_events_for_nameid => o_events_for_nameid);

    FETCH o_events_for_nameid BULK COLLECT
      INTO v_nested_table;               
  FOR i IN v_nested_table.first .. v_nested_table.last
  LOOP
    dbms_output.put_line(v_nested_table(i));
  END LOOP;
END;