`

SQL*Loader

阅读更多

 

Oracle SQL*Loader
Version 10.2
 
General
Note: This page consists of a series of demonstrations of various SQL*Loader capabilities. It is by no means complete.

For the Oracle doc:
http://download-west.oracle.com/docs/cd/B19306_01/server.102/b14215/app_ldr_syntax.htm#i631434
SQL Loader Data Types CHAR
DECIMAL EXTERNAL
INTEGER EXTERNAL
Modes APPEND
INSERT
REPLACE
TRUNCATE
INFILE INFILE * or INFILE '<file_name>'
[RECSIZE <integer> BUFFERS <integer>]
INFILE 'mydata.dat' "RECSIZE 80 BUFFERS 8"
INTO INTO <table_name>
INTO TABLE emp
BADFILE

Records with formatting errors or that cause Oracle errors
BADFILE '<file_name>'
BADFILE 'sample.bad'
DISCARDFILE

Records not satisfying a WHEN clause
DISCARDFILE '<file_name>'
DISCARDMAX <integer>
DISCARDFILE 'sample.dsc'
CHARACTERSET CHARACTERSET <character_set_name>
CHARACTERSET WE8MSWIN1252
LENGTH LENGTH SEMANTICS <BYTE | CHAR>
LENGTH SEMANTICS BYTE
-- this is the default for all character sets except UTF16
LOAD TYPES APPEND
INSERT
REPLACE
TRUNCATE
APPEND
OPTIONS CLAUSE BINDSIZE = n
COLUMNARRAYROWS = n
DIRECT = {TRUE | FALSE} 
ERRORS = n
LOAD = n 
MULTITHREADING = {TRUE | FALSE}
PARALLEL = {TRUE | FALSE}
READSIZE = n
RESUMABLE = {TRUE | FALSE}
RESUMABLE_NAME = 'text string'
RESUMABLE_TIMEOUT = n
ROWS = n 
SILENT = {HEADER | FEEDBACK | ERRORS | DISCARDS | PARTITIONS | ALL}
SKIP = n 
SKIP_INDEX_MAINTENANCE = {TRUE | FALSE}
SKIP_UNUSABLE_INDEXES = {TRUE | FALSE}
STREAMSIZE = n
OPTIONS (BINDSIZE=100000, SILENT=(ERRORS, FEEDBACK))
PATHS CONVENTIONAL PATH
DIRECT PATH

All loads demonstrated below are convention with the exception of demo 6.
 
TERMINATORS
Comma

','

Tab 0x'09'
TRAILING NULLCOLS TRAILING NULLCOLS
-- assuming this data
10 Accounting

-- the following
INTO TABLE dept 
TRAILING NULLCOLS 
( deptno CHAR TERMINATED BY " ", 
dname CHAR TERMINATED BY WHITESPACE, 
loc CHAR TERMINATED BY WHITESPACE) 

-- would generate an error without TRAILING NULLCOLS 
-- as it doesn't have loc data
WHEN WHEN <condition>
See Demo 5 below
 
Assembling Logical Records
CONCATENATE CONCATENATE <number_of_physical_records>
CONCATENATE 3
CONTINUEIF CONTINUEIF THIS [PRESERVE] (start_position:end_position) = value
CONTINUEIF THIS (1:2) = '%%'

CONTINUEIF THIS PRESERVE (1:2) = '%%'
CONTINUEIF CONTINUEIF NEXT [PRESERVE] (start_position:end_position) = value
CONTINUEIF NEXT (1:2) = '%%'

CONTINUEIF NEXT PRESERVE (1:2) = '%%'
CONTINUEIF CONTINUEIF LAST (start_position:end_position) = value
-- Tests against the last non-blank character.
-- Allows only a single character for the test
PRESERVE Preserves the CONTINUEIF characters
 
Demo Tables & Data

Demo Tables
CREATE TABLE dept (
deptno   VARCHAR2(2),
dname    VARCHAR2(20),
loc      VARCHAR2(20));

CREATE TABLE emp (
empno    NUMBER(4),
ename    VARCHAR2(10),
job      VARCHAR2(10),
mgr      NUMBER(4),
hiredate DATE,
sal      NUMBER(8,2),
comm     NUMBER(7,2),
deptno   NUMBER(2),
projno   NUMBER(4),
loadseq  NUMBER(3));

CREATE TABLE proj (
emp      NUMBER(4),
projno   NUMBER(3));

CREATE TABLE funcdemo (
last_name  VARCHAR2(20),
first_name VARCHAR2(20));

CREATE TABLE decodemo (
fld1    VARCHAR2(20),
fld2    VARCHAR2(20));

CREATE TABLE denver_prj (
projno  VARCHAR2(3),
empno   NUMBER(5),
projhrs NUMBER(2));

CREATE TABLE orlando_prj (
projno  VARCHAR2(3),
empno   NUMBER(5),
projhrs NUMBER(2));

CREATE TABLE misc_prj (
projno  VARCHAR2(3),
empno   NUMBER(5),
projhrs NUMBER(2));

CREATE TABLE po_tab OF XMLTYPE;

CREATE TABLE loadnums(
col1 VARCHAR2(10),
col2 NUMBER);
 
Demo 1
Basic import of delimited data with data in the control file

Control File

OPTIONS (ERRORS=500, SILENT=(FEEDBACK))
LOAD DATA
INFILE *
INTO TABLE <table_name>
FIELDS TERMINATED BY <delimiter>
OPTIONALLY ENCLOSED BY <enclosing character>
(<column_name>, <column_name>, <column_name>)
sqlldr userid=uwclass/uwclass control=c:\load\demo01.ctl log=d:\load\demo01.log
 
Demo 2
Basic import of fixed length data with separate data and control files

Control File

Data File

LOAD DATA
INFILE <data_file_path_and_name>
INTO TABLE <table_name> (
<column_name> POSITION(<integer>:<integer>) <data_type>,
<column_name> POSITION(<integer>:<integer>) <data_type>,
<column_name> POSITION(<integer>:<integer>) <data_type>)
sqlldr userid=uwclass/uwclass control=c:\load\demo02.ctl log=c:\load\demo02.log
 
Demo 3
Append of delimited data with data in the control file. This sample demonstrates date formating, delimiters within delimiters and implementation of record numbering with a SQL*Loader sequence. APPEND indicates that the table need not be empty before the SQL*Loader is run.

Control File

LOAD DATA
INFILE *
APPEND
INTO TABLE emp
FIELDS TERMINATED BY ","
OPTIONALLY ENCLOSED BY '"'
(<column_name>, <column_name> DATE "DD-Month-YYYY",
<column_name> CHAR TERMINATED BY ':',
<column_name> SEQUENCE(MAX,1))
sqlldr userid=uwclass/uwclass control=c:\load\demo03.ctl log=c:\load\demo3.log
 
Demo 4
Replace of fixed length data with separate data and control file. This sample demonstrates specifying a discard file, the maximum number of records to discard (DISCARDMAX), and CONTINUEIF ( where it looks for an asterisk in the first position to determine if a new line has started.
Control File

Data File
LOAD DATA
INFILE 'c: emp\demo04.dat'
DISCARDFILE 'c: emp\demo4.dsc'
DISCARDMAX 999
REPLACE
CONTINUEIF THIS (1) = '*'

INTO TABLE emp (
empno    POSITION(1:4)   INTEGER EXTERNAL,
ename    POSITION(6:15)  CHAR,
hiredate POSITION(52:60) INTEGER EXTERNAL)
sqlldr userid=uwclass/uwclass control=c:\load\demo04.ctl log=c:\load\demo4.log
 
Demo 5
Loading into multiple tables during an import using the WHEN keyword. The control file loads two different tables making three passes at one of them. Note the problem with the Doolittle record and how it is handled.

Control File

Data File

LOAD DATA
INFILE 'c: emp\demo05.dat'
BADFILE 'c: emp\bad05.bad'
DISCARDFILE 'c: emp\disc05.dsc'
REPLACE

INTO TABLE emp (
empno  POSITION(1:4)   INTEGER EXTERNAL,
ename  POSITION(6:15)  CHAR,
deptno POSITION(17:18) CHAR,
mgr    POSITION(20:23) INTEGER EXTERNAL)

--1st project: proj has two columns, both not null
INTO TABLE proj
WHEN projno != ' ' (
emp    POSITION(1:4)   INTEGER EXTERNAL,
projno POSITION(25:27) INTEGER EXTERNAL)

-- 2nd project
INTO TABLE proj
WHEN projno != ' ' (
emp    POSITION(1:4)   INTEGER EXTERNAL,
projno POSITION(29:31) INTEGER EXTERNAL)

-- 3rd project
INTO TABLE proj
WHEN projno != ' ' (
emp    POSITION(1:4)   INTEGER EXTERNAL,
projno POSITION(33:35) INTEGER EXTERNAL)
sqlldr userid=uwclass/uwclass control=c:\load\demo5.ctl log=d:\load\demo5.log
 
Demo 6
Using the NULLIF and BLANKS keywords to handle zero length strings being loaded into numeric columns. Also note the use of Direct Path Load in the control file (DIRECT=TRUE).

Control File

Data File

LOAD DATA
INFILE 'c: emp\demo06.dat'
INSERT
INTO TABLE emp
-- SORTED INDEXES (emp_empno)
(
empno  POSITION(01:04) INTEGER EXTERNAL NULLIF empno=BLANKS,
ename  POSITION(06:15) CHAR,
job    POSITION(17:25) CHAR,
mgr    POSITION(27:30) INTEGER EXTERNAL NULLIF mgr=BLANKS,
sal    POSITION(32:39) DECIMAL EXTERNAL NULLIF sal=BLANKS,
comm   POSITION(41:48) DECIMAL EXTERNAL NULLIF comm=BLANKS,
deptno POSITION(50:51) INTEGER EXTERNAL NULLIF deptno=BLANKS)
sqlldr userid=uwclass/uwclass control=c:\load\demo06.ctl log=c:\load\demo06.log DIRECT=TRUE
 
Demo 7
Using a buit-in function to modify data during loading

Control File

LOAD DATA
INFILE *
INSERT
INTO TABLE funcdemo
(
LAST_NAME  position(1:7)  CHAR "UPPER(:LAST_NAME)",
FIRST_NAME position(8:15) CHAR "LOWER(:FIRST_NAME)"
)
BEGINDATA
Locke Phil
Gorman Tim
sqlldr userid=uwclass/uwclass control=c:\load\demo07.ctl log=c:\load\demo07.log
 
Demo 8
Another example of using a built-in function, in this case DECODE, to modify data during loading

Control File

LOAD DATA
INFILE *
INSERT
INTO TABLE decodemo
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
(
fld1, 
fld2 "DECODE(:fld1, 'hello', 'goodbye', :fld1)"
)
BEGINDATA
hello,""
goodbye,""
this is a test,""
hello,""
sqlldr userid=uwclass/uwclass control=c:\load\demo08.ctl log=c:\load\demo08.log
 
Demo 9
Loading multiple files into multiple tables in a singe control file. Note the use of the WHEN keyword.

Control File

Data File

Data File

LOAD DATA
INFILE 'c: emp\demo09a.dat'
INFILE 'c: emp\demo09b.dat'

APPEND
INTO TABLE denver_prj
WHEN projno = '101' (
projno  position(1:3)  CHAR,
empno   position(4:8)  INTEGER EXTERNAL,
projhrs position(9:10) INTEGER EXTERNAL)

INTO TABLE orlando_prj
WHEN projno = '202' (
projno  position(1:3)  CHAR,
empno   position(4:8)  INTEGER EXTERNAL,
projhrs position(9:10) INTEGER EXTERNAL)

INTO TABLE misc_prj
WHEN projno != '101' AND projno != '202' (
projno  position(1:3)  CHAR,
empno   position(4:8)  INTEGER EXTERNAL,
projhrs position(9:10) INTEGER EXTERNAL)
sqlldr userid=uwclass/uwclass control=c:\load\demo09.ctl log=c:\load\demo09.log
 
Demo 10
Loading negative numeric values. Note Clark and Miller's records in the data file. Note empty row

Control File

Data File

LOAD DATA
INFILE 'c: emp\demo10.dat'
INTO TABLE emp
REJECT ROWS WITH ALL NULL FIELDS
(
empno  POSITION(01:04) INTEGER EXTERNAL,
ename  POSITION(06:15) CHAR,
job    POSITION(17:25) CHAR,
mgr    POSITION(27:30) INTEGER EXTERNAL,
sal    POSITION(32:39) DECIMAL EXTERNAL,
comm   POSITION(41:48) DECIMAL EXTERNAL,
deptno POSITION(50:51) INTEGER EXTERNAL)
sqlldr userid=uwclass/uwclass control=c:\load\demo10.ctl log=c:\load\demo10.log
 
Demo 11
Loading XML

Control File

LOAD DATA
INFILE *
INTO TABLE po_tab
APPEND
XMLTYPE (xmldata)
FIELDS
(xmldata CHAR(2000))
desc po_tab

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%';
 
Demo 12
Loading a CONSTANT, RECNUM, and SYSDATE

Control File

OPTIONS (ERRORS=100, SILENT=(FEEDBACK))
LOAD DATA
INFILE *
REPLACE
INTO TABLE dept
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
(recno RECNUM, deptno CONSTANT "XX", dname, loc, tdate SYSDATE)
ALTER TABLE dept
ADD (recno NUMBER(5), tdate DATE);

desc dept

sqlldr userid=uwclass/uwclass control=c:\load\demo12.ctl log=c:\load\demo12.log
 
Demo 13
Setting READSIZE and BINDSIZE


The control file and data for 
this demo can be found in 
/demo/schema/sales_history/
schema under
as cust1v3.ctl and cust1v3.dat

BINDSIZE and READSIZE 
do not apply to Direct Path Loads

LOAD DATA
INFILE 'c: emp\cust1v3.dat'
INTO TABLE CUSTOMERS
TRUNCATE
FIELDS TERMINATED BY '|'
OPTIONALLY ENCLOSED BY '"'
(CUST_ID, CUST_FIRST_NAME, CUST_LAST_NAME, CUST_GENDER, CUST_YEAR_OF_BIRTH, CUST_MARITAL_STATUS, CUST_STREET_ADDRESS, CUST_POSTAL_CODE, CUST_CITY, CUST_CITY_ID, CUST_STATE_PROVINCE, CUST_STATE_PROVINCE_ID, COUNTRY_ID, CUST_MAIN_PHONE_NUMBER, CUST_INCOME_LEVEL, CUST_CREDIT_LIMIT, CUST_EMAIL, CUST_TOTAL, CUST_TOTAL_ID, CUST_SRC_ID, CUST_EFF_FROM DATE(19) "YYYY-MM-DD-HH24-MI-SS", CUST_EFF_TO DATE(19) "YYYY-MM-DD-HH24-MI-SS", CUST_VALID)
conn sh/sh

GRANT select ON customers TO uwclass;

conn uwclass/uwclass

CREATE TABLE customers AS
SELECT * FROM sh.customers
WHERE 1=2;

desc customers

-- run 1 - default sizing
sqlldr userid=uwclass/uwclass control=c:\load\demo13.ctl log=c:\load\demo13.log

Space allocated for bind array: 251252 bytes(46 rows)
Read buffer bytes: 1048576
Elapsed time was: 00:00:03.73
CPU time was: 00:00:01.25

-- run 2 - double default to 2M
sqlldr userid=uwclass/uwclass control=c:\load\demo13.ctl log=c:\load\demo13.log readsize=2048000 bindsize=2048000 rows=64

Space allocated for bind array: 349568 bytes(64 rows)
Read buffer bytes: 2048000

Elapsed time was: 00:00:03.50
CPU time was: 00:00:01.09

-- run 3 - double default to 4M
sqlldr userid=uwclass/uwclass control=c:\load\demo13.ctl
log=c:\load\demo13.log readsize=4096000 bindsize=4096000 rows=64

Space allocated for bind array: 349568 bytes(64 rows)
Read buffer bytes: 4096000

Elapsed time was: 00:00:03.65
CPU time was: 00:00:01.12
 
Demo 14
Trailing


Load Numbers with trailing + and - signs

LOAD DATA
INFILE *
TRUNCATE
INTO TABLE loadnums (
col1 position(1:5),
col2 position(7:16) "TO_NUMBER(:col2,'99,999.99MI')")
BEGINDATA
abcde 1,234.99-
abcde 11,234.34+
abcde 45.23-
abcde 99,234.38-
abcde 23,234.23+
abcde 98,234.23+
sqlldr userid=uwclass/uwclass control=c:\load\demo14.ctl log=c:\load\demo09.log
分享到:
评论

相关推荐

    SQL*Loader详细介绍

    4. **运行SQL*Loader** 使用命令行工具sqlldr或通过PL/SQL调用DBMS_LOADER包来执行SQL*Loader。命令行语法大致如下: ``` sqlldr user/pass@database control=control_file_name data=data_file_name ``` 5. **...

    SQL*Loader.zip

    SQL*Loader是Oracle数据库系统中的一个实用工具,用于高效地将大量数据批量导入到数据库中。这个工具在处理大量数据的场景下非常有用,比如在数据迁移、数据分析或数据库初始化时。"SQL*Loader.zip"可能包含了一份...

    sql*loader 指南

    ### SQL*Loader 指南:语法与用法详解 #### 一、SQL*Loader简介 SQL*Loader是Oracle提供的一款高效数据加载工具,用于将外部文件中的数据批量加载到Oracle数据库表中。该工具支持多种文件格式,包括CSV、文本文件...

    SQL*Loader-Oracle高速数据装入最佳工具软件.pdf

    标题:“SQL*Loader-Oracle高速数据装入最佳工具软件.pdf”描述:“SQL*Loader-Oracle高速数据装入最佳工具软件.pdf”标签:“SQL 数据库 数据处理 参考文献 专业指导” 在当今信息飞速发展的时代,统计信息化建设...

    SQL LOADER错误小结

    在使用SQL*Loader进行数据加载时,我们可能会遇到多种错误情况。SQL*Loader是一个Oracle数据库提供的工具,用于高效地从平面文件批量导入数据。以下是一些常见错误的总结及其解决方案: 1. **编码错误导致的乱码...

    基于SQL*Loader的海量数据装载方案优化.pdf

    SQL*Loader是Oracle数据库系统中一个强大的工具,它能够快速地将大量数据从文本文件加载到数据库表中。本文档将深入探讨如何针对大规模数据装载场景优化SQL*Loader的使用,以提高数据处理效率和性能。 首先,理解...

    使用SQL*Loader将dBASE数据转储到ORACLE数据库.pdf

    4. **运行SQL*Loader**:执行SQL*Loader命令,指定数据文件和控制文件路径。 5. **检查结果**:分析运行情况记录文件和坏文件,处理错误或遗漏的数据。 6. **重试或优化**:根据错误记录,修复数据文件或调整控制...

    Oracle数据库装载外部数据工具SQL*Loader的应用 (1).pdf

    Oracle数据库的SQL*Loader工具是用于快速批量导入大量外部数据到Oracle数据库的高效工具。它在操作系统命令行环境中运行,可以处理多种格式的数据文件,包括固定格式、自由定界格式和变长格式。SQL*Loader的功能强大...

    SQL * Loader的使用技巧.pdf

    SQL*Loader是Oracle数据库系统提供的一种高效的数据导入工具,它能够快速地将文本文件中的数据加载到Oracle数据库中。在使用SQL*Loader之前,需要准备两份关键文件:数据文件和控制文件。 数据文件通常包含待导入的...

    Oracle数据库装载外部数据工具SQL*Loader的应用.pdf

    在进行数据库的数据装载时,Oracle提供了SQL*Loader工具,这是一个强大的数据导入程序,能够将外部数据文件导入Oracle数据库表中。以下是对Oracle数据库装载外部数据工具SQL*Loader应用的详细知识点介绍。 首先,...

    sqlloader详解

    ### SQL*Loader详解 #### 一、SQL*Loader概述 SQL*Loader是Oracle数据库系统中一个强大而灵活的工具,其主要功能是从各种外部数据源批量导入数据到Oracle数据库。随着现代数据库规模的不断扩大,从GB级扩展到TB级...

    如何使用ORACLE中的SQL*LOADER.pdf

    3. **运行SQL*Loader**: 在命令行中,使用以下命令运行SQL*Loader: ``` sqlldr username/password@database CONTROL=control_file.ctl ``` 这里的`username`、`password`和`database`应替换为实际的Oracle...

    sqlloader

    ### SQL*Loader:高效数据加载工具 #### 引言 SQL*Loader是Oracle提供的一款强大且灵活的工具,专门用于将大量数据从平面文件(如逗号分隔值CSV文件)高效地导入到Oracle数据库中。它不仅适用于处理标准的文本格式...

    sqlloader的成功案例

    在IT领域,尤其是在数据库管理与数据导入导出技术中,Oracle SQL*Loader是一个非常重要的工具。SQL*Loader的成功案例不仅展示了其高效的数据加载能力,还体现了它在处理大量数据时的灵活性与可靠性。本文将根据提供...

    [Oracle] 常用工具集之SQL*Loader的用法

    SQL*Loader是Oracle数据库系统中的一个强大工具,用于快速、高效地将大量数据批量加载到数据库中。尤其在处理业务分析或数据仓库场景时,它的优势显著,能够处理各种格式的平面文件,使得数据导入效率远超传统的逐条...

    SQL_LOADER教程

    ### SQL_LOADER教程详解 #### 一、SQL*LOADER简介 SQL*LOADER是Oracle提供的一款高效的数据加载工具,主要用于将操作系统中的文件批量导入到Oracle数据库中。它支持多种数据格式,包括纯文本文件、Excel文件等,...

    利用 sqluldr2导出数据 使用sqlldr导入数据 通过merge into 合并更新数据

    Oracle数据库提供了一套强大的工具,包括SQL*Loader(简称SQLLDR)和SQL Ultra Data Recorder 2(SQLULDR2),来帮助我们处理这些任务。下面将详细阐述如何使用这两个工具以及如何通过`MERGE INTO`语句进行数据合并...

    sqlldr报超出字符长度错误

    ### SQL*Loader 超出字符长度错误解析与解决方案 #### 概述 在使用SQL*Loader工具导入数据时,可能会遇到超出字符长度限制的错误。这类问题通常出现在需要将包含较长字符串的数据加载到表中时,而目标列定义的长度...

    sqlloader数据导入详解

    ### SQL*Loader 数据导入详解 #### 一、SQL*Loader简介 SQL*Loader 是 Oracle 数据库下用于数据导入的一种重要工具,它由 Oracle 客户端提供。此工具支持多种数据格式,能够高效地将外部文件中的数据加载到 Oracle ...

Global site tag (gtag.js) - Google Analytics