批量收集和forall保存例外

时间:2015-07-09 14:12:07

标签: exception oracle11g save forall bulk-collect

我有下面的代码,emp1表有12行的数据,其中2 empno已经存在于emp表中。我正在尝试保存这两个记录的例外,并将剩余的全部插入到emp中。但是一切都搞错了,我无法插入到emp中。有人请帮忙。

SET SERVEROUTPUT ON;
DECLARE
  CURSOR C1
  IS
    SELECT EMPNO FROM EMP1;
TYPE T
IS
  TABLE OF C1%ROWTYPE;
  L_DATA T;
  L_ERRORS_COUNT NUMBER;
  EX_DML_ERRORS  EXCEPTION;
  PRAGMA EXCEPTION_INIT (ex_DML_ERRORS, -47);
  L_ERRNO NUMBER;
  L_MSG   VARCHAR2(4000);
  L_IDX   NUMBER;
BEGIN
  OPEN C1;
  LOOP
    FETCH C1 BULK COLLECT INTO L_DATA;
    BEGIN
      FORALL I IN 1..L_DATA.COUNT SAVE EXCEPTIONS
      INSERT
      INTO EMP
        (
          EMPNO,
          ENAME,
          MGR
        )
        VALUES
        (
          L_DATA(I).EMPNO,
          'SCOTT',
          L_DATA(I).EMPNO
        );
    EXCEPTION
    WHEN dup_val_on_index THEN
      DBMS_OUTPUT.PUT_LINE('WHD');
      L_ERRORS_COUNT := SQL%BULK_EXCEPTIONS.COUNT;
      DBMS_OUTPUT.PUT_LINE('WHD::::'||L_ERRORS_COUNT);
      FOR J IN 1..L_ERRORS_COUNT
      LOOP
        L_ERRNO := SQL%BULK_EXCEPTIONS
        (
          J
        )
        .ERROR_CODE;
        L_MSG := SQLERRM(-L_ERRNO);
        L_IDX := SQL%BULK_EXCEPTIONS(J).ERROR_INDEX;
        DBMS_OUTPUT.PUT_LINE('I am here SVS:'||L_ERRNO||':'|| L_MSG||':'||L_IDX);
        --INSERT INTO EMPS_EXC1(SQLC ,SQLE ,INDX ) VALUES(L_ERRNO, L_MSG,L_IDX);
        DBMS_OUTPUT.PUT_LINE('I am here SVS:'||L_ERRNO||':'|| L_MSG||':'||L_IDX);
      END LOOP;
    END;
    EXIT
  WHEN C1%NOTFOUND;
  END LOOP;
  CLOSE C1;
END;

0 个答案:

没有答案