说明:下面只是简单地介绍一些命令。
我们可以用Oracle的sqlldr工具来导入数据。例如:
sqlldr scott/tiger control=loader.ctl
控制文件(loader.ctl) 将加载一个外部数据文件(含分隔符). loader.ctl如下:
load data
infile 'c:\data\mydata.csv'
into table emp
fields terminated by "," optionally enclosed by '"' --代表 ""
(empno,empname,sal,deptno)
c:\data\mydata.csv文件如下内容:
10001,"Scott Tiger",1000,40
10002,"Frank Naude",500,20
下面是一个指定记录长度的示例控制文件。
"*" 代表数据文件与此文件同名,即在后面使用BEGINDATA段来标识数据。
load data
infile * --说明操作的是本文件
replace
into table departments
( dept position (02:05) char(4),--从第二个开始,截4个字符串
deptname position (08:27) char(20))
begindata
COSC COMPUTER SCIENCE --截后OSC MPUTER SCIENCE --后面存在空格
ENGL ENGLISH LITERATURE
MATH MATHEMATICS
POLY POLITICAL SCIENCE
--CMD>sqlldr userid=scott/tiger@clement
control='E:\sqllder\sqllder_01.ctl' log=d:\load\demo01.log
Unloader这样的工具
Oracle 没有提供将数据导出到一个文件的工具。但是,
我们可以用SQL*Plus的select 及 format 数据来输出到一个文件:
set echo off newpage 0 space 0 pagesize 0 feed off head off trimspool on
spool oradata.txt
select col1 || ',' || col2 || ',' || col3 from tab1 where col2 = 'XYZ';
spool off
另外,也可以使用使用 UTL_FILE PL/SQL 包处理:
REM Remember to update initSID.ora, utl_file_dir='D:\oralce_log\utl' parameter
declare
oraload utl_file.file_type;
CURSOR sql_out IS
SELECT EMPNO||' ' || ENAME ||' ' || JOB || ' ' ||MGR || ' ' ||
HIREDATE || ' ' || SAL ||' '||COMM as A from emp_001;
sql_out_all sql_out%ROWTYPE;
begin
oraload := utl_file.fopen('D:\oralce_log\utl', 'tab1.txt', 'w');
OPEN sql_out;
LOOP
FETCH sql_out INTO sql_out_all;
EXIT WHEN sql_out%NOTFOUND;
utl_file.putf(oraload, '%s,%s\n',sql_out_all.A,'');
end loop;
utl_file.fclose(oraload);
end;
/
--下面是IBM网站的详细介绍
当然你也可以使用第三方工具,如SQLWays ,TOAD for Quest等。
1、加载可变长度或指定长度的记录
如:
LOAD DATA
INFILE *
INTO TABLE load_delimited_data
FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(data1,
data2)
BEGINDATA
11111,AAAAAAAAAA
22222,"A,B,C,D,"
2、下面是导入固定位置(固定长度)数据示例:
LOAD DATA
INFILE *
INTO TABLE load_positional_data
( data1 POSITION(1:5),
data2 POSITION(6:15))
BEGINDATA
11111AAAAAAAAAA
22222BBBBBBBBBB
3、跳过数据行:
可以用 "SKIP n" 关键字来指定导入时可以跳过多少行数据。如:
LOAD DATA
INFILE *
INTO TABLE load_positional_data
SKIP 5
( data1 POSITION(1:5),
data2 POSITION(6:15))
BEGINDATA
11111AAAAAAAAAA
22222BBBBBBBBBB
4、导入数据时修改数据:
在导入数据到数据库时,可以修改数据。
注意,这仅适合于常规导入,并不适合 direct导入方式.如:
a、LOAD DATA
INFILE *
INTO TABLE modified_data
( rec_no "my_db_sequence.nextval",
region CONSTANT '31',
time_loaded "to_char(SYSDATE, 'HH24:MI')",
data1 POSITION(1:5) ":data1/100",
data2 POSITION(6:15) "upper(:data2)",
data3 POSITION(16:22)"to_date(:data3, 'YYMMDD')")
BEGINDATA
11111AAAAAAAAAA991201
22222BBBBBBBBBB990112
b、LOAD DATA
INFILE 'mail_orders.txt'
BADFILE 'bad_orders.txt'
APPEND
INTO TABLE mailing_list
FIELDS TERMINATED BY ","
(addr,
city,
state,
zipcode,
mailing_addr "decode(:mailing_addr, null, :addr, :mailing_addr)",
mailing_city "decode(:mailing_city, null, :city, :mailing_city)",
mailing_state)
5、将数据导入多个表:
LOAD DATA
INFILE *
REPLACE
INTO TABLE emp
WHEN empno != ' '
(empno POSITION(1:4) INTEGER EXTERNAL,
ename POSITION(6:15) CHAR,
deptno POSITION(17:18) CHAR,
mgr POSITION(20:23) INTEGER EXTERNAL)
INTO TABLE proj
WHEN projno != ' '
(projno POSITION(25:27) INTEGER EXTERNAL,
empno POSITION(1:4) INTEGER EXTERNAL)
6、导入选定的记录:
如下例: (01) 代表第一个字符, (30:37) 代表30到37之间的字符:
LOAD DATA
INFILE 'mydata.dat' BADFILE 'mydata.bad' DISCARDFILE 'mydata.dis'
APPEND
INTO TABLE my_selective_table
WHEN (01) <> 'H' and (01) <> 'T' and (30:37) = '19991217'
(region CONSTANT '31',
service_key POSITION(01:11) INTEGER EXTERNAL,
call_b_no POSITION(12:29) CHAR)
7、导入时跳过某些字段:
可用 POSTION(x:y)来分隔数据. 在Oracle8i中可以通过指定FILLER字段实现。
FILLER字段用来跳过、忽略导入数据文件中的字段.如:
LOAD DATA
TRUNCATE INTO TABLE T1
FIELDS TERMINATED BY ','
(field1,
field2 FILLER,
field3)
8、导入多行记录:
可以使用下面两个选项之一来实现将多行数据导入为一个记录:
CONCATENATE: - use when SQL*Loader should combine the same
number of physical records together to form one logical record.
CONTINUEIF - use if a condition indicates that multiple records should
be treated as one. Eg. by having a '#' character in column 1.
9、Loading XML
LOAD DATA
INFILE *
INTO TABLE po_tab
APPEND
XMLTYPE (xmldata)
FIELDS
(xmldata CHAR(2000))
desc po_tab
--CMD>sqlldr userid=uwclass/uwclass control=c:\load\demo11.ctl
log=c:\load\demo11.log
set long 1000000
SELECT * FROM po_tab;
SELECT * FROM po_tab WHERE sys_nc_rowinfo$ LIKE '%Hurry%';