Sunteți pe pagina 1din 10

SQL*Loader Features

SQL*Loader loads data from external files into tables of an Oracle database. It has a powerful data parsing engine that puts little limitation on the format of the data in the datafile. You can use SQL*Loader to do the following: Load data across a network if our data files are on a different s stem than the database. Load data from multiple datafiles during the same load session. Load data into multiple tables during the same load session. Specif the character set of the data. Selecti!el load data " ou can load records based on the records# !alues$. %anipulate the data before loading it& using SQL functions. 'enerate uni(ue se(uential ke !alues in specified columns. )se the operating s stem#s file s stem to access the datafiles. Load data from disk& tape& or named pipe. 'enerate sophisticated error reports& which greatl aid troubleshooting. Load arbitraril complex ob*ect+relational data. )se secondar datafiles for loading LO,s and collections. t pical SQL*Loader session takes as input a control file& which controls the beha!ior of SQL*Loader& and one or more datafiles. -he output of SQL*Loader is an Oracle database "where the data is loaded$& a log file& a bad file& and potentiall & a discard file. .n example of the flow of a SQL*Loader session is shown in /igure 0+1.

SQL*Loader Parameters
SQL*Loader is in!oked when ou specif the sqlldr command and& optionall & parameters that establish session characteristics. In situations where ou alwa s use the same parameters for which the !alues seldom change& it can be more efficient to specif parameters using the following methods& rather than on the command line: 2arameters can be grouped together in a parameter file. You could then specif the name of the parameter file on the command line using the PARFILE parameter. 3ertain parameters can also be specified within the SQL*Loader control file b using the OPTIONS clause. 2arameters specified on the command line o!erride an parameter !alues specified in a parameter file or OPTIONS clause.

PARFILE (parameter file)


4efault: none PARFILE specifies the name of a file that contains commonl used command+line parameters. /or example& a parameter file named daily_report.par might ha!e the following contents:
USERID=scott CONTROL=daily_report.ctl ERRORS=9999 LO =daily_report.lo!

/or securit reasons& ou should not include our USERID password in a parameter file. SQL*Loader will prompt ou for the password after ou specif the parameter file at the command line& for example:
sqlldr"PARFILE=daily_report.par Pass#ord$"pass#ord

SQL*Loader Control File


-he control file is a text file written in a language that SQL*Loader understands. -he control file tells SQL*Loader where to find the data& how to parse and interpret the data& where to insert the data& and more. .lthough not precisel defined& a control file can be said to ha!e three sections. -he first section contains session+wide information& for example: 'lobal options such as bindsi5e& rows& records to skip& and so on INFILE clauses to specif where the input data is located 4ata to be loaded -he second section consists of one or more INTO"TA%LE blocks. 6ach of these blocks contains information about the table into which the data is to be loaded& such as the table name and the columns of the table. -he third section is optional and& if present& contains input data.

Some control file s ntax considerations to keep in mind are: -he s ntax is free+format "statements can extend o!er multiple lines$. It is case insensiti!e7 howe!er& strings enclosed in single or double (uotation marks are taken literall & including case. In control file s ntax& comments extend from the two h phens "++$ that mark the beginning of the comment to the end of the line. -he optional third section of the control file is interpreted as data rather than as control file s ntax7 conse(uentl & comments in this section are not supported. -he ke words CONSTANT and &ONE ha!e special meaning to SQL*Loader and are therefore reser!ed. -o a!oid potential conflicts& Oracle recommends that ou do not use either CONSTANT or &ONE as a name for an tables or columns.

Control File Contents


-he SQL*Loader control file is a text file that contains data definition language "44L$ instructions. 44L is used to control the following aspects of a SQL*Loader session: 8here SQL*Loader will find the data to load 9ow SQL*Loader expects that data to be formatted 9ow SQL*Loader will be configured "memor management& re*ecting records& interrupted load handling& and so on$ as it loads the data 9ow SQL*Loader will manipulate the data being loaded

Specifying atafiles
-o specif a datafile that contains the data to be loaded& use the INFILE ke word& followed b the filename and optional file processing options string. You can specif multiple files b using multiple INFILE ke words.

E!amples of I"FILE Synta!


-he following list shows different wa s ou can specif INFILE s ntax: 4ata contained in the control file itself:
INFILE""'

4ata contained in a file named sa(ple with a default extension of .dat$


INFILE""sa(ple

4ata contained in a file named data)ile.dat with a full path specified:


INFILE"*c$+topdir+s,-dir+data)ile.dat*"

pecifying #ultiple atafiles


-o load data from multiple datafiles in one SQL*Loader run& use an INFILE clause for each datafile. 4atafiles need not ha!e the same file processing options& although the la out of the records must be identical. /or example& two files could be specified with completel different file processing options strings& and a third could consist of data in the control file. You can also specif a separate discard file and bad file for each datafile. In such a case& the separate bad files and discard files must be declared immediatel after each datafile name. /or example& the following excerpt from a control file specifies four datafiles with separate bad and discard files:
INFILE""(ydat..dat""%ADFILE""(ydat..-ad""DISCARDFILE"(ydat..dis" INFILE""(ydat/.dat" INFILE""(ydat0.dat""DISCARDFILE""(ydat0.dis" INFILE""(ydat1.dat""DISCARD2A3"".4"4

/or (ydat..dat5 both a bad file and discard file are explicitl specified. -herefore both files are created& as needed. /or (ydat/.dat5 neither a bad file nor a discard file is specified. -herefore& onl the bad file is created& as needed. If created& the bad file has the default filename and extension (ydat/.-ad. -he discard file is not created& e!en if rows are discarded. /or (ydat0.dat5 the default bad file is created& if needed. . discard file with the specified name "(ydat0.dis$ is created& as needed. /or (ydat1.dat5 the default bad file is created& if needed. ,ecause the DISCARD2A3 option is used& SQL*Loader assumes that a discard file is re(uired and creates it with the default name (ydat1.dsc.

Identifying ata in t$e Control File %it$ &E'I" A(A


If the data is included in the control file itself& then the INFILE clause is followed b an asterisk rather than a filename. -he actual data is placed in the control file after the load configuration specifications. Specif the %E INDATA statement before the first data record. -he s ntax is:
%E INDATA" data

:eep the following points in mind when using the %E INDATA statement: If ou omit the %E INDATA statement but include data in the control file& SQL*Loader tries to interpret our data as control information and issues an error message. If our data is in a separate file& do not use the %E INDATA statement. 4o not use spaces or other characters on the same line as the %E INDATA statement& or the line containing %E INDATA will be interpreted as the first line of data. 4o not put comments after %E INDATA5 or the will also be interpreted as data.

Specifying atafile Format and &uffering


8hen configuring SQL*Loader& ou can specif an operating s stem+dependent file processing options

string "os_)ile_proc_cla,se$ in the control file to specif file format and buffering. /or example& suppose that our operating s stem has the following option+string s ntax:

4escription of the illustration recsi5e;spec.gif In this s ntax& RECSI&E is the si5e of a fixed+length record& and %UFFERS is the number of buffers to use for as nchronous I<O. -o declare a file named (ydata.dat as a file that contains =>+b te records and instruct SQL*Loader to use = I<O buffers& ou would use the following control file entr :
INFILE"*(ydata.dat*"6RECSI&E"74"%UFFERS"76"

pecifying t$e &ad File


8hen SQL*Loader executes& it can create a file called a bad file or re*ect file in which it places records that were re*ected because of formatting errors or because the caused Oracle errors. If ou ha!e specified that a bad file is to be created& the following applies: If one or more records are re*ected& the bad file is created and the re*ected records are logged. If no records are re*ected& then the bad file is not created. If the bad file is created& it o!erwrites an existing file with the same name7 ensure that ou do not o!erwrite a file ou wish to retain. ?ote: On some s stems& a new !ersion of the file ma be created if a file with the same name alread exists. -o specif the name of the bad file& use the %ADFILE clause& followed b a filename. If ou do not specif a name for the bad file& the name defaults to the name of the datafile with an extension or file t pe of .-ad. You can also specif the bad file from the command line with the %AD parameter described in 3ommand+Line 2arameters. -he s ntax for the bad file is as follows:

E!amples of Specifying a &ad File "ame


-o specif a bad file with filename sa(ple and default file extension or file t pe of .-ad5 enter:
%ADFILE"sa(ple"

-o specif a bad file with filename -ad444. and file extension or file t pe of .re85 enter either of the following lines:
%ADFILE"-ad444..re8 %ADFILE"*+RE9ECT_DIR+-ad444..re8*"

Criteria for Re)ected Records


. record can be re*ected for the following reasons: 1. )pon insertion& the record causes an Oracle error "such as in!alid data for a gi!en datat pe$. @. -he record is formatted incorrectl so that SQL*Loader cannot find field boundaries. A. -he record !iolates a constraint or tries to make a uni(ue index non+uni(ue. If the data can be e!aluated according to the :;EN clause criteria "e!en with unbalanced delimiters$& then it is either inserted or re*ected. ?either a con!entional path nor a direct path load will write a row to an table if it is re*ected because of reason number @ in the pre!ious list. . con!entional path load will not write a row to an tables if reason number 1 or A in the pre!ious list is !iolated for an one table. -he row is re*ected for that table and written to the re*ect file. In a con!entional path load& if the data file has a record that is being loaded into multiple tables and that record is re*ected from at least one of the tables& then that record is not loaded into an of the tables.

Specifying t$e iscard File


4uring execution& SQL*Loader can create a discard file for records that do not meet an of the loading criteria. -he records contained in this file are called discarded records. 4iscarded records do not satisf an of the :;EN clauses specified in the control file. -hese records differ from re*ected records. 4iscarded records do not necessaril ha!e an bad data. ?o insert is attempted on a discarded record. . discard file is created according to the following rules: You ha!e specified a discard filename and one or more records fail to satisf all of the :;EN clauses specified in the control file. "If the discard file is created& it o!erwrites an existing file with the same name& so be sure that ou do not o!erwrite an files ou wish to retain.$ If no records are discarded& then a discard file is not created. -o create a discard file from within a control file& specif an of the following: DISCARDFILE )ile<a(e& DISCARDS& or DISCARD2A3. -o create a discard file from the command line& specif either DISCARD or DISCARD2A3. You can specif the discard file directl b specif ing its name& or indirectl b specif ing the maximum number of discards. -he discard file is created in the same record and file format as the datafile. /or datafiles in stream record format& the same record terminator that is found in the datafile is also used in the discard file.

Specifying t$e iscard File in t$e Control File


-o specif the name of the file& use the DISCARDFILE clause& followed b the filename.

4escription of the illustration discard.gif

the DISCARDFILE clause specifies that a discard filename follows. -he )ile<a(e parameter specifies a !alid filename specification for our platform. .n spaces or punctuation marks in the filename must be enclosed in single (uotation marks. -he default filename is the name of the datafile& and the default file extension or file t pe is .dsc. . discard filename specified on the command line o!errides one specified in the control file. If a discard file with that name alread exists& it is either o!erwritten or a new !ersion is created& depending on our operating s stem

Criteria for iscarded Records


If there is no INTO"TA%LE clause specified for a record& the record is discarded. -his situation occurs when e!er INTO"TA%LE clause in the SQL*Loader control file has a :;EN clause and& either the record fails to match an of them& or all fields are null. ?o records are discarded if an INTO"TA%LE clause is specified without a :;EN clause. .n attempt is made to insert e!er record into such a table. -herefore& records ma be re*ected& but none are discarded.

Loading Logical Records into (a*les


.mong its man functions& the INTO TA%LE clause enables ou to specif the table into which ou load data. -o load multiple tables& ou include one INTO TA%LE clause for each table ou wish to load. -o begin an INTO TA%LE clause& use the ke words INTO TA%LE5 followed b the name of the Oracle table that is to recei!e the data. -he s ntax is as follows:

Partitioned +*)ect Support


SQL*Loader supports loading partitioned ob*ects in the database. . partitioned ob*ect in an Oracle database is a table or index consisting of partitions "pieces$ that ha!e been grouped& t picall b common logical attributes. /or example& sales data for the ear @>>> might be partitioned b month. -he data for each month is stored in a separate partition of the sales table. 6ach partition is stored in a separate segment of the database and can ha!e different ph sical attributes. SQL*Loader partitioned ob*ect support enables SQL*Loader to load the following: . single partition of a partitioned table .ll partitions of a partitioned table

. nonpartitioned table

SQL*Loader Case Studies


SQL*Loader features are illustrated in a !ariet of case studies. -he case studies are based upon the Oracle demonstration database tables& e(p and dept& owned b the user scott. "In some case studies& additional columns ha!e been added.$-he case studies are numbered 1 through 11& starting with the simplest scenario and progressing in complexit . -he following is a summar of the case studies: 3ase Stud 1: Loading Bariable+Length 4ata + Loads stream format records in which the fields are terminated b commas and ma be enclosed b (uotation marks. -he data is found at the end of the control file. 3ase Stud @: Loading /ixed+/ormat /ields + Loads data from a separate datafile. 3ase Stud A: Loading a 4elimited& /ree+/ormat /ile + Loads data from stream format records with delimited fields and se(uence numbers. -he data is found at the end of the control file. 3ase Stud C: Loading 3ombined 2h sical Decords + 3ombines multiple ph sical records into one logical record corresponding to one database row. 3ase Stud E: Loading 4ata into %ultiple -ables + Loads data into multiple tables in one run. 3ase Stud 0: Loading 4ata )sing the 4irect 2ath Load %ethod + Loads data using the direct path load method. 3ase Stud F: 6xtracting 4ata from a /ormatted Deport + 6xtracts data from a formatted report. 3ase Stud =: Loading 2artitioned -ables + Loads partitioned tables. 3ase Stud G: Loading LO,/IL6s "3LO,s$ + .dds a CLO% column called res,(e to the table e(p& uses a FILLER field "res_)ile$& and loads multiple LO,/IL6s into the e(p table. 3ase Stud 1>: D6/ /ields and B.DD.Ys + Loads a customer table that has a primar ke as its OI4 and stores order items in a =ARRA>. Loads an order table that has a reference to the customer table and the order items in a =ARRA>. 3ase Stud 11: Loading 4ata in the )nicode 3haracter Set + Loads data in the )nicode character set& )-/10& in little+endian b te order. -his case stud uses character+length semantics. ttp:<<www.mscd.edu<ittsdba<oradoc=1F<ser!er.=1F<aF0GEE<ch>F.htm *****************case stud ****************************************************** able 0+1 3ase Studies and -heir Delated /iles

Case 1 @ A C E 0 F

.ctl ulcase1.ctl ulcase@.ctl ulcaseA.ctl ulcaseC.ctl ulcaseE.ctl ulcase0.ctl ulcaseF.ctl

.dat ?<. ulcase@.dat ?<. ulcaseC.dat ulcaseE.dat ulcase0.dat ulcaseF.dat

.sql ulcase1.s(l ?<. ulcaseA.s(l ulcaseC.s(l ulcaseE.s(l ulcase0.s(l ulcaseFs.s(l ulcaseFe.s(l

= G 1> 11

ulcase=.ctl ulcaseG.ctl ulcase1>.ctl ulcase11.ctl

ulcase=.dat ulcaseG.dat ?<. ulcase11.dat

ulcase=.s(l ulcaseG.s(l ulcase1>.s(l

unning t$e Case Studies


In general& ou use the following steps to run the case studies "be sure ou are in the ?ORACLE_;O2E+rd-(s+de(o director & which is where the case stud files are located$: 1. .t the s stem prompt& t pe sqlpl,s and press 6nter to start SQL*2lus. .t the user+name prompt& enter scott. .t the password prompt& enter ti!er. -he SQL prompt is displa ed. @. .t the SQL prompt& execute the SQL script for the case stud . /or example& to execute the SQL script for case stud 1& enter the following:
S@LA"B,lcase.

-his prepares and populates tables for the case stud and then returns ou to the s stem prompt. A. .t the s stem prompt& in!oke SQL*Loader and run the case stud & as follows:
sqlldr"USERID=scott"CONTROL=,lcase..ctl"LO =,lcase..lo!

Substitute the appropriate control file name and log file name for the CONTROL and LO parameters and press 6nter. 8hen ou are prompted for a password& t pe ti!er and then press 6nter. ,e sure to read the control file for an notes that are specific to the particular case stud ou are executing. /or example& case stud 0 re(uires that ou add DIRECT=TRUE to the SQL*Loader command line.

S-ar putea să vă placă și