在 FOR 循环中构建 table 个数字

Building a table of numbers within a FOR LOOP

我有以下程序需要从另一个程序调用。

PROCEDURE get_list_ntds_for_given_port(
  pis_olt_name       IN common.types.string
, pis_chasis_lt_port IN common.types.string
, pona_ntd_ids       OUT common_api.ntabtype
)
IS
  lsa_object_name       common_api.vtabtype;
  lsa_link_type         common_api.vtabtype;
  lsa_link_status       common_api.vtabtype;
  lna_link_object_id    common_api.ntabtype;
  lsa_link_object_name  common_api.vtabtype;
  lsa_link_object_class common_api.vtabtype;
  ln_pon_port_id        NUMBER;
  lna_child_pon_result  FLS_NUMBER_TABLE;
  lona_ntd_ids          common_api.ntabtype;
  ls_pon_port_map       common.types.string;
BEGIN
  ls_pon_port_map := pis_olt_name || '/' || pis_chasis_lt_port;
  ln_pon_port_id := nwmdl.alu_pon_port.find(
      pis_full_pon_port_name => ls_pon_port_map
    );
  lna_child_pon_result:= rm.sri_object_utils.get_child_objects(
      pis_proc_name => lc_proc_name
    , pin_parent_id => ln_pon_port_id
    );
    pona_ntd_ids := common_api.ntabtype();
  FOR i IN 1..lna_child_pon_result.COUNT
  LOOP
    rm.ims_objects_api.get_peer_links (
        pin_object_id          => lna_child_pon_result(i)
      , pis_geographical_only  => 'N'
      , pona_object_id         => lona_ntd_ids
      , posa_object_name       => lsa_object_name
      , posa_link_type         => lsa_link_type
      , posa_link_status       => lsa_link_status
      , pona_link_object_id    => lna_link_object_id
      , posa_link_object_name  => lsa_link_object_name
      , posa_link_object_class => lsa_link_object_class
      ); 
    pona_ntd_ids.EXTEND(lona_ntd_ids.COUNT);
    FOR i IN 1..lona_ntd_ids.COUNT 
    LOOP 
      pona_ntd_ids(i) := lona_ntd_ids(i);
    END LOOP;  
  END LOOP;
END get_list_ntds_for_given_port;

我用这个pl-sql块模拟外部调用:

SET SERVEROUTPUT ON;
DECLARE
lona_ntd_ids common_api.ntabtype;
BEGIN
svcmgr_lni.design_and_assign.get_list_ntds_for_given_port( pis_olt_name        =>  'SWOLT0000003DV'
                              ,pis_chasis_lt_port =>  '1-1/LT1/1'
                              ,pona_ntd_ids       => lona_ntd_ids
                             );
     FOR i IN 1..lona_ntd_ids.COUNT 
      LOOP                       
      DBMS_OUTPUT.PUT_LINE(lona_ntd_ids(i)); 
      END LOOP; 
 END;

问题是我在输出中没有得到任何结果。 我的猜测是,我的 rm.ims_objects_api.get_peer_links API 将被 rm.sri_object_utils.get_child_objects 找到的每个 lna_child_pon_result(i) 调用,并将更准确地放入 API 的 OUT 参数中输出参数 a table of numbers(ids) 或 varchar。 我想要实现的是构建主程序 (get_list_ntds_for_given_port) 的 table 数字并将其设置为输出参数,以便在主过程之外使用 table 数字。 请留下您对解决此问题的任何想法。

试试这个:

将 "pona_ntd_ids := common_api.ntabtype();" 移到循环外:

pona_ntd_ids := common_api.ntabtype();
FOR i IN 1..lna_child_pon_result.COUNT

改变

pona_ntd_ids(i) := lona_ntd_ids(i);

pona_ntd_ids(pona_ntd_ids.COUNT+1) := lona_ntd_ids(i); 

由于在您的原始代码中您为每个循环初始化 pona_ntd_ids 如果最后一次外部迭代产生 lona_ntd_ids.COUNT=0.

,您最终将一无所获

你可以做一些小修改,批量收集整个对象,然后将其作为 OUT 参数。是的,这将是程序的开销,因为使用了正则表达式,但它足以满足您的查询需求。

FOR i IN 1..lona_ntd_ids.COUNT 
    LOOP 
    lv_eg_string := lona_ntd_ids(i)||','||lv_eg_string;
--      pona_ntd_ids(i) := lona_ntd_ids(i);
    END LOOP; 
    lv_eg_string:=substr(lv_eg_string,1,length(lv_eg_string)-1);
    SELECT regexp_substr(lv_eg_string,'[^,]+', 1, level) BULK COLLECT
    INTO pona_ntd_ids
    FROM dual
   CONNECT BY regexp_substr(lv_eg_string, '[^,]+', 1, level);