學習oracle sql loader 的使用

來源:互聯網
上載者:User
oracle
學習oracle sql loader 的使用

一:sql loader 的特點
oracle自己帶了很多的工具可以用來進行資料的遷移、備份和恢複等工作。但是每個工具都有自己的特點。
 比如說exp和imp可以對資料庫中的資料進行匯出和匯出的工作,是一種很好的Database Backup和恢複的工具,因此主要用在資料庫的熱備份和恢複方面。有著速度快,使用簡單,快捷的優點;同時也有一些缺點,比如在不同版本資料庫之間的匯出、匯入的過程之中,總會出現這樣或者那樣的問題,這個也許是oracle公司自己產品的相容性的問題吧。
 sql loader 工具卻沒有這方面的問題,它可以把一些以文字格式設定存放的資料順利的匯入到oracle資料庫中,是一種在不同資料庫之間進行資料移轉的非常方便而且通用的工具。缺點就速度比較慢,另外對blob等類型的資料就有點麻煩了。
 
二:sql loader 的協助

C:\>sqlldr

SQL*Loader: Release 9.2.0.1.0 - Production on 星期六 10月 9 14:48:12 2004

Copyright (c) 1982, 2002, Oracle Corporation.  All rights reserved.


用法: SQLLDR keyword=value [,keyword=value,...]

有效關鍵字:

    userid -- ORACLE username/password
   control -- Control file name
       log -- Log file name
       bad -- Bad file name
      data -- Data file name
   discard -- Discard file name
discardmax -- Number of discards to allow        (全部預設)
      skip -- Number of logical records to skip  (預設0)
      load -- Number of logical records to load  (全部預設)
    errors -- Number of errors to allow          (預設50)
      rows -- Number of rows in conventional path bind array or between direct p
ath data saves
(預設: 常規路徑 64, 所有直接路徑)
  bindsize -- Size of conventional path bind array in bytes(預設256000)
    silent -- Suppress messages during run (header,feedback,errors,discards,part
itions)
    direct -- use direct path                    (預設FALSE)
   parfile -- parameter file: name of file that contains parameter specification
s
  parallel -- do parallel load                   (預設FALSE)
      file -- File to allocate extents from
skip_unusable_indexes -- disallow/allow unusable indexes or index partitions(默
認FALSE)
skip_index_maintenance -- do not maintain indexes, mark affected indexes as unus
able(預設FALSE)
  readsize -- Size of Read buffer                (預設1048576)
external_table -- use external table for load; NOT_USED, GENERATE_ONLY, EXECUTE(
預設NOT_USED)
columnarrayrows -- Number of rows for direct path column array(預設5000)
streamsize -- Size of direct path stream buffer in bytes(預設256000)
multithreading -- use multithreading in direct path
 resumable -- enable or disable resumable for current session(預設FALSE)
resumable_name -- text string to help identify resumable statement
resumable_timeout -- wait time (in seconds) for RESUMABLE(預設7200)
date_cache -- size (in entries) of date conversion cache(預設1000)

PLEASE NOTE: 命令列參數可以由位置或關鍵字指定
。前者的例子是 'sqlload
scott/tiger foo'; 後一種情況的一個樣本是 'sqlldr control=foo
userid=scott/tiger'.位置指定參數的時間必須早於
但不可遲於由關鍵字指定的參數。例如,
允許 'sqlldr scott/tiger control=foo logfile=log', 但是
不允許 'sqlldr scott/tiger control=foo log', 即使
參數 'log' 的位置正確。

C:\>

三:sql loader使用例子
a)SQLLoader將 Excel 資料匯出到 Oracle
1.建立SQL*Loader輸入資料所需要的檔案,均儲存到C:\,用記事本編輯:
控制檔案:input.ctl,內容如下:
 
  load data           --1、控制檔案標識
  infile 'test.txt'       --2、要輸入的資料檔案名為test.txt
  append into table test    --3、向表test中追加記錄
  fields terminated by X'09'  --4、欄位終止於X'09',是一個定位字元(TAB)
  (id,username,password,sj)   -----定義列對應順序
 
a、insert,為預設方式,在資料裝載開始時要求表為空白
b、append,在表中追加新記錄
c、replace,刪除舊記錄,替換成新裝載的記錄
d、truncate,同上
 
在DOS視窗下使用SQL*Loader命令實現資料的輸入
 
C:\>sqlldr userid=system/manager control=input.ctl
  預設記錄檔名為:input.log
預設壞記錄檔案為:input.bad
 
2.還有一種方法
可以把EXCEL檔案另存新檔CSV(逗號分隔)(*.csv),控制檔案就改為用逗號分隔
LOAD DATA
INFILE 'd:\car.csv'
APPEND  INTO TABLE t_car_temp
FIELDS TERMINATED BY ","
(phoneno,vip_car)

b)在控制檔案中直接匯入資料

1、控制檔案test.ctl的內容
-- The format for executing this file with SQL Loader is:
-- SQLLDR control=<filename> Be sure to substitute your
-- version of SQL LOADER and the filename for this file.
LOAD DATA
INFILE *
BADFILE 'C:\Documents and Settings\Jackey\案頭\WMCOUNTRY.BAD'
DISCARDFILE 'C:\Documents and Settings\Jackey\案頭\WMCOUNTRY.DSC'
INSERT INTO TABLE EMCCOUNTRY
Fields terminated by ";" Optionally enclosed by '"'
(
  COUNTRYID NULLIF (COUNTRYID="NULL"),
  COUNTRYCODE,
  COUNTRYNAME,
  CONTINENTID NULLIF (CONTINENTID="NULL"),
  MAPID NULLIF (MAPID="NULL"),
  CREATETIME DATE "MM/DD/YYYY HH24:MI:SS" NULLIF (CREATETIME="NULL"),
  LASTMODIFIEDTIME DATE "MM/DD/YYYY HH24:MI:SS" NULLIF (LASTMODIFIEDTIME="NULL")
)
BEGINDATA
1;"JP";"Japan";1;9;"09/16/2004 16:31:32";NULL
2;"CN";"China";1;10;"09/16/2004 16:31:32";NULL
3;"IN";"India";1;11;"09/16/2004 16:31:32";NULL
4;"AU";"Australia";6;12;"09/16/2004 16:31:32";NULL
5;"CA";"Canada";4;13;"09/16/2004 16:31:32";NULL
6;"US";"United States";4;14;"09/16/2004 16:31:32";NULL
7;"MX";"Mexico";4;15;"09/16/2004 16:31:32";NULL
8;"GB";"United Kingdom";3;16;"09/16/2004 16:31:32";NULL
9;"DE";"Germany";3;17;"09/16/2004 16:31:32";NULL
10;"FR";"France";3;18;"09/16/2004 16:31:32";NULL
11;"IT";"Italy";3;19;"09/16/2004 16:31:32";NULL
12;"ES";"Spain";3;20;"09/16/2004 16:31:32";NULL
13;"FI";"Finland";3;21;"09/16/2004 16:31:32";NULL
14;"SE";"Sweden";3;22;"09/16/2004 16:31:32";NULL
15;"IE";"Ireland";3;23;"09/16/2004 16:31:32";NULL
16;"NL";"Netherlands";3;24;"09/16/2004 16:31:32";NULL
17;"DK";"Denmark";3;25;"09/16/2004 16:31:32";NULL
18;"BR";"Brazil";5;85;"09/30/2004 11:25:43";NULL
19;"KR";"Korea, Republic of";1;88;"09/30/2004 11:25:43";NULL
20;"NZ";"New Zealand";6;89;"09/30/2004 11:25:43";NULL
21;"BE";"Belgium";3;79;"09/30/2004 11:25:43";NULL
22;"AT";"Austria";3;78;"09/30/2004 11:25:43";NULL
23;"NO";"Norway";3;82;"09/30/2004 11:25:43";NULL
24;"LU";"Luxembourg";3;81;"09/30/2004 11:25:43";NULL
25;"PT";"Portugal";3;83;"09/30/2004 11:25:43";NULL
26;"GR";"Greece";3;80;"09/30/2004 11:25:43";NULL
27;"IL";"Israel";1;86;"09/30/2004 11:25:43";NULL
28;"CH";"Switzerland";3;84;"09/30/2004 11:25:43";NULL
29;"A1";"Anonymous Proxy";0;0;"09/30/2004 11:25:43";NULL
30;"A2";"Satellite Provider";0;0;"09/30/2004 11:25:43";NULL
31;"AD";"Andorra";3;0;"09/30/2004 11:25:43";NULL
32;"AE";"United Arab Emirates";1;0;"09/30/2004 11:25:43";NULL
33;"AF";"Afghanistan";1;0;"09/30/2004 11:25:43";NULL
34;"AG";"Antigua and Barbuda";7;0;"09/30/2004 11:25:43";NULL
35;"AI";"Anguilla";7;0;"09/30/2004 11:25:43";NULL
36;"AL";"Albania";3;0;"09/30/2004 11:25:43";NULL
37;"AM";"Armenia";3;0;"09/30/2004 11:25:43";NULL
38;"AN";"Netherlands Antilles";3;0;"09/30/2004 11:25:43";NULL
39;"AO";"Angola";2;0;"09/30/2004 11:25:43";NULL
40;"AP";"Asia/Pacific Region";2;0;"09/30/2004 11:25:43";NULL
41;"AQ";"Antarctica";8;0;"09/30/2004 11:25:43";NULL
42;"AR";"Argentina";5;0;"09/30/2004 11:25:43";NULL
43;"AS";"American Samoa";6;0;"09/30/2004 11:25:43";NULL
44;"AW";"Aruba";5;0;"09/30/2004 11:25:43";NULL
45;"AZ";"Azerbaijan";1;0;"09/30/2004 11:25:43";NULL
46;"BA";"Bosnia and Herzegovina";3;0;"09/30/2004 11:25:43";NULL
47;"BB";"Barbados";5;0;"09/30/2004 11:25:43";NULL
48;"BD";"Bangladesh";1;0;"09/30/2004 11:25:43";NULL
49;"BF";"Burkina Faso";2;0;"09/30/2004 11:25:43";NULL
50;"BG";"Bulgaria";3;0;"09/30/2004 11:25:43";NULL
51;"BH";"Bahrain";1;0;"09/30/2004 11:25:43";NULL
52;"BI";"Burundi";2;0;"09/30/2004 11:25:43";NULL
53;"BJ";"Benin";2;0;"09/30/2004 11:25:43";NULL
54;"BM";"Bermuda";4;0;"09/30/2004 11:25:43";NULL
55;"BN";"Brunei Darussalam";1;0;"09/30/2004 11:25:43";NULL
56;"BO";"Bolivia";5;0;"09/30/2004 11:25:43";NULL
57;"BS";"Bahamas";7;0;"09/30/2004 11:25:43";NULL
58;"BT";"Bhutan";1;0;"09/30/2004 11:25:43";NULL
59;"BV";"Bouvet Island";5;0;"09/30/2004 11:25:43";NULL
60;"BW";"Botswana";2;0;"09/30/2004 11:25:43";NULL
61;"BY";"Belarus";3;0;"09/30/2004 11:25:43";NULL
2、執行匯入命令
C:\>sqlldr userid=system/manager control=test.ctl

c)複雜格式的匯入



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.