标签:
所有SQL*Loader实验笔记
实验案例总结(1-7): SQL*Loader实验笔记【一】
实验案例总结(8-13): SQL*Loader实验笔记【二】
实验案例总结(14-19): SQL*Loader实验笔记【三】
8)加载序列
CJ@db11g> create table t2 (seqno int , name varchar2(20)); Table created. |
load data infile * into table t2 replace ( seqno RECNUM, name Position(1:1024) ) BEGINDATA chen jian wang |
CJ@db11g> select * from t2; SEQNO NAME ---------- -------------------- 1 chen 2 jian 3 wang |
INTO TABLE dept (deptno SEQUENCE (1, 3), dname POSITION(1:14) CHAR ) |
9)载入有换行符的数据(REPLACE)
CJ@db11g> desc t; Name Null ? Type ----------------------------------------------------- -------- ------------------------------------ DEPTNO NUMBER(2) DNAME VARCHAR2(14) LOC VARCHAR2(13) LAST_UPTIME DATE ALL_LINE VARCHAR2(100) |
LOAD DATA INFILE * INTO TABLE t REPLACE FIELDS TERMINATED BY ‘,‘ TRAILING NULLCOLS ( DEPTNO, DNAME "upper(:dname)" , LOC "upper(:loc)" , LAST_UPTIME date ‘dd/mm/yyyy‘ , ALL_LINE "replace(:all_line,‘n‘,chr(10))" ) BEGINDATA 10,Sales,Virginia,01/04/2001,This is the SalesnOffice in Virginia 20,Accounting,Virginia,13/04/2001,This is the AccountingnOffice in Virginia 30,Consulting,Virginia,14/04/2001,This is the ConsultingnOffice in Virginia 40,Finance,Virginia,14/05/2001,This is the FinancenOffice in Virginia |
CJ@db11g> select * from t; DEPTNO DNAME LOC LAST_UPTI ALL_LINE ---------- -------------- ------------- --------- ---------------------------------------------------------------------------------------------------- 10 SALES VIRGINIA 01-APR-01 This is the Sales Office i Virgi ia 20 ACCOUNTING VIRGINIA 13-APR-01 This is the Accou ti g Office i Virgi ia DEPTNO DNAME LOC LAST_UPTI ALL_LINE ---------- -------------- ------------- --------- ---------------------------------------------------------------------------------------------------- 30 CONSULTING VIRGINIA 14-APR-01 This is the Co sulti g Office i Virgi ia 40 FINANCE VIRGINIA 14-MAY-01 This is the Fi a ce DEPTNO DNAME LOC LAST_UPTI ALL_LINE ---------- -------------- ------------- --------- ---------------------------------------------------------------------------------------------------- Office i Virgi ia |
10)载入有换行符的数据(FIX)
CJ@db11g> desc t; Name Null ? Type ----------------------------------------- -------- ---------------------------- DEPTNO NUMBER(5) DNAME VARCHAR2(14) LOC VARCHAR2(50) |
数据内容
10,bean1,ora cle beanbee user 20,bean2,oracle bean bee user 30,bean3,oracle beanbee us er 40,beanbee4,o racle beanbee user
LOAD DATA INFILE ‘test.csv‘ "fix 40" INTO TABLE t REPLACE FIELDS TERMINATED BY ‘,‘ TRAILING NULLCOLS ( DEPTNO, DNAME, LOC ) |
CJ@db11g> select * from t; DEPTNO DNAME LOC ---------- -------------- -------------------------------------------------- 10 bean1 ora cle beanbee user 20 bean2 oracle bean bee user 30 bean3 oracle beanbee us er |
[oracle@bean ~]$ od -c -w10 -v test.csv 0000000 1 0 , b e a n 1 , o 0000012 r a \n c l e b e a 0000024 n b e e u s e r 0000036 0000050 2 0 , b e a n 2 , o 0000062 r a c l e b e a n 0000074 \n b e e u s e r 0000106 0000120 3 0 , b e a n 3 , o 0000132 r a c l e b e a n 0000144 b e e u s \n e r 0000156 0000170 4 0 , b e a n b e e 0000202 4 , o \n r a c l e 0000214 b e a n b e e u s 0000226 e r \n 0000231 |
可以注意到上面导入的数据中40的行没有导入成功,这就是输入数据格式不对,可以看到最后一行有一个 \n 结束,这个情况会在导入的时候出现报错:
SQL*Loader-501: Unable to read file (test.csv)
SQL*Loader-566: partial record found at end of datafile
SQL*Loader-2026: the load was aborted because SQL Loader cannot continue.
可以在od的输出看到每行记录之间出现了一个空行,其实这个都是空格,需要注意的是这些空格会在导入后在数据库中保留,如下:
CJ@db11g> select deptno, ‘"‘ ||loc|| ‘"‘ loc from t; DEPTNO LOC ---------- ---------------------------------------------------- 10 "ora cle beanbee user " 20 "oracle bean bee user " 30 "oracle beanbee us er " |
后续的数据处理可以使用SQL的TRIM函数截断。
11)载入有换行符的数据(VAR)
CJ@db11g> desc t Name Null ? Type ----------------------------------------- -------- ---------------------------- DEPTNO NUMBER(5) DNAME VARCHAR2(14) LOC VARCHAR2(50) |
数据内容
04610,Sales,This is the Sales Office in Virginia 05620,Accounting,This is the Accounting Office in Virginia 05630,Consulting,This is the Consulting Office in Virginia 05040,Finance,This is the Finance Office in Virginia
LOAD DATA INFILE ‘test.csv‘ "var 3" INTO TABLE t REPLACE FIELDS TERMINATED BY ‘,‘ TRAILING NULLCOLS ( DEPTNO, DNAME, LOC ) |
CJ@db11g> select * from t; DEPTNO DNAME LOC ---------- -------------- -------------------------------------------------- 10 Sales This is the Sales Office in Virginia 20 Accounting This is the Accounting Office in Virginia 30 Consulting This is the Consulting Office in Virginia 40 Finance This is the Finance Office in Virginia |
12)载入有换行符的数据(STR)
CJ@db11g> desc t; Name Null ? Type ----------------------------------------- -------- ---------------------------- DEPTNO NUMBER(5) DNAME VARCHAR2(14) LOC VARCHAR2(50) |
数据内容
10,bean1,ora cle beanbee user| 20,bean2,oracle bean bee user1| 30,bean3,oracle beanbee us er12| 40,beanbee4,o racle be anbee us er123|
LOAD DATA INFILE ‘test.csv‘ "str x‘7C0A‘" INTO TABLE t REPLACE FIELDS TERMINATED BY ‘,‘ TRAILING NULLCOLS ( DEPTNO, DNAME, LOC ) |
CJ@db11g> select * from t; 10 bean1 ora cle beanbee user 20 bean2 oracle bean bee user1 30 bean3 oracle beanbee us er12 40 beanbee4 o racle be anbee us er123 |
CJ@db11g>select utl_raw.cast_to_raw(‘|‘||chr(10)) from dual; UTL_RAW.CAST_TO_RAW(‘|‘||CHR(10)) -------------------------------------------------------------------------------- 7C0A
13)使用nullif子句
CJ@db11g> create table t3 (x varchar2(10),y varchar2(10)); Table created. |
LOAD DATA INFILE * INTO TABLE t3 REPLACE ( x position(1:2) integer external nullif x= ‘1‘ , y position(3:8) ) BEGINDATA 1 10 20lg |
CJ@db11g> select * from t3; X Y ---------- ---------- 10 20 lg |
标签:
原文地址:http://www.cnblogs.com/zfswff/p/5720459.html