0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SQL*Loaderで固定長データをロードしたい

Posted at

SQL*Loaderで固定長データをロードしたい場合は、POSITIONをうまく活用します。

例. 製品の情報を格納するproductテーブルがあります。

CREATE TABLE product (
    id    CHAR(5)       NOT NULL PRIMARY KEY, 
    name  NVARCHAR2(20) NOT NULL,
    price INT           NOT NULL
);

このテーブルに以下のような固定長データを投入したいとします。

product.dat
00001     Summer Tire         1    
00002     Winter Tire         22   
00003     Wheel               333  
00004     Battery             4444 
00005     Wiper               55555

まず、以下のような制御ファイルを用意します。

OPTIONS ( 
  LOG = 'product.log'
)
LOAD DATA
CHARACTERSET UTF8
INFILE       'product.dat'
BADFILE      'product.bad'
DISCARDFILE  'product.dis'
APPEND INTO TABLE product
(
  id      POSITION(1:5)   CHAR,
  name    POSITION(11:30) CHAR,
  price   POSITION(31:35) INTEGER EXTERNAL
)

あとは、この制御ファイルを使って、SQL*Loaderを実行します。

sqlldr userid="devuser/Passw0rd@oracle-db-host:1521/XEPDB1" control='product.ctl'

実行ログは以下の通り。想定通り動いていそうです。

SQL*Loader: Release 19.0.0.0.0 - Production on Sat Jun 29 15:30:18 2024
Version 19.19.0.0.0

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

Path used:      Conventional
Commit point reached - logical record count 4
Commit point reached - logical record count 5

Table PRODUCT:
  5 Rows successfully loaded.

Check the log file:
  product.log
for more information about the load.

最後にテーブルデータを見てみましょう。想定通りのデータが投入されていることがわかります。

image.png

※制御ファイルにてCHAR型を指定すると、空白が切り捨てられます(右トリム)。空白を切り捨てたくない場合はPRESERVE BLANKSを指定します。

name POSITION(11:30) CHAR PRESERVE BLANKS,

環境情報

  • Oracle Database 21c Express Edition Release 21.0.0.0.0
  • SQL*Loader: Release 19.0.0.0.0
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?