Download How to set up and use SQL Loader

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Contents
Install Oracle Client ...................................................................................................................................... 2
Connect to the database .............................................................................................................................. 3
Prepare SQL Loader files ............................................................................................................................ 10
Run SQL Loader .......................................................................................................................................... 14
TIPS ............................................................................................................................................................. 14
Install Oracle Client
1. Download and install Oracle client. Go to www.oracle.com
NOTE: When the installer asks, select an Administrator installation
Connect to the database
1. Create a tnsames.ora file and drop it in the location below
C:\app\product\11.2.0.3\client\network\admin
2. Input the new database service in the file
NOTE: In this example, I am interested in adding a connection called TESTDEV
3. Click on Net Configuration Manager
4. Select Local Net service Name Configuration
5. Click Add
6. Type the fully qualified global database name
7. Select TCP
8. Enter the database servername. Also, enter the port that database uses
9. Click on Perform a test
10. Click Next. If test does not work, click on the change login button and try another user
11. You may use the default Service Name. Click next
12. Click Next
13. Click Next. You are complete
14. When you use sqlplus just add the servicename after the username
Prepare SQL Loader files
1. Create a folder called SQLLOADER
2. Create a sub folder in SQLLOADER called TEST
C:\ SQLLOADER\TEST
3. Using any text editor, create the following blank files:
NOTE: I am naming the files based on the data I am loading. Each file has a purpose.
File Name
Purpose
PeopleData.log
Log file for SQL Loader. Displays success or
failed details
PeopleData.DAT
File containing the data. Extension and name
may vary from client to client
PeopleData.CTL
Field mapping file for SQL Loader
PeopleData.bad
File with records that could not load
PeopleData.bat
Windows batch file to run sql loader
commands. For Linux/Unix, use
PeopleData.sh
4. Open the .CTL file
Input the following in the .CTL file. Ignore the numbers on the left hand side.
--NOTE: This .CTL file is loading all data as a string (default for SQL Loader). Therefore, datatypes
--are not defined after the column names.
--Examples of some datatypes:
--DECIMAL EXTERNAL, CHAR, DATE, INTEGER EXTERNAL, CHAR
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
LOAD DATA
INFILE 'PeopleData.DAT'
BADFILE 'PeopleData.bad'
APPEND INTO TABLE S_MYTABLE
FIELDS TERMINATED BY '|" TRAILING NULLCOLS
(
RECORD_ID SEQUENCE,
COLUMN FILLER, --Used for enclosed field delimited EX: |Sally|Smith|
-- FILLER normally not used if it is sent like this: Sally|Smith
LANIdTX, --LAN ID
FirstNameTX, --FIRSTNAME
LastNameTX, --LASTNAME
EmailTX, --EMAIL
WorkPhoneTX, --PHONE NUMBER
--ExtTX, REMOVED --PHONE EXTENSION
TitleTX, --TITLE
SecurityGroupTX, --SECURITY GROUP
RequestTypeTX --REQUEST TYPE
)
Explanation of each line
Line 1 - key words used for file
Line 2 - INFILE is a keyword. Immediately after, you need to put the data file name
Line 3 - BADFILE is a keyword. Immediately after, you need to put the bad file name
Line 4 - APPEND INTO TABLE are keywords. Immediately after, add the name of the table
Line 5 - FIELDS TERMINATED BY are keywords. Inside the "" is your delimiter for the file. In
Other delimted options are:
o
o
o
o
o
,
::
|
\t
x’09’
Used for CSV files
Used for double colon
Pipe
tab delimited
another method for tab delimited. Example: FIELDS TERMINATED BY x’09’
Line 5 (Optional) If your data is wrapped in quotes and you do NOT want to load the quotes with it, use
the keywords OPTIONALLY ENCLOSED BY ‘”’. This will ignore double quotes wrapped around my data
Example: FIELDS TERMINATED BY ‘|’ OPTIONALLY ENCLOSED ‘”’ TRAILING NULLCOLS
Line 6 - open paren.
Line 7 - RECORD_ID is a name of the column in a table. SEQUENCE is a keyword
in SQL Loader. This is a counter that starts at 1 and increments with each record
Line 8 - FILLER is a keyword. It is used to fill in a column that is not being used.
Not mandatory. It was needed for this case
Lines 9- Define the columns that will be in the data file. The default datatype setting
is VARCHAR2
Line 18- close paren.
5. Open the .BAT file
Input the following in the .CTL file. Ignore the numbers on the left hand side. This sample is for a
windows operating system
1.
2.
3.
4.
5.
6.
echo Go to SQL Loader Directory
c:
cd C:\SQLLOADER\TEST
echo Call SQL LOADER
SQLLDR USERID= DBSchemaName/DBSchemaNamePassword
CONTROL=PeopleData.CTL DATA=PeopleData.DAT LOG=PeopleData.log
ERRORS=50000 SKIP=1
exit
Explanation of each line:
Line 1 - echo command. Nothing special
Line 2 - go to the C drive
Line 3 - change directories to where the SQL Loader files are located
Line 4 - echo command. Nothing special
Line 5 - SQLLDR is a keyword to start up SQL LOADER.
USERID =
the username/password of the database schema
CONTROL =
the name of the CTL file
DATA =
the name of the data file it will read
LOG =
the name of the log file
ERRORS =
the value of how many errors you allow to display
SKIP =
the line number of the data file to skip. This is used when a datafile has a
header with column names. You do not care to load the column names on line 1. So,
you perform a SKIP = 1.
Line 6 – exit
Run SQL Loader
1. To run SQL Loader, just execute the batch file created above.
TIPS
1. Review the .log file for success or failure of the data load
2. If records failed, review the .BAD file to determine which records failed out of the data file
3. If you a column is passed over as blank, you may use the following keywords to assure a value of
NULL is placed in that column.
Example:
LANIDTX NULLIF LANIDTX = BLANKS
4. To remove whitespace before loading data into a table, use the following keywords in the
column
LANIDTX TERMINATED BY WHITESPACE
5. For more examples, go to this website http://psoug.org/reference/sqlloader.html