1. 程式人生 > >用 APPEND 批量載入資料時,可避免不合格資料

用 APPEND 批量載入資料時,可避免不合格資料

$ sqlplus /nolog

SQL*Plus: Release 10.2.0.1.0 - Production on Mon Nov 19 16:50:50 2007

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

SQL> conn / as sysdba
Connected.

SQL> GRANT EXECUTE ON DBMS_ERRLOG TO scott; -- 呼叫包許可權

Grant succeeded.

SQL> conn scott/tiger;
Connected.
SQL> CREATE TABLE tar_emp AS SELECT * FROM emp WHERE 1=2;

Table created.

SQL> alter table tar_emp add constraint CK_SCOTT_TAR_EMP_SAL check (SAL > 1000);
Table altered.

SQL> INSERT /*+APPEND*/ INTO tar_emp SELECT * FROM emp;
INSERT /*+APPEND*/ INTO tar_emp SELECT * FROM emp
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.CK_SCOTT_TAR_EMP_SAL) violated

SQL> EXEC DBMS_ERRLOG.CREATE_ERROR_LOG('TAR_EMP');

PL/SQL procedure successfully completed.

-- Oracle 會自動建立一個 "err$_ + 表名" 的表來儲存批量載入時有錯誤的資料。
SQL> desc err$_tar_emp;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ORA_ERR_NUMBER$                                    NUMBER
 ORA_ERR_MESG$                                      VARCHAR2(2000)
 ORA_ERR_ROWID$                                     ROWID
 ORA_ERR_OPTYP$                                     VARCHAR2(2)
 ORA_ERR_TAG$                                       VARCHAR2(2000)
 EMPNO                                              VARCHAR2(4000)
 ENAME                                              VARCHAR2(4000)
 JOB                                                VARCHAR2(4000)
 MGR                                                VARCHAR2(4000)
 HIREDATE                                           VARCHAR2(4000)
 SAL                                                VARCHAR2(4000)
 COMM                                               VARCHAR2(4000)
 DEPTNO                                             VARCHAR2(4000)

SQL> INSERT /*+APPEND*/ INTO tar_emp SELECT * FROM emp LOG ERRORS REJECT LIMIT UNLIMITED;

12 rows created.

SQL> commit;

Commit complete.