Inserting values to database

hi ,

I'm new to Unix shell scripting.

I need help to insert read csv which has two columns -emp no and logged date.

csv file is a large file so i want to keep the insertion query in a separate .sql file.

csv file looks this:

empno | loggeddate
___________________
5666 , "2-mar-2017"
7888, "3-mar-2017"
......

Please help .

Thanks in advance.

I would guess your db is oracle. What shell and OS (ksh, bash, linux, and so on) are you using? If you can help us to help you things will go very well.

Hi,

Yes,database is oracle.I'm using bash.

Thanks

Do you know about using sqlldr? It is a lot faster than trying to write and run a sql file with thousands of insert statements, and is meant to do exactly what you want.

sqlldr will be in your PATH, if you have your environment variables set so that

sqlplus preema/password

connects you to the db you want.
loader1.dat is a control file in the current directory

load data
INFILE 'myinputfilename.csv'
INTO TABLE mytablename
APPEND
FIELDS TERMINATED BY ','
(empno,
 logged_date DATE "DD-MON-YYYY")

The log file shows errors. If there are errors this helps you to correct input
and remove the good lines that were entered. Then you can rerun.
Shell command:

sqlldr userid=preema/password control=loader1.dat log=loader.log
1 Like

Hi ,
Thanks it works!!!

But in case I need to insert these data to a file say - master.log. what should be done?

What do you mean by 'insert to a file' ? The csv file you used as the source of data is already a file isn't it?