I am using SQL Loader to load data into tables from a flat file. Some sample data in the file might be:
我打算使用SQL Loader裝載來自平面文件數據。樣例數據如下:
12,smith,1234556@@1234567 @@876556612,1200表結構如下:
create table emp ( empno number(5) primary key, ename varchar2(10), Sal number(10,2) )create or replace directory my_dir as '/home/tkyte' / CREATE TABLE et ( "EMPNO" VARCHAR2(10), "ENAME" VARCHAR2(20), "TELNOS" VARCHAR2(1000), "SAL" VARCHAR2(10) ) ORGANIZATION external ( TYPE oracle_loader DEFAULT DIRECTORY MY_DIR ACCESS PARAMETERS ( RECORDS DELIMITED BY NEWLINE CHARACTERSET US7ASCII BADFILE 'MY_DIR':'t.bad' LOGFILE 't.log_xt' READSIZE 1048576 FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY '"' LDRTRIM MISSING FIELD VALUES ARE NULL REJECT ROWS WITH ALL NULL FIELDS ( "EMPNO" CHAR(255) TERMINATED BY "," OPTIONALLY ENCLOSED BY '"', "ENAME" CHAR(255) TERMINATED BY "," OPTIONALLY ENCLOSED BY '"', "TELNOS" CHAR(1000) TERMINATED BY "," OPTIONALLY ENCLOSED BY '"', "SAL" CHAR(255) TERMINATED BY "," OPTIONALLY ENCLOSED BY '"' ) ) location ( 't.dat' ) ) / SQL> select * from et; EMPNO ENAME TELNOS SAL ————— ————— ————————————————————————————————————————————— ———— 12 smith 1234556@@1234567@@876556612 1200 14 John 1234@@4567@@56789@@12345@@45778@@34566@@23456 1345
SQL> select empno, ename, sal, i, substr( tnos, instr( tnos, '@@', 1, i )+2, instr( tnos, '@@', 1, i+1 ) -instr( tnos, '@@', 1, i) - 2 ) tno from ( select to_number(et.empno) empno, et.ename, to_number(et.sal) sal, column_value i, '@@'||et.telnos||'@@' tnos from et, table( cast( multiset( select level from dual connect by level <= (length(et.telnos) -length(replace(et.telnos,'@@','')))/2+1 ) as sys.odciNumberList ) ) ) / EMPNO ENAME SAL I TNO ————— ————— ———— —— ——————— 12 smith 1200 1 1234556 12 smith 1200 2 1234567 12 smith 1200 3 876556612 14 John 1345 1 1234 14 John 1345 2 4567 14 John 1345 3 56789 14 John 1345 4 12345 14 John 1345 5 45778 14 John 1345 6 34566 14 John 1345 7 23456 10 rows selected.
SQL> create table emp 2 ( empno number primary key, 3 ename varchar2(10), 4 sal number 5 ); Table created. SQL> create table emp_contact 2 ( empno number references emp, 3 phone_no number 4 ); Table created. \ SQL> insert all when (i = 1) then into emp (empno,ename,sal) values (empno,ename,sal) when (i > 0) then into emp_contact(empno,phone_no) values (empno,tno) select empno, ename, sal, i, substr( tnos, instr( tnos, '@@', 1, i )+2, instr( tnos, '@@', 1, i+1 ) -instr( tnos, '@@', 1, i) - 2 ) tno from ( select to_number(et.empno) empno, et.ename, to_number(et.sal) sal, column_value i, '@@'||et.telnos||'@@' tnos from et, table( cast( multiset( select level from dual connect by level <= (length(et.telnos) -length(replace(et.telnos,'@@','')))/2+1 ) as sys.odciNumberList ) ) ) / 12 rows created. SQL> select * from emp; EMPNO ENAME SAL ————— —————— ————— 12 smith 1200 14 John 1345 SQL> select * from emp_contact; EMPNO PHONE_NO ——————— ————————— 12 1234556 12 1234567 12 876556612 14 1234 14 4567 14 56789 14 12345 14 45778 14 34566 14 23456 10 rows selected. ------------------------------------ Dylan Presents.