SQL*Loader之CASE11

本文介绍了一个使用SQL*Loader加载UTF-16编码数据到Oracle数据库的案例,详细展示了SQL脚本、控制文件及数据文件的配置,并解释了关键参数如CHARACTERSET和BYTEORDER的作用。
CASE11

1. SQL脚本

[oracle@node3 ulcase]$ cat ulcase11.sql

set termout off

rem host write sys$output "Building demonstration tables for case study 11.  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),
    projno number,
    loadseq number);

exit

2. 控制文件

[oracle@node3 ulcase]$ cat ulcase11.ctl

-- Copyright (c) 1991, 2004 Oracle.  All rights reserved.
-- NAME
-- ulcase11.ctl - SQL*Loader Case Study 11: Load Data in the Unicode
--                Character Set UTF-16
--
-- DESCRIPTION
-- This case study demonstrates the following:
-- Using SQL*Loader to load data in the Unicode character set, UTF16.
--
-- Using SQL*Loader to load data in a fixed-width, multibyte character set.
--
-- Using character-length semantics.
--
-- Using SQL*Loader to load data in little-endian byte order. SQL*Loader 
-- checks the byte order of the system on which it is running. If necessary, 
-- SQL*Loader swaps the byte order of the data to ensure that any 
-- byte-order-dependent data is correctly loaded.
--
-- TO RUN THIS CASE STUDY:
-- 1. Before executing this control file, log in to SQL*Plus as
--    scott/tiger. Enter @ulcase11 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=ulcase11.ctl LOG=ulcase11.log
-- 
-- NOTES ABOUT THIS CONTROL FILE
--
-- The character set specified with the CHARACTERSET keyword is UTF16. 
-- SQL*Loader will convert the data from the UTF16 character set to 
-- the database character set. Because UTF16 is specified as the 
-- character set, character-length semantics are used for the load.
--
-- BYTEORDER LITTLE tells SQL*Loader that the data in the datafile is 
-- in little-endian byte order. SQL*Loader checks the byte order of the 
-- system on which it is running to determine if any byte-swapping is 
-- necessary. In this example, all the character data in UTF16 is 
-- byte-order dependent.
--
-- The TERMINATED BY and OPTIONALLY ENCLOSED BY clauses both specify 
-- hexadecimal strings. The X'002c' is the encoding for a comma (,) in 
-- UTF-16 big-endian format. The X'0022' is the encoding for a double 
-- quotation mark (") in big-endian format. Because the datafile is in 
-- little-endian format, SQL*Loader swaps the bytes before checking for 
-- a match. If these clauses were specified as character strings instead 
-- of hexadecimal strings, SQL*Loader would convert the strings to the 
-- datafile character set (UTF16) and byte-swap as needed before checking 
-- for a match.
--
-- Because character-length semantics are used, the maximum length for 
-- the empno, hiredate, and deptno fields is interpreted as characters, 
-- not bytes.
--
-- The TERMINATED BY clause for the deptno field is specified using the 
-- character string ":". SQL*Loader converts the string to the datafile 
-- character set (UTF16) and byte-swaps as needed before checking for a match.

LOAD DATA 
CHARACTERSET utf16
BYTEORDER little
INFILE ulcase11.dat
REPLACE

INTO TABLE EMP
FIELDS TERMINATED BY X'002c' OPTIONALLY ENCLOSED BY X'0022'
(empno integer external (5), ename, job, mgr,
 hiredate DATE(20) "DD-Month-YYYY",
 sal, comm,
 deptno   CHAR(5) TERMINATED BY ":",
 projno,
 loadseq  SEQUENCE(MAX,1) )    

3. 数据文件

数据文件因为是UTF16编码,在文本文件中显示为乱码,在这里不贴出。

执行后结果:

[oracle@node3 ulcase]$ sqlplus scott/tiger @ulcase11.sql

[oracle@node3 ulcase]$ sqlldr userid=scott/tiger control=ulcase11.ctl

SQL> select * from emp;

EMPNO ENAME  JOB       MGR HIREDATE       SAL    COMM  DEPTNO PROJNO    LOADSEQ
----- ------ --------- ----- --------- -------   ----- ------ ------ ----------
 7782 Clark  Manager   7839 09-JUN-81      2573          10    101        1

 7839 King   President      17-NOV-81      5500          10    102        2

 7934 Miller Clerk     7782 23-JAN-82       920          10    102        3

 7566 Jones  Manager   7839 02-APR-81      3124          20    101        4 

7499 Allen Salesman 7698 20-FEB-81 1600 300 30 103 5 7654 Martin Salesman 7698 28-SEP-81 1313 1400 30 103 6 7658 Chan Analyst 7566 03-MAY-82 3450 20 101 7 7 rows selected.

查看一下日志文件:

[oracle@node3 ulcase]$ cat ulcase11.log 

SQL*Loader: Release 11.2.0.1.0 - Production on Fri Sep 19 04:13:59 2014

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

Control File:   ulcase11.ctl
Character Set utf16 specified for all input.
Using character length semantics.
Byteorder little endian specified.

Data File:      ulcase11.dat
  Bad File:     ulcase11.bad
  Discard File:  none specified
 
 (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 from every logical record.
Insert option in effect for this table: REPLACE

   Column Name                  Position   Len  Term Encl Datatype
------------------------------ ---------- ----- ---- ---- ---------------------
EMPNO                               FIRST    10   ,  O(") CHARACTER            
ENAME                                NEXT     *   ,  O(") CHARACTER            
JOB                                  NEXT     *   ,  O(") CHARACTER            
MGR                                  NEXT     *   ,  O(") CHARACTER            
HIREDATE                             NEXT    40   ,  O(") DATE DD-Month-YYYY   
SAL                                  NEXT     *   ,  O(") CHARACTER            
COMM                                 NEXT     *   ,  O(") CHARACTER            
DEPTNO                               NEXT    10   :  O(") CHARACTER            
PROJNO                               NEXT     *   ,  O(") CHARACTER            
LOADSEQ                                                   SEQUENCE (MAX, 1)


Table EMP:
  7 Rows successfully loaded.
  0 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:                 104768 bytes(64 rows)
Read   buffer bytes: 1048576

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

Run began on Fri Sep 19 04:13:59 2014
Run ended on Fri Sep 19 04:13:59 2014

Elapsed time was:     00:00:00.41
CPU time was:         00:00:00.24

总结:在本例中

   1> CHARACTERSET utf16指数据文件的字符集是utf16

   2> BYTEORDER little指the data in the datafile is in little-endian byte order。

<think>好的,我现在需要帮助用户找到Oracle SQL*Loader的使用示例。首先,我应该回忆一下SQL*Loader的基本概念和常见用法。SQL*Loader是Oracle用来将外部数据文件导入数据库的工具,通常需要一个控制文件(.ctl)来指定数据格式和加载规则。 用户提到他们想查找使用示例,特别是写法示例。根据提供的引用内容,有几个例子被引用了,比如ldr_case1.ctl和ldr_case5.ctl。我需要检查这些引用中的具体内容,看看是否有可用的示例。 引用[1]展示了如何执行sqlldr命令,使用scott用户的控制文件ldr_case1.ctl。引用[3]则展示了执行后的结果,包括加载的数据到bonus中。这明控制文件中可能定义了如何将数据加载到bonus的结构。 接下来,我需要构建一个典型的控制文件示例。通常,控制文件包括加载数据到哪个,字段的定义,以及数据文件的路径。例如,使用FIELDS TERMINATED BY来指定分隔符,或者使用POSITION来定位字段位置。 考虑到用户可能需要不同的数据格式,比如逗号分隔或固定宽度,我应该提供这两种情况的示例。例如,对于逗号分隔的数据,控制文件可能使用TERMINATED BY ',',而固定宽度则使用POSITION(start:end)。 此外,用户可能关心如何执行命令,所以需要包括sqlldr的命令行调用示例,如sqlldr scott/tiger control=example.ctl。同时,应该提到日志文件和错误处理的部分,因为这些是实际使用中常见的注意事项。 还要注意引用中的例子,比如在引用[3]中,控制文件可能定义了字段的映射,如ENAME、JOB等,对应的数据文件中的位置或分隔方式。需要确保示例中的语法正确,比如正确的关键字和结构。 最后,总结常见的应用场景,比如从CSV导入、处理不同分隔符、跳过标题行等,帮助用户理解SQL*Loader的灵活性。同时,生成相关问题,帮助用户进一步学习优化方法和处理复杂情况。</think>以下是Oracle SQL*Loader的使用示例及关键步骤说明: ### 基础控制文件示例(ldr_case1.ctl) ```sql LOAD DATA INFILE '/path/data.csv' INTO TABLE bonus FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' (ename, job, sal, comm) ``` 执行命令: ```bash sqlldr scott/tiger CONTROL=ldr_case1.ctl ``` 此示例将CSV文件数据按字段名映射到$bonus$中[^3] ### 固定宽度文件示例 ```sql LOAD DATA INFILE 'data.txt' INTO TABLE employees ( empno POSITION(1:4) INTEGER EXTERNAL, ename POSITION(5:20) CHAR, job POSITION(21:30) ) ``` 适用于文本列严格对齐的场景[^2] ### 高级特性示例 ```sql OPTIONS (SKIP=1, ERRORS=50) LOAD DATA CHARACTERSET UTF8 APPEND INTO TABLE sales WHEN product_id != BLANKS FIELDS TERMINATED BY '|' TRAILING NULLCOLS ( sale_date DATE "YYYY-MM-DD", product_id, quantity INTEGER EXTERNAL, comments CHAR(100) ) ``` 包含字符集设置、数据追加模式、条件加载等特性[^1]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值