从ROWTYPE嵌套表中检索列

时间:2018-01-31 12:27:15

标签: oracle plsql

我有一个SELECT查询,我将其存储在嵌套表中(例如CONTACT_INFO%ROWTYPE)。我刚刚添加了简单的选择查询,因为我无法在这里添加实际的选择查询。

create or replace PROCEDURE process_all_rows (limit_in IN PLS_INTEGER DEFAULT 100)
IS
    CURSOR suspect_cur 
    IS 
        select ID, PHONE, EMAIL, MAILING_CITY , CITY, FIRST_NAME, LAST_NAME from CONTACT_INFO WHERE rownum < 10;
    TYPE suspectType IS TABLE OF suspect_cur%ROWTYPE
        INDEX BY PLS_INTEGER;
    l_suspect suspectType;
    TYPE city_name_type IS TABLE OF CONTACT_INFO.CITY%TYPE;
    V_CITY_NAME_TBL city_name_type;
BEGIN   
    OPEN suspect_cur;
    LOOP
        FETCH suspect_cur 
            BULK COLLECT INTO l_suspect LIMIT limit_in;
        EXIT WHEN l_suspect.COUNT < limit_in;

    --NEED TO PROCESS COLLECT DATA FROM l_suspect into V_CITY_NAME_TBL

   END LOOP;
   CLOSE suspect_cur;
END process_all_rows;

现在我想用检索到的数据完成两项任务: -

  1. 将获取的数据存储到我将能够完成的另一个表中。

  2. 我只需从可用集合中检索单列的不同数据(例如,&#39; CITY&#39;)。

  3. 我正在尝试使用可用的嵌套表,因为select查询需要花费太多时间来处理所有数据..并且它与25K记录匹配近10M行。

1 个答案:

答案 0 :(得分:0)

  

现在我想用检索到的数据完成两项任务: -

     
      
  1. 将获取的数据存储到我将能够完成的另一个表中。
  2.   
  3. 我只需要从可用集合中检索单列的不同数据(例如,&#39; CITY&#39;)。
  4.   

在查看您的代码评论和问题之后,我可以理解您希望将CITY信息放入集合l_suspect到集合V_CITY_NAME_TBL,其次,您希望将不同的CITY记录放入第二个集合即V_CITY_NAME_TBL

您可以按照以下方式执行此操作。内联阅读我的评论。

create or replace PROCEDURE process_all_rows (limit_in IN PLS_INTEGER DEFAULT 100)
IS
    CURSOR suspect_cur 
    IS 
        select ID, PHONE, EMAIL, MAILING_CITY , CITY, FIRST_NAME, LAST_NAME 
        from CONTACT_INFO WHERE rownum < 10;

    TYPE suspectType IS TABLE OF suspect_cur%ROWTYPE INDEX BY PLS_INTEGER;

    l_suspect suspectType;

    TYPE city_name_type IS TABLE OF CONTACT_INFO.CITY%TYPE;

    V_CITY_NAME_TBL city_name_type:=city_name_type();
BEGIN   
    OPEN suspect_cur;
    LOOP
        FETCH suspect_cur 
            BULK COLLECT INTO l_suspect LIMIT limit_in;
        EXIT WHEN l_suspect.COUNT < limit_in;

        --NEED TO PROCESS COLLECT DATA FROM l_suspect into V_CITY_NAME_TBL           
        --Putting CITY information from your `l_suspect` collection to `V_CITY_NAME_TBL`
        FOR REC IN 1..l_suspect.count
        loop
          V_CITY_NAME_TBL.EXTEND(); 

          V_CITY_NAME_TBL(REC) := l_suspect(rec).city;

        end loop;

   END LOOP;

    --Selecting Disticnt records in your collection using Multiset Union Distinct. 

    V_CITY_NAME_TBL := V_CITY_NAME_TBL multiset union distinct V_CITY_NAME_TBL;

   CLOSE suspect_cur;
END process_all_rows;

详细了解Multiset Union

PS:未经测试