【问题标题】:How to fetch a sys_refcursor to an asociative array table in PL/SQL如何获取 sys_refcursor 到 PL/SQL 中的关联数组表
【发布时间】:2019-10-11 09:57:34
【问题描述】:

有这个定义:

TYPE type_record1 IS RECORD(
id NUMBER,
value NUMBER
);

v_count NUMBER;

TYPE tp_arr_record IS TABLE OF type_record1 INDEX BY PLS_INTEGER;

v_t_arr_record   tp_arr_record;

v_results sys_refcursor;

还有:

v_results := f_execute_cursor(id_process);

如何使用如下形式的关联数组获取这个弱游标 (v_results):

FOR idx IN v_results 
    LOOP
      v_count   :=  v_count + 1;
      v_t_arr_record(v_count).id      :=  idx.id;
      v_t_arr_record(v_count).value   :=  idx.value;
    END LOOP;

换句话说,如何在不使用记录的情况下从 sys_refcursor 获取关联数组,因为我需要从游标中获取数据集?

【问题讨论】:

    标签: sql oracle function plsql procedure


    【解决方案1】:

    使用与我在回答您的另一个posts 时使用的代码几乎相同的代码,这将获取您的函数返回的产品并将值存储到一个集合中。

    如果您需要澄清或对我的回答有其他问题,请告诉我。

    DECLARE
    
      /* Store Ref Cursor returned by f_process_data() Function */
      v_result_cursor   SYS_REFCURSOR;
    
      /* Declare Record so we can store the data FETCHed from the Cursor */
      rec_product       products%ROWTYPE;
    
      /* Declare Type based off of products table */
      TYPE  t_products  IS TABLE OF products%ROWTYPE;
    
      /* Declare Table based off of Type t_products and initialize it. */
      tab_products      t_products   :=    t_products();
    
      /* Declare a couple Product Variables for Proof of Concept */
      v_sausage         NUMBER;
      v_ham             NUMBER;
    
      /* Store output */
      n_id              NUMBER;
      v_id_product      VARCHAR2(100);
    
      /* Declare Type of TABLE NUMBER */
      TYPE  nt_type IS TABLE OF NUMBER;
    
      /* Create Array/Table/Collection of type nt_type to store product ids */
      nt_product_ids    nt_type;
    
      /* Returns a Ref Cursor based on the product_id used as Input to this function */
      FUNCTION f_process_data(p_id_process IN NUMBER, p_id_product IN NUMBER)
      RETURN SYS_REFCURSOR
      AS
        /* Declare Ref Cursor that will be Returned */
        rc_result_cursor   SYS_REFCURSOR;    
    
      BEGIN 
        /* Open Ref Cursor based on Product ID parameter */
        OPEN rc_result_cursor FOR SELECT * FROM products WHERE item_id = p_id_product;
    
        RETURN rc_result_cursor;
    
      END f_process_data
      ;
    
    BEGIN
    
      /* Set Product Variables to IDs */
      v_sausage       := 2003;
      v_ham           := 2007;
    
      /* Store product ids into a Number Table so we can Loop thru it */
      nt_product_ids  :=  nt_type (v_sausage,v_ham);
    
      FOR r IN nt_product_ids.FIRST .. nt_product_ids.LAST
      LOOP
        /* Get Ref Cursor using SINGLE Product ID */
        v_result_cursor := f_process_data(1, nt_product_ids(r));
    
        LOOP
    
          FETCH v_result_cursor INTO rec_product;
    
          n_id            :=  rec_product.item_id;
          v_id_product    :=  rec_product.item;
    
          EXIT WHEN v_result_cursor%NOTFOUND;
    
          --dbms_output.put_line('Product_id: ' || n_id);
          --dbms_output.put_line('Product: ' || v_id_product); 
    
          /* Store data into Collection */
          tab_products.EXTEND(1);          
          /* Set the Row inside the tab_products Table at the newly extended Index to the record rec_product */
          tab_products(tab_products.COUNT)  :=  rec_product;
    
        END LOOP; /* Cursor Loop */
    
        /* Close Cursor */
        CLOSE v_result_cursor;
    
      END LOOP; /* Product IDs Loop */
    
    dbms_output.put_line('Total Products in tab_products COllECTION: ' || tab_products.COUNT);
    
    /* Now we can just loop thru our tab_products Collection */
    For r IN tab_products.FIRST .. tab_products.LAST
    LOOP
    
      dbms_output.put_line('Product: ' || tab_products(r).item_id ||' - ' || tab_products(r).item);
    
    END LOOP;
    
    EXCEPTION WHEN OTHERS
      THEN CLOSE v_result_cursor;
    
    END;
    

    【讨论】:

    • 没有澄清,没关系,但我需要研究这个并应用到另一个项目中。谢谢。
    • @reymagnus 这个指向我自己的 SO 帖子之一的链接可能对您有帮助,也可能对您没有帮助。 stackoverflow.com/questions/55855857/…
    猜你喜欢
    • 2021-03-06
    • 1970-01-01
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    • 2017-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多