如何在oracle 9i中使用bulk collect forall进行更新

时间:2017-01-12 06:44:20

标签: plsqldeveloper

我在使用oracle 9i中的FORALL更新记录时遇到问题。当我执行i' m得到错误ORA-22160时:索引[1]处的元素不存在。

我有代码从表中派生值并将其存储在一个变量中。 当我使用该变量使用FORALL更新记录时,我没有得到索引[1]。

你能帮我解决一下如何从这个错误中解决。

enter code here
DECLARE
  CURSOR tk_ora_mod_dt
  IS
    SELECT DISTINCT model_id,
      ora_org_id,
      inventory_item_id,
      segment1,
      segment2
    FROM tk_ora_model_link top,
      mtl_system_items_b@SS_TKL_OMAR.WORLD msib
    WHERE top.ora_inv_item_id = msib.inventory_item_id
    AND msib.organization_id  =top.ora_org_id
    AND model_id              ='+12DB'
    AND ora_org_id            =127
    AND ora_inv_item_id       =366046;
  --Variable declaration
TYPE l_org_id
IS
  TABLE OF NUMBER INDEX BY binary_integer;
TYPE l_mo_id
IS
  TABLE OF VARCHAR2(200) INDEX BY binary_integer;
TYPE l_mo_org_id
IS
  TABLE OF NUMBER INDEX BY binary_integer;
TYPE l_seg1
IS
  TABLE OF VARCHAR2(200) INDEX BY binary_integer;
TYPE l_seg2
IS
  TABLE OF VARCHAR2(200) INDEX BY binary_integer;
TYPE l_inv_id
IS
  TABLE OF NUMBER INDEX BY binary_integer;
TYPE l_r12_org_id
IS
  TABLE OF NUMBER INDEX BY binary_integer;
TYPE l_r12_inv_id
IS
  TABLE OF VARCHAR2(200) INDEX BY binary_integer;
TYPE l_org_id_r12
IS
  TABLE OF NUMBER INDEX BY binary_integer;
  t_org_id l_org_id;
  t_mo_id l_mo_id;
  t_seg1 l_seg1;
  t_seg2 l_seg2;
  t_r12_org_id l_r12_org_id;
  t_inv_id l_inv_id;
  t_r12_inv_id l_r12_inv_id;
  t_org_id_r12 l_org_id_r12;
  user_excep  EXCEPTION;
  v_err_count NUMBER;
  PRAGMA EXCEPTION_INIT (user_excep, -24381);
  t_r12_inv_id_1 VARCHAR2(300);
  t_org_id_r12_1 VARCHAR2(300);
  t_inv_id_1     VARCHAR2(200);
BEGIN
  BEGIN
    OPEN tk_ora_mod_dt;
    LOOP
      FETCH tk_ora_mod_dt bulk collect
      INTO t_mo_id,
        t_org_id,
        t_inv_id,
        t_seg1,
        t_seg2;
      BEGIN
        FOR i IN 1..t_inv_id.COUNT
        LOOP
          SELECT gl_org_id --- query to select GLBLDEV values
            BULK COLLECT
          INTO t_r12_org_id
          FROM r12_lookup_test2
          WHERE om_org_id=t_org_id(i);
        BEGIN 
          FOR j IN 1..t_r12_org_id.COUNT
          LOOP
            SELECT DISTINCT r12_dt.inventory_item_id,
              r12_dt.organization_id
            INTO t_r12_inv_id_1,
              t_org_id_r12_1
            FROM inv.mtl_system_items_b@OMAR_TKL_SS.WORLD r12_dt
            WHERE segment1          =t_mo_id(i)
            AND organization_id     =t_r12_org_id(i)
            AND NVL(SEGMENT2,'NULL')=NVL(t_seg2(i),'NULL');

           -- t_r12_inv_id.EXTEND(j);
            t_r12_inv_id(j):=t_r12_inv_id_1;
            t_org_id_r12(j):=t_org_id_r12_1;
            dbms_output.put_line('the r12 inventory item id'||t_r12_inv_id(j));
            dbms_output.put_line('the r12 inventory item id'||t_r12_org_id(j));
            dbms_output.put_line('openup Org_id'||t_org_id(i));
           END LOOP;
         END;  
        END LOOP;
      END;
      BEGIN
        FORALL i IN t_r12_inv_id.FIRST..t_r12_inv_id.LAST SAVE EXCEPTIONS
        UPDATE tk_ora_model_link_12499
        SET ora_org_id      =t_org_id_r12(i)
        WHERE model_id      = t_mo_id(i)
        AND ora_inv_item_id = t_inv_id(i)
        AND ora_org_id      =t_org_id(i);
      EXCEPTION
      WHEN user_excep THEN
        v_err_count := SQL%BULK_EXCEPTIONS.COUNT;
        FOR i       IN 1 .. v_err_count
        LOOP
          DBMS_OUTPUT.put_line ( 'Error: ' || i || ' Array Index: ' || SQL%BULK_EXCEPTIONS (i).ERROR_INDEX || ' Message: ' || SQLERRM (SQL%BULK_EXCEPTIONS (i).ERROR_CODE) );
        END LOOP;
      END;
      EXIT
    WHEN t_inv_id.COUNT=0;
      --   COMMIT;
    END LOOP;
    CLOSE tk_ora_mod_dt;
  END;
END;  
/

1 个答案:

答案 0 :(得分:0)

尝试使用此代码,避免填充没有数据的数组

DECLARE
  CURSOR tk_ora_mod_dt IS
    SELECT DISTINCT model_id,
                    ora_org_id,
                    inventory_item_id,
                    segment1,
                    segment2
      FROM tk_ora_model_link top, mtl_system_items_b@SS_TKL_OMAR.WORLD msib
     WHERE top.ora_inv_item_id = msib.inventory_item_id
       AND msib.organization_id = top.ora_org_id
       AND model_id = '+12DB'
       AND ora_org_id = 127
       AND ora_inv_item_id = 366046;
  --Variable declaration
  TYPE l_org_id IS TABLE OF NUMBER INDEX BY binary_integer;
  TYPE l_mo_id IS TABLE OF VARCHAR2(200) INDEX BY binary_integer;
  TYPE l_mo_org_id IS TABLE OF NUMBER INDEX BY binary_integer;
  TYPE l_seg1 IS TABLE OF VARCHAR2(200) INDEX BY binary_integer;
  TYPE l_seg2 IS TABLE OF VARCHAR2(200) INDEX BY binary_integer;
  TYPE l_inv_id IS TABLE OF NUMBER INDEX BY binary_integer;
  TYPE l_r12_org_id IS TABLE OF NUMBER INDEX BY binary_integer;
  TYPE l_r12_inv_id IS TABLE OF VARCHAR2(200) INDEX BY binary_integer;
  TYPE l_org_id_r12 IS TABLE OF NUMBER INDEX BY binary_integer;
  t_org_id     l_org_id;
  t_mo_id      l_mo_id;
  t_seg1       l_seg1;
  t_seg2       l_seg2;
  t_r12_org_id l_r12_org_id;
  t_inv_id     l_inv_id;
  t_r12_inv_id l_r12_inv_id;
  t_org_id_r12 l_org_id_r12;
  user_excep EXCEPTION;
  v_err_count NUMBER;
  PRAGMA EXCEPTION_INIT(user_excep, -24381);
  t_r12_inv_id_1 VARCHAR2(300);
  t_org_id_r12_1 VARCHAR2(300);
  t_inv_id_1     VARCHAR2(200);
    l_counter      number := 0;
BEGIN
  BEGIN
    OPEN tk_ora_mod_dt;
    LOOP
      FETCH tk_ora_mod_dt bulk collect
        INTO t_mo_id, t_org_id, t_inv_id, t_seg1, t_seg2;
      BEGIN
        FOR i IN 1 .. t_inv_id.COUNT LOOP
          SELECT gl_org_id --- query to select GLBLDEV values
            BULK COLLECT
            INTO t_r12_org_id
            FROM r12_lookup_test2
           WHERE om_org_id = t_org_id(i);
          BEGIN
            FOR j IN 1 .. t_r12_org_id.COUNT LOOP

                        t_r12_inv_id_1 := null;
                        t_org_id_r12_1 := null;

              SELECT DISTINCT r12_dt.inventory_item_id,
                              r12_dt.organization_id
                INTO t_r12_inv_id_1, t_org_id_r12_1
                FROM inv.mtl_system_items_b@OMAR_TKL_SS.WORLD r12_dt
               WHERE segment1 = t_mo_id(i)
                 AND organization_id = t_r12_org_id(i)
                 AND NVL(SEGMENT2, 'NULL') = NVL(t_seg2(i), 'NULL');

                            if t_r12_inv_id_1 is not null and t_org_id_r12_1 is not null then
                                --fill array
                              l_counter := l_counter +1;

                                t_r12_inv_id.EXTEND();
                                t_org_id_r12.EXTEND();

                                t_r12_inv_id(l_counter) := t_r12_inv_id_1;
                                t_org_id_r12(l_counter) := t_org_id_r12_1;
                            end if;

              dbms_output.put_line('the r12 inventory item id' ||
                                   t_r12_inv_id(j));
              dbms_output.put_line('the r12 inventory item id' ||
                                   t_r12_org_id(j));
              dbms_output.put_line('openup Org_id' || t_org_id(i));
            END LOOP;
          END;
        END LOOP;
      END;
      BEGIN
        FORALL i IN t_r12_inv_id.FIRST .. t_r12_inv_id.LAST SAVE EXCEPTIONS
          UPDATE tk_ora_model_link_12499
             SET ora_org_id = t_org_id_r12(i)
           WHERE model_id = t_mo_id(i)
             AND ora_inv_item_id = t_inv_id(i)
             AND ora_org_id = t_org_id(i);
      EXCEPTION
        WHEN user_excep THEN
          v_err_count := SQL%BULK_EXCEPTIONS.COUNT;
          FOR i IN 1 .. v_err_count LOOP
            DBMS_OUTPUT.put_line('Error: ' || i || ' Array Index: ' || SQL%BULK_EXCEPTIONS(i)
                                 .ERROR_INDEX || ' Message: ' ||
                                 SQLERRM(SQL%BULK_EXCEPTIONS(i).ERROR_CODE));
          END LOOP;
      END;
      EXIT WHEN t_inv_id.COUNT = 0;
      --   COMMIT;
    END LOOP;
    CLOSE tk_ora_mod_dt;
  END;
END;
/