Continuing Inserts in Oracle when exception is raised

后端 未结 5 679
滥情空心
滥情空心 2020-12-11 09:39

I\'m working on migration of data from a legacy system into our new app(running on Oracle Database, 10gR2). As part of the migration, I\'m working on a script which inserts

相关标签:
5条回答
  • 2020-12-11 09:42

    If you use sqlldr you can specify to continue loading data, and all the 'bad' data will be skipped and logged in a separate file.

    0 讨论(0)
  • 2020-12-11 10:01

    Using PLSQL you can perform each insert in its own transaction (COMMIT after each) and log or ignore errors with an exception handler that keeps going.

    0 讨论(0)
  • 2020-12-11 10:01

    If the data volumes were higher, row-by-row processing in PL/SQL would probably be too slow. In those circumstances, you can use DML error logging, described here

    CREATE TABLE raises (emp_id NUMBER, sal NUMBER 
       CONSTRAINT check_sal CHECK(sal > 8000));
    
    EXECUTE DBMS_ERRLOG.CREATE_ERROR_LOG('raises', 'errlog');
    
    INSERT INTO raises
       SELECT employee_id, salary*1.1 FROM employees
       WHERE commission_pct > .2
       LOG ERRORS INTO errlog ('my_bad') REJECT LIMIT 10;
    
    SELECT ORA_ERR_MESG$, ORA_ERR_TAG$, emp_id, sal FROM errlog;
    
    ORA_ERR_MESG$               ORA_ERR_TAG$         EMP_ID SAL
    --------------------------- -------------------- ------ -------
    ORA-02290: check constraint my_bad               161    7700
     (HR.SYS_C004266) violated
    
    0 讨论(0)
  • 2020-12-11 10:02
    DECLARE
       cursor;
    BEGIN
        loop for each row  in cursor
          BEGIN  -- subBlock begins 
             SAVEPOINT startTransaction;  -- mark a savepoint
     -- do whatever you have do here
             COMMIT;         
          EXCEPTION
             ROLLBACK TO startTransaction;  -- undo changes
          END;  -- subBlock ends
       end loop;
    END;
    
    0 讨论(0)
  • 2020-12-11 10:08

    Try this:

    for r_row in c_legacy_data loop
      begin
        insert into some_table(a, b, c, ...)
        values (r_row.a, r_row.b, r_row.c, ...);
      exception
        when others then 
          null;  /* or some extra logging */
      end;
    end loop;
    
    0 讨论(0)
提交回复
热议问题