程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> Oracle數據庫 >> Oracle教程 >> 使用ORACLE外部表裝載復雜數據

使用ORACLE外部表裝載復雜數據

編輯:Oracle教程

使用ORACLE外部表裝載復雜數據


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
14,John,1234@@4567@@56789 @@12345@@45778@@34566@@23456,1345



The field values are empno, empname, phone numbers, and salary. The table structures are

表結構如下:

create table emp ( empno number(5) primary key, ename varchar2(10), Sal number(10,2) )
create table emp_contact ( empno references emp, phone_no number(10) )


I want to insert the data into the emp and emp_contact tables. I don’t know how many values for phone_no the file contains (the number of values for phone_no is not fixed). How do I insert the data into the tables?
我想將以上數據分別裝入2個表中: emp和emp_contact
問題是:我無法確定第三列(電話號碼)有多少個,此列不固定。我將如何將數據裝載進表?


這個問題很狡猾,看上去我們必須將第三列拆成多行插入。
對於號稱“21世紀神奇數據裝載工具”--SQL Loader 來講也無法直接實現!!!
此處,我建議使用21世紀數據裝載工具--外部表 解決。思路是:將平面文件裝入外部表,然後通過編寫SQL進行拆分,最後按特定插入規則一次性插入指定表中。
下面,看我演示:
--1)進行數據裝載
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



--2)編寫拆分SQL
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.



--注意:這裡使用了cast multiset語法,column_value是TABLE(odciNumberList)中一列




--3)編寫插入SQL
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.



  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved