SQL Loader Utility In Oracle

Abstract: This article is about uploading the bulk of millions of pieces of data in a single go, in a single table.

Introduction

Relational DBMS gives us many features which we generally are not aware of in real life. If you are lucky, you will get to know some features through colleagues, or by watching tutorials; or when you are stuck in day to day escalation from the support team, when the utility, they have given for inserting, is running slowly.

Agenda: Uploading bulk data from CSV file to the database, using SQL Loader utility.

Here, in the article, I will be inserting 2 lakh records in the table, which I was finding as overhead in my day to day development life.

Let’s get started. I have a CSV file with 2,00,999 records. In order to insert the same, follow the below steps:

  1. Go to the database and search for table ( ex TblEmployee)
  2. Right click on table and select import data.

     Import data
    Figure 1.0: Demonstrating Import data feature in SQL developer.

  3. Search for the file that you want to insert. Click OK.

    csv file
    Figure 2.0: Selecting your CSV file.

  4. Now, the following window will open. You can define the delimiter as per your need. A CSV file has ‘,’ as a delimiter, by default.

    records from csv
    Figure 3.0: Checking the records from CSV file.

  5. Cross verify your records. If you find any redundant data that you don’t want to insert, then please correct the same in the file.

    Once checked, click on Next.

    Select the import method
    Figure 4.0: Select the import method.

  6. Select import method as SQL Loader Utility, as shown below:

    SQL Loader Utility
    Figure 5.0: SQL Loader Utility
  7. To remove the row limit, clear the 100 value from the textbox and click Next.

  8. Now, in Column definition, you need to map your CSV headers with the table column.

  9. mapping csv headers
    Figure 6.0: Mapping CSV headers with table column.

  10. Click Next.

  11. setting up configuration
    Figure 7.0: Setting up configuration path for the log file and ctl file.

  12. Now, set the path for the log file, bad file, and batch file, as shown below:

  13. setting up configuration
    Figure 8.0: Setting up configuration path for the log file and ctl file.

  14. Once you are done with the path configuration, click on Next.

  15. verifying
    Figure 9.0: Verifying the configuration done in the SQL Loader.

  16. Click on "Verify" to verify the configuration. If you have mapped two CSV headers with the same column, this window will throw an error.

  17. verified
    Figure 10.0: Verified successfully.

  18. Once everything is verified, click on Finish. We can see that the files are created to our configured folder.

  19. Batch File
    Figure 11.0: Batch file is generated.
Now, before executing, I want to you to note down one more requirement. What if one of my columns is using oracle sequences for the column value? How should we proceed to do the same?

In ctl file generated, we need to add a simple line to achieve the same, as shown below: 
  1. load data infile 'C:\Users\Saillesh.pawar\Desktop\RRP_B_E_01082016new.csv' "str '\r\n'"  
  2. append  
  3. into table tableName  
  4. fields terminated by ','  
  5. OPTIONALLY ENCLOSED BY '"' AND '"'  
  6. trailing nullcols  
  7. ( EMPLOYEE_ID CHAR(4000),  
  8. FIRST_NAME CHAR(4000),  
  9. EMAIL_ID CHAR(4000),  
  10. MOBILE_NO CHAR(4000),  
  11. CITY CHAR(4000),  
  12. STATE CHAR(4000),  
  13. DEVICE_TYPE CHAR(4000),  
  14. FLAG CHAR(4000),  
  15. UD CHAR(4000),  
  16. COUPON_CODE CHAR(4000),  
  17. ADDED_DATE DATE "dd-mm-yy"  
  18. )  
After adding Sequence command
  1. load data infile 'C:\Users\Saillesh.pawar\Desktop\01082016new.csv' "str '\r\n'"  
  2. append  
  3. into table REFERRAL_TRANS_DATA  
  4. fields terminated by ','  
  5. OPTIONALLY ENCLOSED BY '"' AND '"'  
  6. trailing nullcols  
  7. ( EMPLOYEE_ID CHAR(4000),  
  8. FIRST_NAME CHAR(4000),  
  9. EMAIL_ID CHAR(4000),  
  10. MOBILE_NO CHAR(4000),  
  11. CITY CHAR(4000),  
  12. STATE CHAR(4000),  
  13. DEVICE_TYPE CHAR(4000),  
  14. FLAG CHAR(4000),  
  15. UD CHAR(4000),  
  16. COUPON_CODE CHAR(4000),  
  17. ADDED_DATE DATE "dd-mm-yy",  
  18. SR_NO "REFERRAL_TV_SR_NO.nextval"  
  19. )  
Once we have resolved this problem, let's look into another problem.
 
What if I don’t have access to the server where db is installed  and I don’t have tnsnames.ora files for the tns of database?
 
In that case, I will just pass the full tns to the batch file. I will edit the batch file and insert the following commands after sqldr.
  1. userid='user/user123@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.01)(PORT=1521))(CONNECT_DATA=(SERVER=dedicated)(SERVICE_NAME=tns)))'  
After all the commands, write pause for pausing the cmd.
  1. sqlldr userid='user/user123@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.01)(PORT=1521))(CONNECT_DATA=(SERVER=dedicated)(SERVICE_NAME=tns)))'  
  2. CONTROL=RRP_B_E_01082016new.ctl LOG=D:\bulkUploadSqlDeveloper\RRP_B_E_01082016new.log BAD=D:\bulkUploadSqlDeveloper\RRP_B_E_01082016new.bad skip=1   
  3. Pause  
Once we are done with this, we are ready to execute our batch file.

Running Batch File
Figure12.0: Running Batch File.

After running the batch file, you will get the following output:

Records
Figure 13.0: Records are getting inserted

You can see your table records for the status of the job. The SQL Loader Utility helps you upload the bulk data from CSV or other types of files, in an easy and efficient manner.

SQL loader
Figure 14.0: SQL Loader gets completed.

Once all the records are inserted, we can check the same in our db.

count
Figure 15.0: Count reconciled with database.

Now, if we want to insert the same values into another table, then we need to add some more configuration in our cdl file below the first table, as shown below:
  1. into table tableName  
  2. fields terminated by ','   
  3. OPTIONALLY ENCLOSED BY '"' AND '"'  
  4. trailing nullcols  
  5. (  
  6. "Empl ID" FILLER POSITION(1) CHAR--start from the first location of csv header  
  7. " Name" FILLER, --by pass this header  
  8. "Email ID" filler, --by pass  
  9. mobileno "Substr(:mobileno,1,10)"first header  
  10. City filler, --by pass  
  11. State filler,--by pass  
  12. Type filler,--by pass  
  13. flag filler,--by pass  
  14. ud filler,--by pass  
  15. pmsg_Val ,   
  16. email_id ":pmsg_Val"--using above value for the other columns.  
  17. employee_id ":pmsg_Val",  
  18. pmsg_status "1",  
  19. item_count "1",  
  20. pmsg_id "41",  
  21. create_dt "SYSDATE+1",  
  22. product_id "1000461",  
  23. expiry_date "SYSDATE+14"  
  24. )  
I hope this article was useful for uploading bulk data from CSV file, using SQL Loader.

References
  • https://community.oracle.com/thread/699631?start=0&tstart=0
  • http://www.thatjeffsmith.com/archive/2012/08/using-oracle-sql-developer-to-setup-sqlloader-runs/


Similar Articles