Oracle SQL*Loader使用指南(第一部分)
保存这篇文章有段时间了,感觉很实用,这里感谢原作者:Angel.John
SQL*Loader是Oracle数据库导入外部数据的一个工具.它和DB2的Load工具相似,但有更多的选择,它支持变化的加载模式,可选的加载及多表加载.
如何使用 SQL*Loader 工具
我们可以用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 )
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),
deptname position (08:27) char(20)
)
begindata
COSC COMPUTER SCIENCE
ENGL ENGLISH LITERATURE
MATH MATHEMATICS
POLY POLITICAL SCIENCE
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='c:\oradata' parameter
declare
fp utl_file.file_type;
begin
fp := utl_file.fopen('c:\oradata','tab1.txt','w');
utl_file.putf(fp, '%s, %s\n', 'TextField', 55);
utl_file.fclose(fp);
end;
/
当然你也可以使用第三方工具,如SQLWays ,TOAD for Quest等。
加载可变长度或指定长度的记录
如:
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,"
下面是导入固定位置(固定长度)数据示例:
LOAD DATA
INFILE *
INTO TABLE load_positional_data
( data1 POSITION(1:5),
data2 POSITION(6:15)
)
BEGINDATA
11111AAAAAAAAAA
22222BBBBBBBBBB
固定程度数据load 可以通过position 来指定
例子:
LOAD DATA
INFILE 'zipcodes.dat'
REPLACE INTO TABLE zipcodes (
city_name POSITION(1:10) CHAR(10),
zip_code POSITION(*:15) CHAR,
state_abbr POSITION(17-18) CHAR
)
city_name POSITION(1:10) CHAR(10),
The city name begins at position 1 and goes through position 10. In this case,
the length was specified redundantly in the datatype specification. A colon
was used to separate the beginning and ending values
zip_code POSITION(*:15) CHAR,
The * indicates that the ZIP Code begins with the first byte following the city
name. The ending position has been hardcoded as the 15th byte. A colon has
been used to separate the two values. No length has been specified with the
datatype, so SQL*Loader will compute the length as ending – beginning + 1.
state_abbr POSITION(17–18) CHAR
The state abbreviation has been specified in absolute terms. This time, a
hyphen has been used to separate the two values. Again, no length has been
specified with the datatype, so SQL*Loader will derive the length based on the
beginning and ending values.
跳过数据行:
可以用 "SKIP n" 关键字来指定导入时可以跳过多少行数据。如:
相关新闻>>
- 发表评论
-
- 最新评论 更多>>