本篇文章主要在介紹 Oracle SQL Loader: SQLLDR 的使用方法,如何將檔案資料快速上載到 Oracle 資料庫的 Table 中。

  • Input data file for SQL*Loader

準備要匯入的資料,以逗點''作為每一欄位的分隔,每一行則為每一筆資料。以下列的來說,則有六筆資料,每一筆資料則有 4 個欄位值。

C:\tmp\oracle>type employee.txt
100,Thomas,Sales,5000
200,Jason,Technology,5500
300,Mayla,Technology,7000
400,Nisha,Marketing,9500
500,Randy,Technology,6000
501,Ritu,Accounting,5400

 

  • SQL*Loader Control File

依匯入資料的格式,作好SQL Loader 控制檔

C:\tmp\oracle>type employee.ctl
load data
 infile 'employee.txt'
 into table employee
 fields terminated by ","
 ( id, name, dept, salary )

參數說明如下:

  • infile – 資料檔放置的路徑及檔名
  • into table – 要載入到那一個 table name
  • fields terminated by – 欄位與欄位之間的分隔符號
  • ( id, name, dept, salary ) – 列出是要將資料放入那幾個欄位

 

  • 在資料庫中建立一個table:employee
SQL> create table employee
(
  id integer,
  name varchar2(10),
  dept varchar2(15),
  salary integer,
  hiredon date
)

 

  •  執行 sqlldr 上載資料
C:\tmp\oracle>sqlldr scott/tiger control=employee.ctl
SQL*Loader: Release 11.2.0.2.0 - Production on 星期四 10月 25 14:16:39 2012
Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.
Commit point reached - logical record count 5
Commit point reached - logical record count 6

 

  • 驗證一下資料是否有上載到資料庫的 table:employee 中

sqlldr-employee.png  

 

  • 若有新的資料,要加入到 table:employee ,則必須在控制檔增加 參數:append
C:\tmp\oracle>type employee-append.ctl
load data
 infile 'employee-append.txt'
 append
 into table employee
 fields terminated by ","
 ( id, name, dept, salary )

 

要增加的資料如下:

C:\tmp\oracle>type employee-append.txt
600,Ritu,Accounting,5400
700,Jessica,Marketing,7800

 

  • 再執行一次 sqlldr
C:\tmp\oracle>sqlldr edi/edi control=employee-append.ctl
SQL*Loader: Release 11.2.0.2.0 - Production on 星期四 10月 25 14:35:30 2012
Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.
Commit point reached - logical record count 1
Commit point reached - logical record count 2

 

  • 再次驗證資料

sqlldr-employee-append.png  

 

  • 若要上載到資料庫的資料格式是固定位置如下:
200JasonTechnology550001-02-2005
300MaylaTechnology700010-08-2000
500RandyTechnology600001-01-2007

 

  • 控制檔可以修改如下:
C:\tmp\oracle>type employee-fixed.ctl
load data
 infile 'employee-fixed.txt'
 into table employee
 fields terminated by ","
 ( id position(1:3), name position(4:8), dept position(9:18), salary position(19:22),hiredon position(23:32) DATE "DD-MM-YYYY")

 

驗證資料:

sqlldr-employee-fixed.png  

arrow
arrow
    文章標籤
    Oracle SQL Loader sqlldr
    全站熱搜

    MIS 發表在 痞客邦 留言(0) 人氣()