sqlload的居多用法

合集下载

SQLload

SQLload

SQLloadsqlload是oracle提供的批量导⼊⽂件数据的⼯具1、在导⼊先,先根据数据建表。

2、执⾏sqlldr命令sqlldr userid=username/password control =*.tl --要在ctl⽂件的⽬录下执⾏3、编写ctl⽂件for example:load datainfile 'F:\STUDY\6.1\CELLPORT_20161124143843_exp_1.csv' --加载的⽂件infile 'F:\STUDY\6.1\CELLPORT_20161124143843_exp_2.csv' --加载的⽂件into table cellport --⽬标表when port_status='空闲' --可以设置条件筛选数据FIELDS TERMINATED BY ',' --设置分隔符,这⾥分隔符⽯,OPTIONALLY ENCLOSED BY '"' --设置结束符,也可以不设置,除⾮数据很⼯整,否则最好设置TRAILING NULLCOLS --设置空数据也,加载。

( --下⾯是表的字段,对每个字段还可以使⽤sql函数,进⾏⼀些计算或者格式转换等等,city,house_name "upper(:house_name)", --使⽤函数houes_type,house_jd,houese_wd,eq_name,eq_range,eq_location,eq_fgb,eq_port,port_status,port_type,OLT,OLT_PON ,NUMBER_209 , --DL_TIMESTAMP date 'yyyy-mm-dd hh24:mi:ss' --时间转换)1.什么是*.csv,如何得到?⾥⾯存放的是数据表.每⾏代表数据库表格的⼀⾏,每⾏中,每两个数据中间由逗号","分割.*.csv可以通过"将excel⽂件另存为*.csv"得到.2.如何将*.csv格式的数据导⼊oracle数据库?举例来说:test.csv⽂件中存放以下数据.a1,b1a2,b2存放路径为:d:\test.csv准备将其存放到oracle数据库中.a.⾸先,则需要新建表(栏位数量和类型要和需要导⼊的⼀致)create table test(A char,B char);b.新建test.ctl⽂件⽤记事本编辑写⼊:load datainfile ‘d:\test.csv’into table “test”fields terminated by ‘,’(A,B)c.打开cmd命令⾏界⾯输⼊:sqlldr control=d:\test.ctl log=d:\test.log回车:为账号和密码以及数据库名称.以上的介绍⽐较简单.⾥⾯的介绍⽐较详细,我转到这⾥:在 Oracle 数据库中,我们通常在不同数据库的表间记录进⾏复制或迁移时会⽤以下⼏种⽅法:1. A 表的记录导出为⼀条条分号隔开的 insert 语句,然后执⾏插⼊到 B 表中2. 建⽴数据库间的 dblink,然后⽤ create table B as select * from where ...,或 insert into B select * from where ...3. exp A 表,再 imp 到 B 表,exp 时可加查询条件4. 程序实现 select from A ..,然后 insert into B ...,也要分批提交5. 再就是本篇要说到的 Sql Loader(sqlldr) 来导⼊数据,效果⽐起逐条 insert 来很明显第 1 种⽅法在记录多时是个噩梦,需三五百条的分批提交,否则客户端会死掉,⽽且导⼊过程很慢。

SQLLOADER原理及用法

SQLLOADER原理及用法

SQLLOADER原理及用法SQL*LoaderFrom Oracle FAQJump to: navigation, searchContents[hide]1 Intro to SQL*Loader2 Conventional loads3 Direct loads4 Command Line parameters5 Control files6 Data files7 Return codes8 Examples8.1 Combined data and control file8.2 Fixed width data, one data file - one table8.3 Fixed width data, one data file - two tables8.4 Selective Load8.5 Use Of Functions8.6 Assigning Constants8.7 Use Of Sequence8.8 Integer Feeds treated as date8.9 Conditional Load[edit]Intro to SQL*LoaderSQL*Loader is a tool used by DBAs and developers to populate Oracle tables with data from flat files. The SQL*Loader gives a lot of flexibility to selectively load certain columns but not others, or to exclude certain records entirely. SQL*Loader has some advantages over programming languages that allowembedded SQL statements, as well. SQL*Loader consist of understanding its elements. The first is the data to be loaded, which is stored in the datafile (this is basically a flat/text file, and should not be confused with Oracle Server datafiles, that make up the database). The second is the control file (this is basically a text file, acting as a directive to the Loader, it should not be confused with Oracle Servers control file, which holds database-related information). SQL*Loader accepts special parameters that can affect how the load occurs, called command-line parameters. These parameters which include the ID (username/password or commonly know as the schema) to use while loading data, the name of the data file, and the name of the control file.SQL*Loader in action consist of several additional items. If, in the course of performing data loads, SQL*Loader encounters records it cannot load, the record is rejected and the SQL*Loader puts it in special file called bad file. Additionally, SQL*Loader gives the user options to reject data based on special criteria. These criteria are defined in the control file as a part of the when clause. Is the SQL*Loader encounters a record that fails a specified when clause, the record is placed in aspecial file called discard file.SQL*Loader operates in two modes:[edit]Conventional loadsIn a conventional load, SQL*Loader reads multiple data records from the input file (flat/text file) into a bind array. When the array fills, SQL*Loader passes the data to the Oracle SQL processing mechanism for insertion into the database tables. Oracle SQL processing mechanism will in-turn generates equivalent INSERT statements. All records have to pass through database buffer cache, and only DBWR writes the records to thephysical datafiles. Since all data passes through SGA, in case of instance failure recovery is possible. The conventional path load is nondisruptive and work on the same principles that a normal database insert (DML) works, only much faster. Some of its characteristics are outlined below:When loading data across network (client/server), it is better to user conventional load.When loading data in clustered tables, only conventional load can be used. Records loaded will update the associated indexes, enforce any database integrity rules defined (Primary Key, Foreign Key, Check constraints), as the records are loaded.[edit]Direct loadsDuring direct loads, SQL*Loader reads records from the datafile, converts those records directly into Oracle data blocks, and writes them directly to the disk. Since direct path loading bypasses SGA (database buffer cache), in case of instance failure recovery is not possible. Direct loads are typically used to load large amount of data in a short time. The direct path load works much faster compared to conventional loads. Some of its characteristics of Direct Load are outlined below:At the beginning of the direct load, SQL*Loader makes a call to Oracle to put a lock on the tables which are being loaded, at the end it makes another call to release the lock. It may call Oracle intermediately to get extent information. So a direct path load makes very few calls to Oracle.Direct path loader checks the integrity constraints only in the end (i.e. after loading entire data).The constraints are disabled by the direct load process, before starting loading. Once done the constraints are enabled.If enabling of constraints fails due to data errors, then thestate of the constraints are left disabled, which is undesirable. So it is always better to check the state of your constraints after direct path loads.Direct load process, will not update indexes as the data is loaded, it does this operation after all the data is loaded. In fact it rebuilds the indexes associated with the table.Due to reasons like: not finding more space to load data (datafile being full), orinstance failure or duplicate values in primary key columns. If the load fails, then the indexes are left in direct Load State and are unusable, a DBA or developers shown note this and remove culprit records and enable his constraints and rebuild his indexes.Insert triggers are disabled at the beginning of direct Load State. For example you have an insert trigger which fires after each row is inserted to update the time and userid field of a table, the insert trigger will not be functional when you are loading the records. You may have to write an update trigger or stored procedures to handle these records after direct loading.Any Referential integrity constraint defined on a table, is not enforced during direct loads.[edit]Command Line parametersThe following parameters are accepted by SQL * Loader on the command line. I.e. at the "$ "or "dos" prompt.USERID - Oracle userid and password i.e. schema CONTROL - Control filename LOG - Log filename BAD - Bad filename DATA - Data filename DISCARD - Discard filename DISCARDS - Number of discards to terminate the load SKIP - Number of logical records to skip (default: 0) LOAD - Number of logical records to load ROWS - Number of rows in the conventional path bind array or between direct path saves (conventional path : 64, direct path :all) BINDSIZE - Size of conventional path bind array in bytes SILENT - Suppress messages between run DIRECT - Use direct path load (default :FALSE) PARFILE - Parameter filename PARALLEL - Perform parallel load (default :FALSE) FILE - Datafile to allocate extents [edit]Control filesThe control file provides information to the SQL*Loader for the purpose to load data in flat files. It provides information regarding : datafile/flat file names and format, character set used, data types of the fields, how each filed is delimited, which tables and columns to load. The following parameters can be included in the control file as directives:-- - Any comments to be placedoption - Command-line parameters to be placed in the control file as options unrecoverable - Specifies whether to create redo log entries for loaded data. Unrecoverable can be used on direct loads onlyrecoverable - Specifies whether to create redo log entries for loaded data.load - Load or continue_load must be specifiedcontinue_load - Load or continue_load must be specifieddata - Provided for readabilitycharacterset - Specifies the character set of the data filepreserve blanks - Retains leading white spaces from the datafile in cases where enclosure delimiter are not present begin data - Keyword denoting the beginning of data records to be loadedinfile [name] - Keyword to specify name(s) of input files. An asterisk (*) following this keyword indicates data records are in the control file.badfile [name] - Keyword to specify the name of bad file.They are interchangable discardfile [name] - Keyword to specify name of discard file. They are interchangable discards [x] - Allows X discards before opening the next datafile.discardmax [x] - Allows X discards before terminating the loadinsert - Puts rows into an empty tableappend - Appends to an existing tabletruncate - Deletes current rows in the table, and loads new datareplace - Deletes current rows in the table, and loads new datasorted indexes - For direct path loads, indicates that data has already been sorted in specified indexessinglerow - For use when appending rows or loading a small amount of data into a large table (rowcount ration of 1:20 or less) [edit]Data filesDatafiles, or text file, which is used to load data intoDatabase can be in two formats:fixed-length fields -Field 1 : Column 1-7 Field 2 : Column 8-15variable-length fields - variable-length records are terminated by special character or enclosed by special characters eg. |Deepak|Chebbi|[edit]Return codesWhen executing sqlldr within UNIX, the return code values have changed. In 7.x the sqlldr utility returned a 0 if successful and a 1 if not successful. If there were records that were rejected, then the sqlldr utility would return a successful return code of 0.In Oracle 8 and above, it has four return code values. They are:0 - successful 1 - failed 2 - warm 3 - fatal Here are the conditions and how the return code work:All rows loader successfully - 0 All/some rows discarded - 2 All/some row rejected - 2 Discontinued load - 2 Command line/syntax errors - 1 Errors Fatal to Sql*Loader - 1 OS related errors - 3[edit]Examples[edit]Combined data and control fileThe example below is a combined data and control file (i.e. data part of the file is present in the control file itself). The control file holds key words and data as a directive to SQL * Loader. The table under consideration is "empmast" and has fields "emp_no number(6), emp_lname varchar2(24)". The sequence of steps are listed below:Create a control file, to hold the directives. You can use your favorite editor to create the file. I advice you to follow a naming standard to identify the file. In this example I have named the file "case1.ctl". The contents of the control file are:--This control file hold data to be loaded into empmast table -- * is use only is the data is contained in the control file LOAD DATA INFILE * APPEND INTO TABLE empmast FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY ' " ' (emp_no, emp_lname) BEGIN_DATA 100, "Chebbi" 200, Grant Invoke SQl * Loadersqlload / control=case1.ctl [edit]Fixed width data, one data file - one table The example below is a control file for fixed width data, the datafile and control fiel are separate (i.e. data part of the file is not present in the control file). The table under consideration is "empmast" and has fields "emp_no number(6), emp_lname varchar2(24)". The data file is named "xyz.dat" and the control file "case2.ctl". The sequence of steps are listed below:The data file contents is as shown100000Chebbi 200000Grant 300000Zinky Create a control file, to hold the directives. You can use your favorite editor to create the file.--case2.ctl LOAD DATA INFILE 'xyz.dat' BADFILE 'xyz.bad' LOG xyz.log INSERT INTO TABLE empmast (emp_no POSITION(1:6) INTEGER, emp_name POSITION(7:31) CHAR) Invoke SQl * Loader sqlload / control=case2.ctl [edit]Fixed width data, one data file - two tables The example below is a control file for fixed width data, the datafile and control file are separate (i.e. data part of the file is not present in the control file). The tables under consideration is "empmast" having fields "emp_no number(6), emp_lname varchar2(24)" and empsal having fields "emp_no number(6), salary number(6)". A single datafile is being loaded into two tables. The sequence of steps are listed below: The data file is named "xyz.dat" and the control file "case3.ctl"The data file contents is listed below100000Chebbi 200000GrantBBBBBBBBBBBBBBBBBBB3000 300000Zinky 4000 Create a control file, to hold the directives. You can use your favorite editor to create the file. --case3.ctl LOAD DATA INFILE 'xyz.dat' BADFILE 'xyz.bad' LOG xyz.log INSERT INTO TABLE empmast (emp_no POSITION(1:6) INTEGER, emp_name POSITION(7:31) CHAR) INTO TABLE emp_sal (emp_no POSITION(1:6) INTEGER, empsal POSITION(32:37) INTEGER) Invoke SQl * Loadersqlload / control=case3.ctl [edit]Selective LoadThe example illustrates the use of when clause, in this case if the first field is blank the record is not loaded. The tables under consideration is "empmast" having fields "emp_no number(6),emp_lname varchar2(24)". The data file is named "xyz.dat" and the control file "case4.ctl". The sequence of steps are listed below: The data file contents is listed belowChebbi 2000 Grant 3000 CarpinoBBBBBBBBBBBBBBBBB Create a control file, to hold the directives. You can use your favorite editor to create the file.--case4.ctl LOAD DATA INFILE 'xyz.dat' BADFILE 'xyz.bad' LOG xyz.log DISCARDFILE'xyz.dsc' INSERT INTO TABLE empmast WHEN emp_no != ' ' (emp_no POSITION(1:6) INTEGER, emp_name POSITION(7:31) CHAR) Invoke SQl * Loadersqlload / control=case4.ctl [edit]Use Of FunctionsThe example illustrates the use of function initcap, in this case no matter the case of the second filed it is converted as upper case and stored in the database. The table under consideration is "empmast" having fields "emp_no number(6), emp_lname varchar2(24)". The data file is named "xyz.dat" and the control file "case5.ctl". The sequence of steps are listed below: The data file contents is listed below100000chebbi 200000grantBBBBBBBBBBBBBBBBBBB 300000zinky Create a control file, to hold the directives. You can use your favorite editor to create the file.--case5.ctl LOAD DATA INFILE 'xyz.dat' BADFILE 'xyz.bad' LOG xyz.log INSERT INTO TABLE empmast (emp_no POSITION(1:6) INTEGER, emp_name POSITION(7:31) CHAR "initcap(:emp_name)") Invoke SQl * Loadersqlload / control=case5.ctl [edit]Assigning ConstantsThe example illustrates the use of plugging in a constant value to a field. The table under consideration is "empmast" having fields "emp_no number(6), emp_lname varchar2(24), alivechar(1)". The data file is named "xyz.dat" and the control file "case6.ctl". The sequence of steps are listed below:The data file contents is listed below100000chebbi 200000grantBBBBBBBBBBBBBBBBBBB 300000zinky Create a control file, to hold the directives. You can use your favorite editor to create the file.--case6.ctl LOAD DATA INFILE 'xyz.dat' BADFILE 'xyz.bad' LOG xyz.log INSERT INTO TABLE empmast (emp_no POSITION(1:6) INTEGER, emp_name POSITION(7:31) CHAR, alive CONSTANT "Y") Invoke SQl * Loadersqlload / control=case6.ctl [edit]Use Of SequenceThe example illustrates the use of plugging in a sequence number to a field. The table under consideration is "empmast" having fields "emp_no number(6), emp_lname varchar2(24), seq_no NUMBER". The data file is named "xyz.dat" and the control file "case7.ctl". The sequence of steps are listed below: The data file contents is listed below100000chebbi 200000grantBBBBBBBBBBBBBBBBBBB 300000zinky Create a control file, to hold the directives. You can use your favorite editor to create the file.--case7.ctl LOAD DATA INFILE 'xyz.dat' BADFILE 'xyz.bad' LOG xyz.log INSERT INTO TABLE empmast (emp_no POSITION(1:6) INTEGER, emp_name POSITION(7:31) CHAR, seq_no SEQUENCE(MAX,1)) Invoke SQl * Loadersqlload / control=case7.ctl [edit]Integer Feeds treated as dateThe example illustrates the use of date .The table under consideration is "empmast" having fields "emp_no number(6), emp_lname varchar2(24), hire_date DATE". The data file is named "xyz.dat" and the control file "case8.ctl". I have often found insituations where in when a flat file comes from mainframes as feed to be loaded intoOracle database, the date format is in chracter or integer format (010100) . To loads this data here are the steps: The data file contents is listed below100000chebbiAAAAAAAAAAAAAAAAAA040699200000grantBBBBBBBBBBBBBBBBBBB040599 Create a control file, to hold the directives. You can use your favorite editor to create the file.--case8.ctl LOAD DATA INFILE 'xyz.dat' BADFILE 'xyz.bad' LOG xyz.log INSERT INTO TABLE empmast (emp_no POSITION(1:6) INTEGER, emp_name POSITION(7:31) CHAR, hire_date POSITION(32:37) DATE 'RRMMDD' nullif (hire_date = '000000') Invoke SQl * Loader sqlload / control=case8.ctl [edit]Conditional LoadIn some situation you may want to load the records depending on the value of the first field, for example say you want to load only records having first field value as "100".The table under consideration is "invoice_detail" having fields "inv_no number(6), inv_quantity number(6), line_no number(6)". The data file is named "xyz.dat" and the control file "case9.ctl".The data file contents is listed below1000002001 2000003001 Create a control file, to hold the directives. You can use your favorite editor to create the file. In the following control file records with only invoice number "1000000" are loaded.--case9.ctl LOAD DATA INFILE 'xyz.dat' BADFILE 'xyz.bad' LOG xyz.log DISCARDFILE 'xyz.dsc' INSERT INTO TABLE invoice_detail WHEN inv_no = 100000 (inv_no POSITION(1:6) INTEGER, inv_quantity POSITION(7:12) INTEGER, line_no POSITION(13:18)INTEGER) Invoke SQl * Loader sqlload / control=case9.ctl。

Oracle数据库SqlLoad常用技巧总结

Oracle数据库SqlLoad常用技巧总结

Oracle数据库SqlLoad常用技巧总结Oracle数据库SqlLoad常用技巧的相关知识是本文我们主要要介绍的内容,本文我们总结了14种SqlLoad的使用技巧,并给出了测试用的文件源码,接下来我们就开始一一介绍这部分内容,希望能够对您有所帮助。

1、控制文件中注释用“--”。

2、为防止导入出现中文乱码,在控制文件中加入字符集控制1.LOAD DATA2.CHARACTERSET ZHS16GBK3、让某一列成为行号,用RECNUM关键字1.load data2.infile *3.into table t4.replace5.( seqno RECNUM //载入每行的行号6.text Position(1:1024))7.BEGINDATA8.fsdfasj4、过滤某一列,用FILLER关键字1.LOAD DATA2.TRUNCATE INTO TABLE T13.FIELDS TERMINATED BY ','4.( field1,5.field2 FILLER,6.field37.)5、过滤行在INTO TABLE table_name后加WHEN过滤条件,但功能有限,如果以竖线分隔符的文件,不能实现字段级的过滤,定长的还好。

1.LOAD DATA2.INFILE 'mydata.dat'3.BADFILE 'mydata.bad'4.DISCARDFILE 'mydata.dis'5.APPEND6.INTO TABLE my_selective_table7.WHEN (01) <> 'H' and (01) <> 'T' and (30:37) = '200312 17'8.(9.region CONSTANT '31',10.service_key POSITION(01:11) INTEGER EXTE RNAL,11.call_b_no POSITION(12:29) CHAR12.)6、过滤首行,用OPTIONS (SKIP 1)选项,也可以写在命令行中,如:sqlldr sms/admin control=test.ctl skip=17、TRAILING NULLCOLS的使用,作用是表的字段没有对应的值时允许为空如:1.LOAD DATA2.INFILE *3.INTO TABLE DEPT4.REPLACE5.FIELDS TERMINATED BY ','6.TRAILING NULLCOLS // 其实下面的ENTIRE_LINE在BEGINDATA后面的数据中是没有直接对应的列的值的如果第一行改为 10,Sales,Virginia,1/5/2000,, 就不用TRAILING NULLCOLS了7.(DEPTNO,8.DNAME "upper(:dname)", // 使用函数9.LOC "upper(:loc)",ST_UPDATED date 'dd/mm/yyyy', // 日期的一种表达方式还有'dd-mon-yyyy' 等11.ENTIRE_LINE ":deptno||:dname||:loc||:last_updated "12.)13.BEGINDATA14.10,Sales,Virginia,1/5/200015.20,Accounting,Virginia,21/6/199916.30,Consulting,Virginia,5/1/200017.40,Finance,Virginia,15/3/20018、添加、修改数据(1)、1.LOAD DATA2.INFILE *3.INTO TABLE tmp_test4.( rec_no "my_db_sequence.nextval",5.region CONSTANT '31',6.time_loaded "to_char(SYSDATE, 'HH24:MI')",7.data1 POSITION(1:5) ":data1/100",8.data2 POSITION(6:15) "upper(:data2)",9.data3 POSITION(16:22)"to_date(:data3, 'YYMMDD')"10.)11.BEGINDATA12.11111AAAAAAAAAA99120113.22222BBBBBBBBBB990112(2)、1.LOAD DATA2.INFILE 'mail_orders.txt'3.BADFILE 'bad_orders.txt'4.APPEND5.INTO TABLE mailing_list6.FIELDS TERMINATED BY ","7.( addr,8.city,9.state,10.zipcode,11.mailing_addr "decode(:mailing_addr, null, :addr, : mailing_addr)",12.mailing_city "decode(:mailing_city, null, :city, :mai ling_city)",13.mailing_state14.)9、合并多行记录为一行记录通过关键字concatenate 把几行的记录看成一行记录:1.LOAD DATA2.INFILE *3.concatenate 3 // 通过关键字concatenate 把几行的记录看成一行记录4.INTO TABLE DEPT5.replace6.FIELDS TERMINATED BY ','7.(DEPTNO,8.DNAME "upper(:dname)",9.LOC "upper(:loc)",ST_UPDATED date 'dd/mm/yyyy'11.)12.BEGINDATA13.10,Sales, // 其实这3行看成一行 10,Sales,Virginia,1/5/200014.Virginia,15.1/5/200010、用”|+|”分隔符,避免数据混淆:fields terminated by "|+|"11、如果数据文件包含在控制文件中,用INFILE *如下:1.LOAD DATA2.INFILE *3.append4.INTO TABLE tmp_test5.FIELDS TERMINATED BY ","6.OPTIONALLY ENCLOSED BY '"'7.TRAILING NULLCOLS8.( data1,9.data210.)11.BEGINDATA12.11111,AAAAAAAAAA13.22222,"A,B,C,D,"12、一次导入多个文件到同一个表1.LOAD DATA2.INFILE file1.dat3.INFILE file2.dat4.INFILE file3.dat5.APPEND6.INTO TABLE emp7.( empno POSITION(1:4) INTEGER EXTERNAL,8.ename POSITION(6:15) CHAR,9.deptno POSITION(17:18) CHAR,10.mgr POSITION(20:23) INTEGER EXTERNAL11.)13、将一个文件导入到不同的表(1)、1.LOAD DATA2.INFILE *3.INTO TABLE tab1 WHEN tab = 'tab1'4.( tab FILLER CHAR(4),5.col1 INTEGER6.)7.INTO TABLE tab2 WHEN tab = 'tab2'8.( tab FILLER POSITION(1:4),9.col1 INTEGER10.)11.BEGINDATA12.tab1|113.tab1|214.tab2|215.tab3|316.==============(2)、1.LOAD DATA2.INFILE 'mydata.dat'3.REPLACE4.INTO TABLE emp5.WHEN empno != ' '6.( empno POSITION(1:4) INTEGER EXTERNAL,7.ename POSITION(6:15) CHAR,8.deptno POSITION(17:18) CHAR,9.mgr POSITION(20:23) INTEGER EXTERNAL10.)11.INTO TABLE proj12.WHEN projno != ' '13.( projno POSITION(25:27) INTEGER EXTERNAL,14.empno POSITION(1:4) INTEGER EXTERNAL15.)14、过滤掉的数据文件路径指定1./opt/app/oracle/product/10.2.0/bin/sqlldr APS/APS cont rol=/home/oracle/APS_LOAD/ctl/AP_CONTRACT.CTL LOG=/ho me/oracle/APS_LOAD/log/$yesterday/AP_CONTRACT_$yesterda y.log bad=/home/oracle/APS_LOAD/bad/DUE_BILL_$yesterday.b ad rows=10000readsize=20000000bindsize=20000000DISCA RD=/home/oracle/APS_LOAD/bad/discard_ts.dis15、附:测试用控制文件1.LOAD DATA2.INFILE '/home/oracle/APS_LOAD/dat/APS_AP_CONTRAC T.dat'3.TRUNCATE4.INTO TABLE AP_CONTRACT5.WHEN (01)<>'1'6.FIELDS TERMINATED BY "|"7.TRAILING NULLCOLS8.(9.AGMT_NO "(TRIM(:AGMT_NO ))",10.CONTRACT_NO FILLER, -- "(TRIM(:CONTRACT_NO ))",11.LOAN_AMT "(TRIM(:LOAN_AMT ))",12.AGMT_HOLDER "(TRIM(:AGMT_HOLDER ))",13.LOAN_TYPE_CD "(TRIM(:LOAN_TYPE_CD ))",14.CURR_CD "(TRIM(:CURR_CD ))" ,15.BALANCE "(TRIM(:BALANCE ))" ,16.LOAN_DIRC_CD "(TRIM(:LOAN_DIRC_CD ))",17.AGMT_START_DATE "(TRIM(:AGMT_START_D ATE ))",18.AGMT_END_DATE "(TRIM(:AGMT_END_DATE ))",19.AGMT_BELONG_ORG_NO "(TRIM(:AGMT_BEL ONG_ORG_NO ))",20.MANAGER_NO "(TRIM(:MANAGER_NO ))",21.PROCESS_RATE "(TRIM(:PROCESS_RATE ))",22.INSURE_METH_TYPE_CD "(TRIM(:INSURE_MET H_TYPE_CD ))",23.AGMT_SIGN_DATE "(TRIM(:AGMT_SIGN_DA TE ))",24.LOAN_PROP_CD "(TRIM(:LOAN_PROP_CD ))",25.LOAN_USE_TYPE "(TRIM(:LOAN_USE_TYPE ))",26.ENTRUST_LOAN_FLAG "(TRIM(:ENTRUST_LOA N_FLAG ))",27.ENTRUST_NAME "(TRIM(:ENTRUST_NAME ))",28.FARM_LOAN_FLAG "(TRIM(:FARM_LOAN_FL AG ))",29.FARM_LOAN_TYPE_CD "(TRIM(:FARM_LOAN_ TYPE_CD ))",30.LOAN_BIZ_TYPE_CD "(TRIM(:LOAN_BIZ_TYPE _CD ))",31.ID_TEST RECNUM ,32.CHAR_TEST CONSTANT '31',33.SQ "sqlldr.nextval",34.TEST_4 "TO_CHAR(SYSDATE,'YYYYMM DD HH24:MI:SS')",35.TEST_5 "(TRIM(:LOAN_BIZ_TYPE_CD)||' ---'||TRIM(:AGMT_NO))"36.)关于Oracle数据库SqlLoad常用技巧的相关知识就介绍到这里了,希望本次的介绍能够对您有所收获!。

oracle sqlload用法

oracle sqlload用法

oracle sqlload用法SQL*Loader 是 Oracle 数据库中一个用于高效地将大量数据加载到表中的工具。

它可以从文本文件、CSV 文件或其他数据库中加载数据,并将其插入到指定的表中。

以下是SQL*Loader 的用法:1. 创建控制文件:SQL*Loader 需要一个控制文件来指定数据的格式和加载方式。

控制文件可以手动创建,也可以使用Oracle Enterprise Manager 或 SQL Developer 等工具生成。

控制文件包含以下内容:- LOAD DATA:指定数据加载的语句。

- INFILE:指定要加载的数据文件的路径和名称。

- INTO TABLE:指定要加载数据的目标表的名称。

- FIELDS TERMINATED BY:指定字段之间的分隔符。

- TRAILING NULLCOLS:指定允许空列。

- 载入的列名和列数据类型。

2. 准备数据文件:需要准备一个包含要加载的数据的文本文件。

数据文件中的每一行对应表中一条记录,字段之间使用控制文件中指定的分隔符进行分隔。

3. 运行 SQL*Loader:打开命令行终端或 SQL*Plus 窗口,使用以下命令来运行 SQL*Loader:```sqlldr username/password@database control=controlfile.ctllog=logfilename.log```- `username/password`:Oracle 数据库的用户名和密码。

- `database`:要连接的数据库实例的名称。

- `control=controlfile.ctl`:指定控制文件的路径和名称。

- `log=logfilename.log`:指定日志文件的路径和名称。

4. 检查日志文件:SQL*Loader 在加载数据时会生成一个日志文件,用于记录加载过程中的错误和警告信息。

通过查看日志文件,可以了解加载过程中是否发生了错误,并可以根据其提供的信息进行调试和修复。

ORACLE中如何使用SQLLOAD批量添加数据

ORACLE中如何使用SQLLOAD批量添加数据

ORACLE中如何使用SQLLOAD批量添加数据在Oracle中,可以使用SQLLDR(SQL*Loader)工具来批量添加数据。

SQL*Loader是一个客户端工具,用于从文本文件中加载数据到Oracle表中。

以下是使用SQL*Loader批量添加数据的步骤:1. 创建一个控制文件(Control File):控制文件是一个文本文件,用于指导SQL*Loader加载数据。

它包含了数据文件的格式和数据应该如何被加载到Oracle表中的规则。

控制文件描述了目标表的结构、数据文件的格式和字段之间的映射关系。

一个控制文件可以对应多个数据文件。

2. 创建一个数据文件(Data File):数据文件是包含待加载数据的文本文件。

它可以是一个纯文本文件,也可以是包含字段分隔符的CSV或其他格式的文件。

数据文件的格式应该与控制文件中描述的格式匹配。

3.编写控制文件:控制文件是一个使用特定语法规则编写的文本文件。

它包含了以下几个部分:- LOAD DATA:指示SQL*Loader加载数据的开始。

-INFILE:指定数据文件的路径和名称。

-INTOTABLE:指定目标表的名称。

-FIELDSTERMINATEDBY:指定字段之间的分隔符。

-(列名1,列名2,...):指定数据文件中各个字段的映射关系。

字段的顺序和个数应该与目标表的列顺序和个数一致。

4. 运行SQL*Loader:打开终端(命令提示符或终端窗口),使用以下命令运行SQL*Loader:``````其中,`username`是Oracle用户名,`password`是密码,`database`是数据库名称,`controlfile.ctl`是控制文件的路径和名称。

5. 检查加载结果:运行完成后,SQL*Loader将输出加载的数据行数和错误行数。

如果有错误行,可以查看生成的日志文件以获得更多详细信息。

注意事项:- 在使用SQL*Loader加载数据之前,应该先创建目标表,确保表的结构与控制文件中描述的一致。

sqlloader的用法

sqlloader的用法

sqlloader的用法sqlldr userid=system/password control=h:control.txtuserid是用户名和密码,control是控制文件位置。

下面是控制文件中的内容:load datainfile 'h:bbb.txt'--文本文件的位置append into table table_name ---#1FIELDS TERMINA TED BY ',' ----字段和字段之间的分隔符,如果使用X'09'代表是tab,是否是tab没有测试。

TRAILING NULLCOLS ------表的字段没有对应的值时允许为空(field1,field2,field3)---具体入库的表字段#1。

这里可以使用append、insert、replace和truncate,每个参数具体意义如下:APPEND 原先的表有数据就加在后面INSERT 装载空表如果原先的表有数据sqlloader会停止默认值REPLACE 原先的表有数据原先的数据会全部删除TRUNCA TE 指定的内容和replace的相同会用truncate语句删除现存数据用sqlloader也可以一次导入多个表中只要多加几个into就可以了,具体用法和insert的插入多个表用法类似。

入库的时候格式化也是可以的,可以参照外部表那篇文章。

用法二:数据文件:test1.txtSYSTEM , 40960SYSTEM , 40960SYSTEM , 323649536SYSTEM , 81920SYSTEM , 74350592SYSTEM , 65536SYSTEM , 81920SYSTEM , 40960SYSTEM , 40960SYSTEM , 19963904SYSTEM , 90112SYSTEM , 229376ZFAD , 11526144控制文件:input.ctlload datainfile 'test1.txt'append into table sum_dba_free_spacefields terminated by ','(tablespace_name,bytes)db建表:create table sum_dba_free_space(tablespace_name varchar2(30),bytes number) c:\>sqlldr scott/tiger control=input.ctl加载失败的log(同时会产生test.bad文件):c:\input.logSQL*Loader: Release 10.2.0.4.0 - Production on 星期五10月10 01:53:43 2008 Copyright (c) 1982, 2007, Oracle. All rights reserved.控制文件: input.ctl数据文件: test1.txt错误文件: test1.bad废弃文件: 未作指定(可废弃所有记录)要加载的数: ALL要跳过的数: 0允许的错误: 50绑定数组: 64 行, 最大256000 字节继续: 未作指定所用路径: 常规表SUM_DBA_FREE_SPACE,已加载从每个逻辑记录插入选项对此表APPEND 生效列名位置长度中止包装数据类型------------------------------ ---------- ----- ---- ---- ---------------------TABLESPACE_NAME FIRST * , CHARACTER BYTES NEXT * , CHARACTER表SUM_DBA_FREE_SPACE:6984 行加载成功。

SQLLoader的使用

SQLLoader的使用

1.概述Oracle 的SQL*LOADER可以将外部数据加载到数据库表中。

1.1.基本特点下面是SQL*LOADER的基本特点:1)能装入不同数据类型文件及多个数据文件的数据2)可装入固定格式,自由定界以及可度长格式的数据3)可以装入二进制,压缩十进制数据4)一次可对多个表装入数据5)连接多个物理记录装到一个记录中6)对一单记录分解再装入到表中7)可以用数对制定列生成唯一的KEY8)可对磁盘或磁带数据文件装入制表中9)提供装入错误报告10)可以将文件中的整型字符串,自动转成压缩十进制并装入列表中。

1.2.控制文件控制文件是用一种语言写的文本文件,这个文本文件能被SQL*LOADER识别。

SQL*LOADER根据控制文件可以找到需要加载的数据。

并且分析和解释这些数据。

控制文件由三个部分组成:全局选件,行,跳过的记录数等;INFILE子句指定的输入数据;数据特性说明。

1.3.输入文件对于SQL*Loader, 除控制文件外就是输入数据。

SQL*Loader可从一个或多个指定的文件中读出数据。

如果数据是在控制文件中指定,就要在控制文件中写成INFILE * 格式。

当数据固定的格式(长度一样)时且是在文件中得到时,要用INFILE "fix n"load datainfile 'example.dat' "fix 11"into table examplefields terminated by ',' optionally enclosed by '"'(col1 char(5),col2 char(7))example.dat:001, cd, 0002,fghi,00003,lmn,1, "pqrs",0005,uvwx,当数据是可变格式(长度不一样)时且是在文件中得到时,要用INFILE "var n"。

SqlLoad的简单使用

SqlLoad的简单使用

SqlLoad的简单使⽤
sqlload的简单使⽤:
能实现: 快速导⼊⼤量数据
1.先安装oracle 客户端机器.有点⼤,600M+,
2.安装时选择管理员安装(1.1g)
3.第三步的时候我的出错了.说是环境变量校验不通过,检查环境变量的长度以及是否拥有管理员权限之后都没解决.考虑到这⾥只是⽤来导⼊数据,我选择了全部错误忽略, 最后它还是⾃动把环境变量加上了...具体原因不详
4.确保sqlldr在cmd中能使⽤了就好.
5.开始准备导⼊数据:
1.新建txt⽂件,然后把txt改为.ctl
2.打开ctl⽂件,复制以下代码:我的ctl⽂件名为:sqlload.ctl
LOAD DATA
infile 'e:\aa.csv' ## 源⽂件路径,路径不要包括中⽂
into table xx_temp ## 要导⼊的表
(
id terminated by whitespace ## id 为列名,whitespace 表⽰列之间使⽤空格来区分,如果是其他的 ‘|’ ⽅式则使⽤ terminated by '|'逗号则⽤逗号.以此类推
)
3.保存好之后,打开控制台.在控制台输⼊:
sqlldr username/password@ip或域名:端⼝/sid control=sqlload.ctl log=sqlload.out
然后回车就可以了...⽇志⽂件就在 log=sqlload.out 这⾥,,如果不指定,就默认和ctl同⼀个⽬录...⽇志主要⽤来看异常信息和导⼊的信息.
原创⽂章,转载注明出处.。

数据库常用技术

数据库常用技术

数据泵
• 基本语法 expdp KEYWORD=value [KEYWORD=(value1,value2,...,valueN)] 常用选项 directory --转储目录,用于存放dmp数据文件 dumpfile --导出的dmp数据文件 tables --导出部分表时的表名,可选多个用‘,’分 割 更多选项可以使用 expdp –help进行查看
数据库常用技术
sqlloader
1. SQL*Loader是Oracle数据库导入外部数据的一个工具,它支持变化的加 载模式,可选的加载方式及多表加载等。
2.SQL*Loader的基本语法 SQLLDR keyword=value [,keyword=value,...] 常用选项: userid -- ORACLE 用户名/口令 control -- 控制文件名 log -- 日志文件名 bad -- 错误文件名 data -- 数据文件名 discard -- 废弃文件名 discardmax -- 允许废弃的文件的数目 (全部默认) skip -- 要跳过的逻辑记录的数目 (默认 0) load -- 要加载的逻辑记录的数目 (全部默认) errors -- 允许的错误的数目 (默认 50) rows -- 常规路径绑定数组中或直接路径保存数据间的行数
数据泵
• Impdp使用示例 1.导入到指定用户下 impdp user/user directory=data_dump dumpfile=user20130822.dmp; 2.改变表的所属者owner impdp user/user directory=data_dump dumpfile=user20130822.dmp remap_schema=user:system; 3.导入表 将表dept和emp从scott方案导入到system方案中,对于方案的转移,必须 使用remap_shcema参数 impdp system/manage directory=dump_scott dumpfile=tab.dmp tables=scott.dept,scott.emp remap_schema=scott:system 4.导入数据库 impdp user/user directory=data_dump dumpfile=user20130822.dmp full=y;

sqlload基本使用方法

sqlload基本使用方法

朋友打电话问我如何利用sql讲mysl导出的文本数据加载到oracle数据库,电话说了他也没听清楚,我做了如下实验,给他做例子:一、加载txt文本1.在D盘建立sqlldr的控制文件,111.ctl 内容如下LOAD DATAINFILE 'd:\111.txt'INTO TABLE SALGRADE_TESTTRUNCATEFIELDS TERMINATED BY ','(GRADE,LOSAL,HISAL)2.在D盘建立sqlldr的数据文件111.txt,内容如下格式1,700,12002,1201,14003,1401,20004,2001,30005,3001,99993.在CMD的命令行模式执行加载操作sqlldr scott/tiger control=d:\111.ctl log=d:\111.log bad=d:\111.baSQL*Loader: Release 11.2.0.1.0 - Production on 星期三 9月 14 10:58:35 2011 Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved达到提交点 - 逻辑记录计数 4达到提交点 - 逻辑记录计数 54.登录数据库验证加载结果D:\window\server\mysql-5.1.51\bin>sqlplus scott/tiger连接到:Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - ProductionWith the Partitioning, OLAP, Data Mining and Real Application Testing options SQL> select * from SALGRADE_TEST;GRADE LOSAL HISAL---------- ---------- ----------1 700 12002 1201 14003 1401 20004 2001 30005 3001 99995.导入成功之后,查看111.log6.注释LOAD DATA 控制文件标识INFILE 'd:\111.txt'为数据文件地址和名称,此处如果是INFILE *表示要加载的数据就在这个控制文件里,也就是BEGINDATA后面跟的内容。

达梦数据库load语法-概述说明以及解释

达梦数据库load语法-概述说明以及解释

达梦数据库load语法-概述说明以及解释1.引言1.1 概述概述部分主要介绍了达梦数据库中的Load语法。

Load语法是达梦数据库中用于将数据从外部文件加载到数据库表中的一种重要操作。

通过使用Load语法,用户可以方便快捷地将大量数据批量导入数据库,提高数据导入的效率。

在本文中,我们将介绍Load语法的基本概念、使用方法以及注意事项,帮助读者更好地理解和应用Load语法。

同时,我们还将对Load语法在实际应用中的作用和意义进行讨论,展望未来Load语法在数据库管理中的发展前景。

通过本文的阅读,读者将能够全面了解达梦数据库中Load语法的使用和优势,为日后的数据库操作提供参考和指导。

1.2 文章结构:本文主要分为引言、正文和结论三个部分。

- 引言部分将对达梦数据库load语法进行总体概述,介绍文章的结构和目的。

- 正文部分将详细介绍达梦数据库load语法的相关内容,包括语法的介绍、使用方法和注意事项。

- 结论部分将对本文进行总结,并探讨达梦数据库load语法的应用推广和未来展望。

1.3 目的:在本文中,我们将重点讨论达梦数据库的Load语法,包括其介绍、使用方法和注意事项。

通过深入了解Load语法的相关知识,读者可以更好地掌握达梦数据库的数据加载操作,提高数据处理效率和准确性。

同时,通过本文的学习,读者也能够了解在实际应用中如何利用Load语法进行数据操作,从而更好地应用达梦数据库解决实际问题。

我们希望通过本文的分享,读者可以更加深入地了解达梦数据库Load语法,从而更好地应用于自己的工作和项目中。

2.正文2.1 Load语法介绍Load语法是指在达梦数据库中用来加载数据的一种语法。

通过Load 语法,用户可以方便地将数据从外部文件导入到数据库表中,实现数据的快速导入与更新。

在实际应用中,Load语法可以大大提高数据导入的效率,特别适用于需要批量导入大量数据的场景。

达梦数据库中的Load语法支持多种数据格式,包括文本文件和二进制文件,用户可以根据实际需求选择合适的数据格式进行导入。

ORACLE中如何使用SQLLOAD批量添加数据

ORACLE中如何使用SQLLOAD批量添加数据

ORACLE中如何使用SQLLOAD批量添加数据在Oracle数据库中,可以使用SQL*Loader实用程序来批量添加大量数据。

SQL*Loader允许通过一次性加载数据文件来快速高效地添加数据。

以下是使用SQL*Loader批量添加数据的步骤:1.准备数据文件:创建一个纯文本文件,其中包含要加载到数据库中的数据。

每一行数据应该对应于数据库表中的一条记录,字段之间使用特定的分隔符分隔。

2.创建控制文件:控制文件描述了如何加载数据文件中的数据到数据库表中。

控制文件是一个文本文件,其结构由CONTROLFILE语句定义。

在控制文件中,需要指定数据文件的位置、数据文件的格式和字段之间的分隔符等信息。

3. 运行SQL*Loader:使用SQL*Loader工具来加载数据文件中的数据。

``````其中,`<username>`是要登录的数据库用户名,`<password>`是相应的密码,`<database>`是要连接的数据库的SID或服务名,`<controlfile>`是控制文件的路径和名称,`<logfile>`是日志文件的路径和名称。

4. 检查日志文件:SQL*Loader会生成一个日志文件,其中包含加载过程的详细信息和任何错误消息。

通过分析日志文件,可以查看加载的数据数量、成功加载的数据和失败加载的数据等信息。

使用SQL*Loader批量添加大量数据具有以下优点:- 高效:SQL*Loader可以根据配置的并行度,同时加载多个数据文件,以快速将大量数据添加到数据库中。

- 省时:相较于使用INSERT语句逐条插入数据,SQL*Loader可以大大减少添加大量数据的时间。

-易于使用:通过简单的配置,即可批量添加大量数据,无需手动编写INSERT语句或使用其他复杂的加载工具。

但是,使用SQL*Loader批量添加数据也需要注意以下事项:-数据准备:数据文件必须事先准备好,并且该文件必须符合数据库表的结构和约束,例如字段数量和类型等。

SQL Loader的详细语法及实例

SQL Loader的详细语法及实例
INFILE 'tt.date' // 导入多个文件
INFILE * // 要导入的内容就在control文件里 下面的BEGINDATA后面就是导入的内容
INTO TABLE table_name // 指定装入的表
BADFILE 'c:\bad.txt' // 指定坏文件地址
exception
when others then null;
end;
EXIT when l_return is not null;
end loop;
if ( l_return is null )
then
l_return :=
new_time( to_date('01011970','ddmmyyyy') + 1/24/60/60 *
(DEPTNO,
DNAME "upper(:dname)", // 使用函数
LOC "upper(:loc)",
LAST_UPDATED date 'dd/mm/yyyy', // 日期的一种表达方式 还有'dd-mon-yyyy' 等
ENTIRE_LINE ":deptno||:dname||:loc||:last_updated"
-- FIELDS TERMINATED BY x'09'
(DEPTNO,
DNAME,
LOC
)
BEGINDATA
10 Sales Virginia
3 ***** 指定不装载那一列
LOAD DATA

sqlloader使用指南

sqlloader使用指南

sqlloader使用指南Oracle SQL*Loader 使用指南整理:Angel.JohnSQL*Loader是Oracle数据库导入外部数据的一个工具.它和DB2的Load工具相似,但有更多的选择,它支持变化的加载模式,可选的加载及多表加载.如何使用 SQL*Loader 工具我们可以用Oracle的sqlldr工具来导入数据。

例如:sqlldr scott/tiger control=loader.ctl控制文件(loader.ctl) 将加载一个外部数据文件(含分隔符). loader.ctl如下:load datainfile 'c:\data\mydata.csv'into table empfields terminated by "," optionally enclosed by '"'( empno, empname, sal, deptno )mydata.csv 如下:10001,"Scott Tiger", 1000, 4010002,"Frank Naude", 500, 20下面是一个指定记录长度的示例控制文件。

"*" 代表数据文件与此文件同名,即在后面使用BEGINDATA段来标识数据。

load datainfile *replaceinto table departments( dept position (02:05) char(4),deptname position (08:27) char(20))begindataCOSC COMPUTER SCIENCEENGL ENGLISH LITERATUREMATH MATHEMATICSPOLY POLITICAL SCIENCEUnloader这样的工具Oracle 没有提供将数据导出到一个文件的工具。

如何利用SQLloader导入数据

如何利用SQLloader导入数据

SQL_loader批量上传数据1.注释在工作中,很多时候会碰到如下情形:需要将excel中的数据批量上传到ORACLE 表中。

若是是小数据量,如几十条至几百条,那么用plsql dev工具,在查询命令后加上for update 然后解锁,把数据复制粘贴进去就能够够了。

但如果是碰到大数据量几万至几十万时,上述方式就不可行了。

如下介绍如何利用oracle自带的sqlloader上传数据。

2.SQL_LOADER上传数据2.1 sql_loader说明sql*loader是oracle自带程序。

需要上传数据的本机只要安装了oracle数据库或客户端就会自动集成该工具。

可是不同的版本对操纵文件的写法要求有所不同。

具体可参见节或附件操纵文件的内容。

2.2编辑数据文件要利用sql*loader,其数据文件必需是两种,一种是 *.txt 的文本文件,另一种是 *.csv 的文件。

例如,需要上传的数据是利用excel编辑的,那么只需要将excel 另存为[制表符分割的 txt 文件]或另存为[逗号分割的csv 文件]即可。

上图是将excel文件另存为“文本文件(制表符分割)(*.txt)”文件格式后的成效要将excel文件转换成csv格式同上面一样,另存为“CSV(逗号分割)(*.csv)”格式即可。

如以下图:注意:CSV格式的文件打开后跟excel的样式差不多,但实际存储方式不一样。

在磁盘上CSV格式的文件中每一个格子中的数据利用逗号分割开存储的。

如此的存储方式很有效,方面后面写操纵文件。

操纵文件在操纵读取数据的时候直接以逗号为标记读取数据。

其实,plsql dev在导出文件的时候就可选择存储为csv 格式。

依照以上方式,数据文件就预备好了。

2.3编写操纵文件操纵文件其实确实是SQL_LOADER上传数据时需要运行的脚本,其后缀名为ctl。

操纵文件写明了数据文件的位置、加载数据的方式、加载到哪个表、如何读取数据等信息。

SQLLoader 详细使用教程

SQLLoader 详细使用教程

SQL*Loader 详细使用教程SQL*Loader是Oracle提供的用于数据加载的一种工具,它比较适合业务分析类型数据库(数据仓库),能处理多种格式的平面文件,批量数据装载比传统的数据插入效率更高。

其示意图如下:控制文件(Control File) (.ctl):用于控制数据导入的行为方式的文件(最重要的文件)参数文件(可选)(Parameter File) (.par):可以把参数直接写在控制文件里,也可以单独写一个参数文件数据文件(Data Files) :放置源数据的平面文件(可以一次性导入多个数据文件)坏文件(Bad File) (.bad):在数据加载时,把无法正确加载的数据放入错误文件中(比如数据格式、数据类型问题等)丢弃文件(可选)(Discard File) (.dsc):有些数据,虽然数据格式、数据类型没有问题,但它被逻辑条件过滤掉了(由控制文件WHEN设置),会被放入丢失文件日志文件(Log File) (.log):记录SQL*Loader的数据加载过程SQL*Loader的数据加载方式SQL*Loader支持3种数据加载方式,分别是:∙传统路径加载(direct=false):等同于insert语句∙直接路径加载(direct=true):绕过SGA,把数据直接导入高水位线(HWM)以上,可设置并行加载,性能比传统路径加载更高,但限制也更多∙外部表加载(较少用):先为数据文件上创建一个外部表,然后再把数据从外部表insert 到目标表中数据文件的记录格式从SQL*Loader的角度来看,数据文件里的数据是以记录组织的,分为以下三种记录格式(由控制文件的INFILE参数设置):定长记录格式这种方式灵活性最差,但性能却最好,它的语法如下:INFILE datafile_name "fix n"表示数据文件里的每个记录定长为n个字节。

变长记录格式INFILE "datafile_name" "var n"变长记录分为两个部分:记录长度和记录本身,看下面这个例子:load datainfile 'example.dat' "var 3"into table examplefields terminated by ',' optionally enclosed by '"'(col1 char(5),col2 char(7))example.dat:009hello,cd,010world,im,012my,name is,var 3 表示记录长度为3个字节,紧跟其后的就是记录本身,上例中,第一个记录长度为9个字节(009),值为其后的9个字节(hello,cd,);第二个记录长度为10个字节(010),值为其后的10个字节(world,im,\n),\n表示第一行行尾的换行符;以此类推。

SQL-LOADER 在数据装载中的应用

SQL-LOADER 在数据装载中的应用

SQL*LOADER 在数据装载中的应用【摘要】数据装载是数据库的重要应用之一,从数据库的装载工具入手,将外部的数据导入指定的数据对象之中。

数据库的装载工具有多种,本文主要研究装载工具SQL*LOADER在数据装载中的应用,文章从SQL*LOADER的几种数据的载入方式,这几种载入方式的区别,并以百万数据的海量导入导出进行深入的剖析,从而说明数据的装载的重要性。

【关键词】sql*loader;装载模式;数据数据库在通信服务业,银行业,金融业等都占有重要的地位,数据库的核心内容就是数据,少量的数据我们可以通过手工的录入完成,但仅仅少量的数据不能满足我们现今对数据的需求,数据库的出现使得我们对信息的录入与输出变得更加方便与简洁。

录入海量的数据,简单快捷的对数据库进行更新,都需要数据装载工具。

1.数据装载工具数据装载工具主要用于数据库的海量数据的导入,它最主要的特性是快速性,使用简单快捷,在oracle中数据装载工具有多种,主要是数据泵、导入导出工具(export&import)、sql*loader[1]。

其中sql*loader可以快速的将文本格式存放的数据导入数据库,不存在数据库的兼容性的问题,但它与前两种方式相比会慢一点,但是我们可以通过外部表的形式来解决。

所谓外部表就是直接读取系统中的数据,在每次进行数据导入之后我们都进行了日志的查看,日志文件里描述了整个sqlldr的装载行为,其中包括目录对象和外部表的创建,那么我们通过修改日志即可完成创建外部表的脚本,那么运行此脚本即可创建出外部表。

外部表并不是真正的将数据装载到数据库中去了,而是通过一种形式使用户可以直接读取系统中的数据,因此在数据库中针对外部表的数据只能进行select的操作,如要修改数据只能对表进行修改。

那么接下来我们就从具体的实例来研究sql*loader。

2.sql*loader简介[2]sql*loader是oracle公司提供的数据装载工具,内嵌于oracle之中,不用再自行安装它的固定的装载名令是sqlldr,在应用它的时候必须指定装载数据到哪个schema里面去,并且要指定sqlldr的控制文件,在控制文件里,也要指定要装载的数据在哪,指定要把这些数据装载到哪个表里,数据如何分割等。

SQLLoader的使用方法

SQLLoader的使用方法

SQL Loader的使用方法Oracle SQL Loader的详细语法SQL*LOADER是ORACLE的数据加载工具,通常用来将操作系统文件迁移到ORACLE数据库中。

SQL*LOADER是大型数据仓库选择使用的加载方法,因为它提供了最快速的途径(DIRECT,PARALLEL)。

现在,我们抛开其理论不谈,用实例来使您快速掌握SQL*LOADER 的使用方法。

首先,我们认识一下SQL*LOADER。

在NT下,SQL*LOADER的命令为SQLLDR,在UNIX下一般为sqlldr/sqlload。

如执行:d:\oracle>sqlldrSQL*Loader: Release 8.1.6.0.0 - Production on 星期二 1月 8 11:06:42 2002(c) Copyright 1999 Oracle Corporation. All rights reserved.用法: SQLLOAD 关键字 = 值 [,keyword=value,...]有效的关键字:userid -- ORACLE username/passwordcontrol -- Control file namelog -- Log file namebad -- Bad file namedata -- Data file namediscard -- Discard file namediscardmax -- 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 path data saves(默认: 常规路径 64, 所有直接路径)bindsize --Size of conventional path bind array in bytes(默认65536)silent --Suppress messages during run (header,feedback,errors,disca rds,partitions)direct -- use direct path (默认FALSE)parfile --parameter file: name of file that contains parameter specificationsparallel -- do parallel load (默认FALSE)file -- File to allocate extents fromskip_unusable_indexes --disallow/allow unusable indexes or index partitions(默认FALSE)skip_index_maintenance --do not maintain indexes, mark affected indexes as unusable(默认FALSE)commit_discontinued --commit loaded rows when load is discontinued(默认FALSE)readsize -- Size of Read buffer (默认1048576)PLEASE NOTE: 命令行参数可以由位置或关键字指定。

使用SQLLoader

使用SQLLoader

使用SQL * Loader----by zsq 2010.11.9一:SQL Loader简介:1:数据装入的工具,如:展开(flat)文件中的数据(逗号定界文本文件)。

2:SQL*Loader单一目的就是从展开文件读取数据,然后把数据放入Oracle数据库。

3:可以实现如下功能:。

从定界文本文件装载数据,例如逗号定界文件。

从固定宽度的文本文件装载数据。

从二进制文件…。

将多个输入记录结合成一个逻辑记录。

从一个逻辑记录存储数据到一个表或几个表。

写SQL表达式并使它生效,传输从文件读取的数据。

将多个数据文件中的数据合并成一个文件。

在输入文件中过滤数据,装载被选择的记录。

收集坏(Bad记录,即不能被装载的记录,到一个能修复它们的分离文件。

其它更多功能二:理解SQL * Loader控制文件:1:使用SQL* Loader 条件:。

需要一个数据库。

需要装载的展开文件。

描述展开文件内容的控制文件2:写控制文件时要考虑的问题:。

那个文件或那些文件包含需要装载的数据。

哪个或那些表是要装载的。

装载的数据格式是什么样的。

没有装载的数据如何处理3:指定输入文件:Infile参数A:Infile * #控制文件的数据情况BegindataDataDataB:分离文件中的数据:Infile …file_name.csv‟C:多个文件情况:Infile …file_name1.dat‟Infile …file_name2.dat‟ #.dat默认扩展名4:将数据装入非空表:LOAD DATAINFILE …file_name.csv‟APPEND…….四个关键字:。

INSERT:指定要装一个空表,如果有数据,刚终止。

APPEND:添加数据到表。

REPLACE:装载前,清空表中的所有数据。

TRUNCATE:与上面一样,只是有用truncate删除数据。

不是用delete。

5:指定装载表:(子句中最复杂的,能指定输入文件中数据的格式):装载一个表情况:#也可以装载多个表。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
time_loaded "to_char(SYSDATE, 'HH24:MI')",
data1 POSITION(1:5) ":data1/100",
data2 POSITION(6:15) "upper(:data2)",
data3 POSITION(16:22)"to_date(:data3, 'YYMMDD')"
LOAD DATA
INFILE *
INTO TABLE DEPT
REPLACE
FIELDS TERMINATED BY ','
TRAILING NULLCOLS // 其实下面的ENTIRE_LINE在BEGINDATA后面的数据中是没有直接对应
// 的列的值的 如果第一行改为 10,Sales,Virginia,1/5/2000,, 就不用TRAILING NULLCOLS了
'dd/mm/yyyy',
'dd/mm/yyyy hh24:mi:ss' );
l_return date;
begin
for i in 1 .. l_fmts.count
loop
begin
l_return := to_date( p_string, l_fmts(i) );
exception
LOAD DATA
INFILE *
INTO TABLE DEPT
REPLACE
FIELDS TERMINATED BY WHITESPACE
-- FIELDS TERMINATED BY x'09'
(DEPTNO,
DNAME,
LOC
)
BEGINDATA
10 Sales Virginia
FIELDS TERMINATED BY ','
( field1,
field2 FILLER,
field3
)
导入多行记录:
可以使用下面两个选项之一来实现将多行数据导入为一个记录:
CONCATENATE: - use when SQL*Loader should combine the same number of physical records together to form one logical record.
call_b_no POSITION(12:29) CHAR
)
导入时跳过某些字段:
可用 POSTION(x:y) 来分隔数据. 在Oracle8i中可以通过指定 FILLER 字段实现。FILLER 字段用来跳过、忽略导入数据文件中的字段.如:
LOAD DATA
TRUNCATE INTO TABLE T1
APPEND
INTO TABLE my_selective_table
WHEN (01) <> 'H' and (01) <> 'T' and (30:37) = '19991217'
(
region CONSTANT '31',
service_key POSITION(01:11) INTEGER EXTERNAL,
INTO TABLE DEPT
REPLACE
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
(DEPTNO,
DNAME,
LOC
)
BEGINDATA
10,Sales,"""USA"""
20,Accounting,"Virginia,USA"
)
BEGINDATA
11111AAAAAAAAAA991201
22222BBBBBBBBBB990112
LOAD DATA
INFILE #39;bad_orders.txt'
APPEND
INTO TABLE mailing_list
when others then null;
3) 通过指定 UNRECOVERABLE选项,可以关闭数据库的日志。这个选项只能和 direct 一起使用。
4) 可以同时运行多个导入任务.
常规导入与direct导入方式的区别:
常规导入可以通过使用 INSERT语句来导入数据。Direct导入可以跳过数据库的相关逻辑(DIRECT=TRUE),而直接将数据导入到数据文件中
mailing_state
)
将数据导入多个表:
如:
LOAD DATA
INFILE *
REPLACE
INTO TABLE emp
WHEN empno != ' '
( empno POSITION(1:4) INTEGER EXTERNAL,
ename POSITION(6:15) CHAR,
deptno POSITION(17:18) CHAR,
mgr POSITION(20:23) INTEGER EXTERNAL
)
INTO TABLE proj
WHEN projno != ' '
( projno POSITION(25:27) INTEGER EXTERNAL,
30,Consulting,Virginia
40,Finance,Virginia
50,"Finance","",Virginia // loc 列将为空
60,"Finance",,Virginia // loc 列将为空
2 ***** FIELDS TERMINATED BY WHITESPACE 和 FIELDS TERMINATED BY x'09' 的情况
导入数据时修改数据:
在导入数据到数据库时,可以修改数据。注意,这仅适合于常规导入,并不适合 direct导入方式.如:
LOAD DATA
INFILE *
INTO TABLE modified_data
( rec_no "my_db_sequence.nextval",
region CONSTANT '31',
(2)INSERT 表示装入空表,有数据则停止。默认值
(3)REPLACE 原先表中如果有数据,会被删除
(4)TRUNCATE 如果要载入的数据与现在的数据相同,载入的数据替换现存的数据。
fields terminated by ','
//数据用是','分隔,也可用其他字符,如'|'
一般情况下是在导入数据文件数据后提交的。
也可以通过指定 ROWS= 参数来指定每次提交记录数。
提高 SQL*Loader 的性能:
1) 一个简单而容易忽略的问题是,没有对导入的表使用任何索引和/或约束(主键)。如果这样做,甚至在使用ROWS=参数时,会很明显降低数据库导入性能。
2) 可以添加 DIRECT=TRUE来提高导入数据的性能。当然,在很多情况下,不能使用此参数。
trailing nullcols //表示如果有字段为空也要导入
c constant 100 // 如果表中字段比文本文件字段还多,则可在控制文件中加上constant关键字,并给出某个固定值
日期时间的处理: colname date "yyyy-mm-dd hh24:mi:ss"
字符串超长(超过255字符)的处理: colname char(1000) 其中1000可自行确定
)
BEGINDATA
10,Sales,Virginia,1/5/2000
20,Accounting,Virginia,21/6/1999
30,Consulting,Virginia,5/1/2000
40,Finance,Virginia,15/3/2001
6 ***** 使用自定义的函数 // 解决的时间问题
sqlldr userid=test/test control=abc.ctl
注:
infile '1.txt' //需要装载的数据文件的路径,如果数据在本文件中,则用infile *
append into table test //数据载入的表:
(1)append 表示表中有数据,加在后面
=====================================================================================
//////////// 注意begindata后的数值前面不能有空格
1 ***** 普通装载
LOAD DATA
INFILE *
DNAME,
LOC
)
BEGINDATA
20,Something Not To Be Loaded,Accounting,"Virginia,USA"
4 ***** position的列子
LOAD DATA
INFILE *
INTO TABLE DEPT
REPLACE
( DEPTNO position(1:2),
相关文档
最新文档