SQL*Loader之CASE4

CASE4

1. SQL脚本

[[email protected] ulcase]$ cat ulcase4.sql

set termout off

rem host write sys$output "Building case 4 demonstration tables.  Please wait"

drop table emp;

create table emp
       (empno number(4) not null,
        ename char(10),
        job char(9),
        mgr number(4),
        hiredate date,
        sal number(7,2),
        comm number(7,2),
        deptno number(2));

create unique index empix on emp(empno);

exit

2. 控制文件

[[email protected] ulcase]$ cat ulcase4.ctl

-- NAME
-- ulcase4.ctl - SQL*Loader Case Study 4: Loading Combined Physical Records
--
-- DESCRIPTION
-- This case study demonstrates the following:
-- Combining multiple physical records to form one logical
-- record with CONTINUEIF.
--
-- Inserting negative numbers.
--
-- Using REPLACE to indicate that the table should be emptied
-- before the new data is inserted.
--
-- Specifying a discard file in the control file using DISCARDFILE.
--
-- Specifying a maximum number of discards using DISCARDMAX.
--
-- Rejecting records due to duplicate values in a unique index
-- or due to invalid data values.
--
-- TO RUN THIS CASE STUDY:
-- 1. Before executing this control file, log in to SQL*Plus as
--    scott/tiger. Enter @ulcase4 to execute the SQL script for
--    this case study. This prepares and populates tables and
--    then returns you to the system prompt.
--
-- 2. At the system prompt, invoke the case study as follows:
-- sqlldr USERID=scott/tiger CONTROL=ulcase4.ctl LOG=ulcase4.log
--
-- NOTES ABOUT THIS CONTROL FILE
-- DISCARDFILE specifies a discard file named ulcase4.dsc.
--
-- DISCARDMAX specifies a maximum of 999 discards allowed before
-- terminating the run. For all practical purposes, this allows
-- all discards for this test case. In real-world situations,
-- there may well be more than 999 discarded records.
--
-- REPLACE specifies that if there is data in the table being loaded,
-- then SQL*Loader should delete that data before loading new data.
--
-- CONTINUEIF specifies that if an asterisk is found in column 1
-- of the current record, then the next physical record after that
-- record should be appended to it from the logical record. Note that
-- column 1 in each physical record should then contain either an
-- asterisk or a nondata value.
--
-- The data file (ulcase4.dat) for this case study shows asterisks
-- in the first position and, though not visible, a newline character
-- is in position 20. Note that clark‘s commission is -10, and
-- SQL*Loader loads the value, converting it to a negative number.
--
-- The resulting log file will show that the last two records are
-- rejected, given two assumptions. If a unique index is created on
-- column empno, then the record for chin will be rejected because
-- his empno is identical to chan‘s. If empno is defined as NOT NULL,
-- then chen‘s record will be rejected because it has no value for
-- empno.

--
LOAD DATA
INFILE "ulcase4.dat"
DISCARDFILE "ulcase4.dsc"
DISCARDMAX 999
REPLACE
CONTINUEIF (1) = ‘*‘
INTO TABLE EMP

( 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,
  HIREDATE POSITION(52:60) INTEGER EXTERNAL)

3. 数据文件

[[email protected] ulcase]$ cat ulcase4.dat

*7782 CLARK      MA
 NAGER   7839  2572.50   -10    2512-NOV-85
*7839 KING       PR
 ESIDENT       5500.00          2505-APR-83
*7934 MILLER     CL
 ERK     7782   920.00          2508-MAY-80
*7566 JONES      MA
 NAGER   7839  3123.75          2517-JUL-85
*7499 ALLEN      SA
 LESMAN  7698  1600.00   300.00 25 3-JUN-84
*7654 MARTIN     SA
 LESMAN  7698  1312.50  1400.00 2521-DEC-85
*7658 CHAN       AN
 ALYST   7566  3450.00          2516-FEB-84
*     CHEN       AN
 ALYST   7566  3450.00          2516-FEB-84
*7658 CHIN       AN
 ALYST   7566  3450.00          2516-FEB-84

执行后结果:

[[email protected] ulcase]$ sqlplus scott/tiger @ulcase4.sql

[[email protected] ulcase]$ sqlldr userid=scott/tiger control=ulcase4.ctl

SQL> select * from emp;

EMPNO ENAME  JOB       MGR   HIREDATE  SAL     COMM DEPTNO
----- ------ --------- ----- --------- ------- ----- ------
 7782 CLARK  MANAGER   7839  12-NOV-85 2573     -10  25

 7839 KING   PRESIDENT       05-APR-83 5500          25

 7934 MILLER CLERK      7782 08-MAY-80  920          25

 7566 JONES  MANAGER    7839 17-JUL-85 3124          25 

 7499 ALLEN  SALESMAN   7698 03-JUN-84 1600     300  25

 7654 MARTIN SALESMAN   7698 21-DEC-85 1313    1400  25

 7658 CHAN   ANALYST    7566 16-FEB-84 3450          25

7 rows selected.

不难发现,数据文件中有9条数据,但是最后插入的只有7条数据。执行sqlldr时,如果没有显性指定日志文件名,则会隐性创建一个同名日志,后缀为.log。

我们不妨来看看日志文件的记录

[[email protected] ulcase]$ cat ulcase4.log

SQL*Loader: Release 11.2.0.1.0 - Production on Thu Sep 18 23:41:36 2014

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.

Control File:   ulcase4.ctl
Data File:      ulcase4.dat
  Bad File:     ulcase4.bad
  Discard File: ulcase4.dsc
 (Allow 999 discards)

Number to load: ALL
Number to skip: 0
Errors allowed: 50
Bind array:     64 rows, maximum of 256000 bytes
Continuation:   1:1 = 0X2a(character ‘*‘), in current physical record
Path used:      Conventional

Table EMP, loaded from every logical record.
Insert option in effect for this table: REPLACE

   Column Name                  Position   Len  Term Encl Datatype
------------------------------ ---------- ----- ---- ---- ---------------------
EMPNO                                 1:4     4           CHARACTER
ENAME                                6:15    10           CHARACTER
JOB                                 17:25     9           CHARACTER
MGR                                 27:30     4           CHARACTER
SAL                                 32:39     8           CHARACTER
COMM                                41:48     8           CHARACTER
DEPTNO                              50:51     2           CHARACTER
HIREDATE                            52:60     9           CHARACTER            

Record 8: Rejected - Error on table EMP, column EMPNO.
ORA-01400: cannot insert NULL into ("SCOTT"."EMP"."EMPNO")

Record 9: Rejected - Error on table EMP.
ORA-00001: unique constraint (SCOTT.EMPIX) violated

Table EMP:
  7 Rows successfully loaded.
  2 Rows not loaded due to data errors.
  0 Rows not loaded because all WHEN clauses were failed.
  0 Rows not loaded because all fields were null.

Space allocated for bind array:                   4608 bytes(64 rows)
Read   buffer bytes: 1048576

Total logical records skipped:          0
Total logical records read:             9
Total logical records rejected:         2
Total logical records discarded:        0

Run began on Thu Sep 18 23:41:36 2014
Run ended on Thu Sep 18 23:41:36 2014

Elapsed time was:     00:00:00.29
CPU time was:         00:00:00.07

可见,第8行记录因违法empno的非空约束,第9行记录因违反唯一索引而抛弃。

抛弃的两条记录可在控制文件指定的DISCARDFILE "ulcase4.dsc"中找到

[[email protected] ulcase]$ cat ulcase4.bad

*     CHEN       AN
 ALYST   7566  3450.00          2516-FEB-84
*7658 CHIN       AN
 ALYST   7566  3450.00          2516-FEB-84
时间: 2024-10-10 21:39:02

SQL*Loader之CASE4的相关文章

SQL*loader实验

SQL*Loader 用法: sqlldr keyword=value [,keyword=value,... ] SQL*Loader通过控制文件实现对记录的处理,例子命令行下sqlldr scott/tiger control=case.ctl(并非数据库sql>下) case.ctl内容如下: load data infile *  ##使用*表示加载的数据在控制文件中,如果是独立文件要使用'绝对路径名' into table bonus  ##指定要将数据加载到哪个表中,该表必须已经存在

SQL*Loader FAQ

SQL*Loader FAQ: Contents [hide] 1 What is SQL*Loader and what is it used for? 2 How does one use the SQL*Loader utility? 3 How does one load MS-Excel data into Oracle? 4 Is there a SQL*Unloader to download data to a flat file? 5 Can one load variable

Oracle中的sql loader

oracle 的sql loader用来将操作系统上的数据文件中的内容加载到oracle数据库中. 要使用sql loader,需要先创建一个控制文件,里面告诉sql loader在启动了sql loader之后的加载数据的策略. 新建一个studentdataload.ctl文件,写入   load data      infile 'studentdata.txt'      into table studentinfo      fields terminated by '|'      

SQL LOADER 的用法 TXT文件导入非常之快

前提,需要本地安装ORACLE 客户端 控制文件 cms.ctl load dataCHARACTERSET UTF8infile 'oracle.txt'APPEND INTO TABLE JR fields terminated by '|'(BUKRS,LONNO,WAERS,BLDAT,BUDAT,SHKZG,DLCOD,HKONT,FPGRP,FPNAM,LONPR,WRBTR,ECNMA,DLNAM,EVTCD,SEVTC,SEVTD,CITYC,CITY,LONTP,PAYER,R

Oracle Sql Loader的学习使用

最近由于遇到oracle控制文件的使用,虽然不是很复杂,但是从来没有用过,专门花点时间看看.点击 这里 查看详细 1,概述: Sql Loader: 一个批量工具,将文件数据导入到数据库.可以导入一个表或者多个表,甚至可以在导入时修改数据. 2,使用 a,你电脑需要装Oracle,不然你是找不到Sqlldr 这个命令的. 在控制输入台输入 sqlldr: 会列出相关的参数介绍. > sqlldr . . . Usage: SQLLDR keyword=value [,keyword=value,

SQL*Loader数据装载工具入门_超越OCP精通Oracle视频课程培训24

oracle视频教程目标     Oracle视频教程,风哥本套oracle教程培训学习Oracle SQL*Loader概念及用法及SQL*Loader两个案例,同时课件带有近20个案例用于大家自行练习.适用人群IT相关从业人员.Oracle数据库技术人员.想加工资的.想升职的都可以. 视频在线学习地址:http://edu.51cto.com/course/course_id-6987.html Oracle视频教程简介本课程介绍:Oracle视频教程,风哥本套oracle教程培训学习Ora

[Oracle] SQL*Loader 详细使用教程(2)- 命令行参数

sqlldr工具 SQL*Loader的客户端工具是sqlldr,在操作系统的命令行下输入sqlldr,后面不接任何参数,将显示帮助信息如下所示(所有命令行参数的简单描述及其默认值),所以你并不需要对下面的参数死记硬背,当你忘记它们时,可以通过这个方式快速查询. [plain] view plain copy print? Valid Keywords: userid -- ORACLE username/password control -- control file name log -- 

[Oracle] SQL*Loader 详细使用教程(4)- 字段列表

在上一篇中我们介绍了SQL*Loader中最重要的文件——控制文件,而本篇要介绍控制文件中最重要的部分——字段列表,字段列表的作用是把数据文件中的记录和数据库中表的列对应起来,下面是字段列表的一个例子,本篇我们将一一讲解它们的意思. . . . 1 (hiredate SYSDATE, 2 deptno POSITION(1:2) INTEGER EXTERNAL(2) NULLIF deptno=BLANKS, 3 job POSITION(7:14) CHAR TERMINATED BY W

SQL*Loader之CASE1

最近项目涉及到将文本文件中的数据导入到Oracle数据库中,故研究了下SQL*Loader,官档提供的资料不是很丰富,很多案例中出现的语句在官档中找不到出处.但它提供的案例本身却彰显出了SQL*Loader功能的强大.鉴于Oracle 11g的软件本身没有携带这些案例,需要专门到官方网站下载Oracle Database 11g Release 2 Examples,甚是麻烦.在此,将这些案例分享,也方便以后研究.借鉴. 因官方文档还没有研究完,手里还有本<Oracle SQL*Loader: