`

sql loader

阅读更多
项目上有人要用这个命令,简单看一下


一:sql loader 的特点

oracle自己带了很多的工具可以用来进行数据的迁移、备份和恢复等工作。但是每个工具都有自己的特点。

比如说exp和imp可以对数据库中的数据进行导出和导出的工作,是一种很好的数据库备份和 恢复的工具,因此主要用在数据库的热备份和恢复方面。有着速度快,使用简单,快捷的优点;同时也有一些缺点,比如在不同版本数据库之间的导出、导入的过程 之中,总会出现这样或者那样的问题,这个也许是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)复杂格式的导入

Sqlldr

sql loader可以把一些以文本格式存放的数据顺利的导入到oracle数据库中,

是一种在不同数据库之间进行数据迁移的非常方便而且通用的工具。

缺点就速度比较慢,另外对blob等类型的数据就有点麻烦了。

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


有效的关键字:

    userid -- ORACLE username/password

   control – 控制文件

       log – 记录的日志文件

       bad – 坏数据文件

      data – 数据文件

   discard – 丢弃的数据文件

discardmax – 允许丢弃数据的最大值        (全部默认)

      skip -- Number of logical records to skip (默认0)

      load -- Number of logical records to load (全部默认)

    errors – 允许的错误记录数          (默认50)

      rows -- Number of rows in conventional path bind array or between direct path data saves

                (每次提交的记录数,默认: 常规路径 64, 所有直接路径)

bindsize -- Size of conventional path bind array in bytes(默认256000)

                每次提交记录的缓冲区的大小(字节为单位,默认256000)

    silent --禁止输出信息 (header,feedback,errors,discards,partitions)

    direct – 使用直通路径方式导入                    (默认FALSE)

   parfile -- parameter file: name of file that contains parameter specifications

parallel -- 并行导入                   (默认FALSE)

       file -- File to allocate extents from

与bindsize成对使用,其中较小者会自动调整到较大者

sqlldr先计算单条记录长度,乘以rows,如小于bindsize,不会试图扩张rows以填充bindsize;如超出,则以bindsize为准。


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)


注意:有两种方式可以指定命令行参数:通过位置或者通过关键字。前者的例子:'sqlldr scott/tiger foo';

后者的例子:'sqlldr control=foo userid=scott/tiger';

不能前面使用关键字指定后面通过位置制定的混合方式;

比如:'sqlldr scott/tiger control=foo logfile=log' 是允许的,

但'sqlldr scott/tiger control=foo log'不允许。

为清楚起见最好所有命令行参数都用关键字指定。

控制文件:

一个控制命令的脚本文件,通常以ctl结尾,内容如下:

LOAD DATA

INFILE 't.dat'              要导入的文件

// INFILE 'tt.date' 导入多个文件

// INFILE *               表示要导入的内容就在control文件里 下面的BEGINDATA后面就是导入的内容


INTO TABLE table_name    指定装入的表

BADFILE 'c:\bad.txt'    可选,指定坏文件地址,缺省在当前目录下生成与原文件名一致的.bad文件


************* 以下是4种装入表的方式

APPEND             原先的表有数据 就加在后面

INSERT             装载空表 如果原先的表有数据 sqlloader会停止 默认值

REPLACE           原先的表有数据 原先的数据会全部删除

TRUNCATE        指定的内容和replace的相同 会用truncate语句删除现存数据


************* 指定分隔符

FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'

// TERMINATED BY WRITESPACE 以空白分割


TRAILING NULLCOLS            表的字段没有对应的值时允许为空


************* 下面是表的字段

(

col_1 , col_2 ,col_filler FILLER // FILLER 关键字 此列的数值不会被装载

// 如: lg,lg,not 结果 lg lg

)

如果没声明FIELDS TERMINATED BY ',' 时,可以用下面两种方式实现同样功能:

1.为每一列指定分隔符

(

col_1 [interger external] TERMINATED BY ',' ,

col_2 [date "dd-mon-yyy"] TERMINATED BY ',' ,

col_3 [char] TERMINATED BY ',' OPTIONALLY ENCLOSED BY 'lg'

)

2.用位置告诉字段装载数据

(

col_1 position(1:2),

col_2 position(3:10),

col_3 position(*:16), // 这个字段的开始位置在前一字段的结束位置

col_4 position(1:16),

col_5 position(3:10) char(8) // 指定字段的类型

)


BEGINDATA         对应开始的 INFILE * 要导入的内容就在control文件里

10,Sql,what

20,lg,show

asp.net调用bat和ctl文件实现sql*loader的功能

1 后台调用bat文件

            Dim proc As System.Diagnostics.Process

            proc = System.Diagnostics.Process.Start("cmd.exe", " /c " & "D:\execise\SE004.bat")

            proc.WaitForExit()

或者

        Shell("cmd.exe /c D:\execise\SE004.bat", AppWinStyle.NormalFocus)

或者

             Dim p As New System.Diagnostics.Process

            'p.StartInfo.FileName = "cmd.exe"

            'p.StartInfo.Arguments = " /c " & "D:\execise\SE004.bat"

            'p.StartInfo.UseShellExecute = False

            'p.StartInfo.RedirectStandardInput = True

            'p.StartInfo.RedirectStandardOutput = True

            'p.StartInfo.RedirectStandardError = True

            'p.StartInfo.CreateNoWindow = False

            'p.Start()

            'Dim strValue As String = p.StandardOutput.ReadToEnd()

2 然后调用ctl文件,bat文件的

    SQLLDR USERID=spv3/paper@san CONTROL=d:\execise\SE004.CTL log=D:\execise\LOG\SE004.log

3, ctl文件的内容如下

LOAD DATA

INFILE 'D:\execise\SE004.CSV'

TRUNCATE

INTO TABLE TEMP_tableName

FIELDS TERMINATED BY ","

TRAILING NULLCOLS

(

id

,age

,num

,sum

)

4. 数据文件以 逗号分割
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics