This following illustration implements the process of loading data using SQL*Loader program.
SQL Loader is a utility to load data into a table from flat files. The process requires
1. A file (Readable, i.e., .txt, .dat, etc..) that contains data.
2. A file that has that controls the load process by providing details about file data and delimiter options.
Using the above two files we can load the data into the oracle table as follows.
The following is a text file containing data.
SQL*Loader Control File
This contains the instructions to the sqlldr utility. This tells sqlldr the location of the input file, the format of the input file, and other optional Meta data information required by the sqlldr to upload the data into oracle tables.
The above control file indicates the following:
- infile – Indicates the location of the input data file
- into table – Indicates the table name where this data should be inserted
- fields terminated by – Indicates the delimiter that is used in the input file to separate the fields
- ( id, name, dept, salary ) – Lists the name of the column names in the table into which the data should be uploaded
1. Basic Upload Example Using SQL*Loader
First, create the employee table as shown below.
Next create the control file that explains what needs to be upload and where.
Note: If you have the values inside the data file enclosed with double quote, use this in your control file: fields terminated by “,” optionally enclosed by ‘”‘
Note: If you don’t have the table created, you’ll get the following error message:
You can pass the userid and password to the sqlldr command using any one of the following format. As you see below, both of these will prompt you for control file location, as it was not given in the command line.
Execute the sqlldr command to upload these new record to the empty table by specifying both uid/pwd and the control file location as shown below.
Verify the the records are created in the database
This will create the output log file in the same name as the data file, but with the .log extension (instead of .ctl). Partial output shown below.
Let us say you want to add two new employees to the employee table from the following newemployee.txt file.
If you create a similar control file like the previous example, you might get the following error message.
The above indicates that the table should be empty before you can upload data using sql*loader.
If you like to insert more data to the tables without having to delete the existing rows, use the “append’ command as shown in the following control file.
Now, if you do sqlldr this will append the data.
Verify that the records are appended successfully
3. Data inside the Control File using BEGINDATA
You can also specify the data directly inside the control file itself using BEGINDATA keyword. i.e Anything that comes after BEGINDATA will be treated as data to be uploaded to the table as shown below.
Note: The infile will say ‘*’ in this case, as there is no input data file name for this example.
Execute sqlldr to upload the data from the control file.
4. Date format and Different Delimiter
This example shows how to specify a date format in the control file and how to handle different delimiters in a data file
The following example has different delimiters ($ after name, ^ after department).
Create the following control file and indicate the field delimiters for each and every field using “terminated by” as shown below.
Load the data using sqlldr as shown below.
Verify that the data got loaded properly as shown below.
5. Fixed Length Data Upload
If you have a data file without data that are fixed length (i.e without any delimiter), you can use this example to upload this data.
For this example, let us use the following file which has data that are of fixed length. For example, 1st three characters are always employee number, Next 5 characters are always employee name, etc.
Create the following control file, where you specific the position of each and every field as shown below usig the “Position(start:end)” syntax.
Load this fixed length data using the sqlldr as shown below.
Verify that the data got loaded.
6. Change the data during upload
You can also massage the data and change it during upload based on certain rules.
In the following control file:
- id is incremented by 999 before uploading. i.e if the emp id is 100 in the data file, it will be loaded as 1099
- Convert the name to upper case and load it. This uses the upper function.
- If the department contains the value “Technology” change it to “Techies”. This uses decode function
Load the data using this control file which will massage the data before uploading it.
Verify that the data got changed while loading as per our rules.
7. Load data from multiple files
To load data from multiple files, you just have to specify multiple infile in the control file.
The following control file loads data from two different data files (employee.txt and newemployee.txt) to the employee table.
Load the data using this control file which will upload data from multiple data files as shown below.
8. Load data to Multiple Tables
Create another table called bonus which will have employee id and bonus columns.
Create the employee-bonus.txt data file that contains the fields: id, name, department, salary, bonus
Create the control file as shown below, which will upload the data from the above file to two different tables. As shown below, you should have two “into table” commands, and specify the position of the data which needs to be used to upload the data to that column.
Load the data to multiple tables using this control file as shown below.
Verify that the data got loaded to multiple tables successfully.
9. Handling Bad (Rejected) Records
In the following example, we have two bad records. Employee id 300 and 500 has salary column which is not numeric.
Use the following control file for this example.
Load the data (including the invalid records) using this control file as shown below.
As you see from the abvoe output, it still says “logical record count 5″, but you should check the log files to see if it has rejected any records.
The log file indicates that 2 records are rejected as shown below:
By default the rejected records are stored in a file that has the same name as the data file (but with .bad extension)
As you see below, the employee table has only 3 records (as 2 of them were rejected).
10. Load Specific Rows from a datafile
If you want to load only a specific records from a data file use the WHEN in the control file.
Add the line “when” next to “into table” line. In the following control file, the when clause indicates that it will load only the records that have dept as “Technology”.
Load the selective data (only the “Technology” records) using this control file as shown below.
As you see from the above output, it still says “logical record count 5″, but you should check the log files to see how many records were loaded, and how many records were discarded because it didn’t match the when condition.
The following from the log file shows that 5 records were read, and 2 of them were discarded as it didn’t match the when condition.
Verify that only the selective records were loaded into the table.