SQL*Loader之CASE7

CASE7

1. SQL脚本

case7包含两个SQL脚本,一个是删除脚本ulcase7e.sql,一个是创建脚本ulcase7s.sql

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

set termout off
rem host write sys$output "Cleaning up Case 7 Trigger and Package."

DROP PACKAGE uldemo7;
DROP TRIGGER uldemo7_emp_insert;

EXIT

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

set termout off
rem host write sys$output "Building Package and Trigger for Case 7.Please wait"

CREATE OR REPLACE PACKAGE uldemo7 AS
    last_deptno  NUMBER;
    last_job     CHAR(9);
    last_mgr     NUMBER;
END uldemo7;
/

CREATE OR REPLACE TRIGGER uldemo7_emp_insert
  BEFORE INSERT ON emp
  FOR EACH ROW

  BEGIN
  IF :new.deptno IS NOT NULL THEN
     uldemo7.last_deptno := :new.deptno;   -- save value for later use
  ELSE
     :new.deptno := uldemo7.last_deptno;   -- use last valid value
  END IF;

  IF :new.job IS NOT NULL THEN
     uldemo7.last_job := :new.job;   -- save value for later use
  ELSE
     :new.job := uldemo7.last_job;   -- use last valid value
  END IF;

  IF :new.mgr IS NOT NULL THEN
     uldemo7.last_mgr := :new.mgr;   -- save value for later use
  ELSE
     :new.mgr := uldemo7.last_mgr;   -- use last valid value
  END IF;

  END;
/

EXIT

其实,两个脚本可以并在一起,即先删除,再创建

2. 控制文件

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

-- Copyright (c) 1991, 2004 Oracle. All rights reserved.
-- NAME
-- ulcase7.ctl - Extracting Data From a Formatted Report
--
-- DESCRIPTION
-- This case study demonstrates the following:
-- Use of SQL*Loader with an INSERT trigger.
--
-- Use of the SQL string to manipulate data.
--
-- Use of different initial and trailing delimiters.
--
-- Use of SYSDATE.
--
-- Use of the TRAILING NULLCOLS clause.
--
-- Ambiguous field length warnings.
--
-- Use of a discard file.
--
-- TO RUN THIS CASE STUDY:
-- 1. Before executing this control file, log in to SQL*Plus as
--    scott/tiger. Enter @ulcase7s to execute the SQL script for
--    this case study. This creates a BEFORE INSERT trigger that
--    is required to fill in the department number, job name,
--    and manager‘s number when these fields are not present on
--    a data line. When values are present, they should be saved
--    in a global variable. When values are not present, the
--    global variables are used.
--
-- 2. At the system prompt, invoke the case study as follows:
-- sqlldr USERID=scott/tiger CONTROL=ulcase7.ctl LOG=ulcase7.log
--
-- 3. After you have run the case study and finished with it, you
--    must run the ulcase7e.sql script before you can successfully
--    run other case studies. This script drops the INSERT trigger
--    and the global variables package. Log in to SQL*Plus as
--    scott/tiger. Enter @ulcase7e.
--
-- NOTES ABOUT THIS CONTROL FILE
-- The WHEN (57) = ‘.‘ clause indicates that the decimal point
-- in column 57 (the salary field) identifies a line with data
-- on it. All other lines in the report are discarded.
--
-- The TRAILING NULLCOLS clause causes SQL*Loader to treat any fields
-- that are missing at the end of a record as null. Because the
-- commission field is not present for every record, this clause says
-- to load a null commission instead of rejecting the record when only
-- seven fields are found instead of the expected eight.
--
-- The  hiredate is filled in using the current system date (SYSDATE).
--
-- The specification for deptno will generate a warning message in
-- the log file because the specified length does not agree with
-- the length determined by the field‘s position. The specified
-- length (3) is used. The length is in bytes with the default
-- byte-length semantics. If character-length semantics were used
-- instead, this length would be in characters.
--
-- The NULLIF clause says that because the report shows only department
-- number, job, and manager when the value changes, these fields may
-- be blank. This control file causes them to be loaded as null, and
-- an insert trigger fills in the last valid value.
--
-- For the job field, the SQL string changes the job name to
-- uppercase letters.
--
-- For the mgr field, it is necessary to specify starting position.
-- If the job field and the manager field were both blank, then the
-- job field‘s TERMINATED BY WHITESPACE clause would cause SQL*Loader
-- to scan forward to the employee name field. Without the POSITION
-- clause, the employee name field would be mistakenly interpreted
-- as the manager field.
--
-- For the sal field, the SQL string translates the field from a
-- formatted character string into a number. The numeric value takes
-- less space and can be printed with a variety of formatting options.
--
-- For the comm field, different initial and trailing delimiters pick the
-- numeric value out of a formatted field. The SQL string then converts
-- the value to its stored form.
--
LOAD DATA
INFILE ‘ulcase7.dat‘
DISCARDFILE ‘ulcase7.dsc‘
APPEND
INTO TABLE emp
  WHEN (57)=‘.‘
  TRAILING NULLCOLS
  (hiredate SYSDATE,
   deptno   POSITION(1:2)  INTEGER EXTERNAL(2)
            NULLIF deptno=BLANKS,
   job      POSITION(7:14)   CHAR  TERMINATED BY WHITESPACE
            NULLIF job=BLANKS  "UPPER(:job)",
   mgr      POSITION(28:31)  INTEGER EXTERNAL TERMINATED BY WHITESPACE
            NULLIF mgr=BLANKS,
   ename    POSITION (34:41) CHAR  TERMINATED BY WHITESPACE
            "UPPER(:ename)",
   empno    INTEGER EXTERNAL  TERMINATED BY WHITESPACE,
   sal      POSITION(51)  CHAR  TERMINATED BY WHITESPACE
            "TO_NUMBER(:sal,‘$99,999.99‘)",
   comm     INTEGER EXTERNAL  ENCLOSED BY ‘(‘ AND ‘%‘
            ":comm * 100"
  )

3. 数据文件

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

               Today‘s Newly Hired Employees

Dept  Job       Manager   MgrNo  Emp Name  EmpNo  Salary/Commission
----  --------  --------  -----  --------  -----  -----------------
20    Salesman  Blake      7698  Shepard    8061  $1,600.00 (3%)
                                 Falstaff   8066  $1,250.00 (5%)
                                 Major      8064  $1,250.00 (14%)

30    Clerk     Scott      7788  Conrad     8062  $1,100.00
                Ford       7369  DeSilva    8063    $800.00
      Manager   King       7839  Provo      8065  $2,975.00

执行后结果:

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

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

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

SQL> select * from emp;

EMPNO ENAME     JOB         MGR HIREDATE      SAL  COMM DEPTNO
----- ---------- --------- ----- --------- ------- ----- ------
 8061 SHEPARD     SALESMAN   7698 19-SEP-14    1600   300     20
 8066 FALSTAFF     SALESMAN   7698 19-SEP-14    1250   500     20
 8064 MAJOR     SALESMAN   7698 19-SEP-14    1250  1400     20
 8062 CONRAD     CLERK        7788 19-SEP-14    1100         30
 8063 DESILVA     CLERK        7369 19-SEP-14     800         30
 8065 PROVO     MANAGER    7839 19-SEP-14    2975         30

6 rows selected.

查看日志文件:

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

SQL*Loader: Release 11.2.0.1.0 - Production on Fri Sep 19 03:20:19 2014

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

Control File:   ulcase7.ctl
Data File:      ulcase7.dat
  Bad File:     ulcase7.bad
  Discard File: ulcase7.dsc
 (Allow all discards)

Number to load: ALL
Number to skip: 0
Errors allowed: 50
Bind array:     64 rows, maximum of 256000 bytes
Continuation:    none specified
Path used:      Conventional

Table EMP, loaded when 57:57 = 0X2e(character ‘.‘)
Insert option in effect for this table: APPEND
TRAILING NULLCOLS option in effect

   Column Name                  Position   Len  Term Encl Datatype
------------------------------ ---------- ----- ---- ---- ---------------------
HIREDATE                                                  SYSDATE
DEPTNO                                1:2     2           CHARACTER
    NULL if DEPTNO = BLANKS
JOB                                  7:14     8  WHT      CHARACTER
    NULL if JOB = BLANKS
    SQL string for column : "UPPER(:job)"
MGR                                 28:31     4  WHT      CHARACTER
    NULL if MGR = BLANKS
ENAME                               34:41     8  WHT      CHARACTER
    SQL string for column : "UPPER(:ename)"
EMPNO                                NEXT     *  WHT      CHARACTER
SAL                                    51     *  WHT      CHARACTER
    SQL string for column : "TO_NUMBER(:sal,‘$99,999.99‘)"
COMM                                 NEXT     *        (  CHARACTER
                                                        %
    SQL string for column : ":comm * 100"

Record 1: Discarded - failed all WHEN clauses.
Record 2: Discarded - failed all WHEN clauses.
Record 3: Discarded - failed all WHEN clauses.
Record 4: Discarded - failed all WHEN clauses.
Record 5: Discarded - failed all WHEN clauses.
Record 6: Discarded - failed all WHEN clauses.
Record 10: Discarded - failed all WHEN clauses.

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

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

Total logical records skipped:          0
Total logical records read:            13
Total logical records rejected:         0
Total logical records discarded:        7

Run began on Fri Sep 19 03:20:19 2014
Run ended on Fri Sep 19 03:20:19 2014

Elapsed time was:     00:00:00.28
CPU time was:         00:00:00.17

查看Discard file:

[[email protected] ulcase]$ cat ulcase7.dsc

               Today‘s Newly Hired Employees

Dept  Job       Manager   MgrNo  Emp Name  EmpNo  Salary/Commission
----  --------  --------  -----  --------  -----  -----------------
时间: 2024-10-29 13:22:27

SQL*Loader之CASE7的相关文章

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: