通過spool來製作資料檔案:--可以查詢協助文檔的範例程式碼
SQL> spool /u01/app/oracle/test_data_loader/student.txt--開啟spool匯出資料檔案 SQL> select id ||',' || name ||',' || age ||',' || inner_date from student;--匯出資料
ID||','||NAME||','||AGE||','||INNER_DATE -------------------------------------------------------------------------------- 1,zhangsan,21,23-JAN-15 2,lisi,22,23-JAN-15 3,wangwu,23,23-JAN-15
SQL> spool off;--關閉 SQL> exit Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options [oracle@localhost test_data_loader]$ cat student.txt--可以查看到匯出的資料記錄 SQL> select id ||',' || name ||',' || age ||',' || inner_date from student;
ID||','||NAME||','||AGE||','||INNER_DATE -------------------------------------------------------------------------------- 1,zhangsan,21,23-JAN-15 2,lisi,22,23-JAN-15 3,wangwu,23,23-JAN-15
SQL> spool off;
寫設定檔: [oracle@localhost test_data_loader]$ vi student.ctl [oracle@localhost test_data_loader]$ cat student.ctl options(skip=4)--表示前面的四行 load data--匯入資料 infile 'student.txt'--通過該檔案匯入資料 into table student--匯入的表 insert--執行的是插入操作 fields terminated by ','--記錄中的分割符 ( id char,--注意雖然表中是number類型,但是要寫char類型 name char, age char, inner_date date nullif (inner_date = "null")) [oracle@localhost test_data_loader]$
既然是insert操作所以: SQL> truncate table student;--清空表,由於執行的是插入操作
Table truncated.
SQL> select * from student;
no rows selected
執行sqlldr操作: [oracle@localhost test_data_loader]$ sqlldr hr/hr control= student.ctl log = student.log
SQL*Loader: Release 11.2.0.1.0 - Production on Fri Jan 23 23:11:08 2015
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Commit point reached - logical record count 4 [oracle@localhost test_data_loader]$ cat student.log
SQL*Loader: Release 11.2.0.1.0 - Production on Fri Jan 23 23:11:08 2015
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Control File: student.ctl Data File: student.txt Bad File: student.bad Discard File: none specified
(Allow all discards)
Number to load: ALL Number to skip: 4 Errors allowed: 50 Bind array: 64 rows, maximum of 256000 bytes Continuation: none specified Path used: Conventional
Table STUDENT, loaded from every logical record. Insert option in effect for this table: INSERT
Column Name Position Len Term Encl Datatype ------------------------------ ---------- ----- ---- ---- --------------------- ID FIRST * , CHARACTER NAME NEXT * , CHARACTER AGE NEXT * , CHARACTER INNER_DATE NEXT * , DATE DD-MON-RR NULL if INNER_DATE = 0X6e756c6c(character 'null')
Record 4: Rejected - Error on table STUDENT, column ID. Column not found before end of logical record (use TRAILING NULLCOLS)
Table STUDENT: 3 Rows successfully loaded. 1 Row not loaded due to data errors. 0 Rows not loaded because all WHEN clauses were failed. 0 Rows not loaded because all fields were null.
Space allocated for bind array: 66048 bytes(64 rows) Read buffer bytes: 1048576
Total logical records skipped: 4 Total logical records read: 4 Total logical records rejected: 1 Total logical records discarded: 0
Run began on Fri Jan 23 23:11:08 2015 Run ended on Fri Jan 23 23:11:08 2015
Elapsed time was: 00:00:00.10 CPU time was: 00:00:00.01 [oracle@localhost test_data_loader]$ sqlplus hr/hr
SQL*Plus: Release 11.2.0.1.0 Production on Fri Jan 23 23:13:14 2015
Copyright (c) 1982, 2009, Oracle. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> select * from student;
ID NAME AGE INNER_DATE ---------- -------------------- ---------- ------------ 1 zhangsan 21 23-JAN-15 2 lisi 22 23-JAN-15 3 wangwu 23 23-JAN-15
SQL> exit |