Skip to Main Content
  • Questions
  • sqloader load multiple file into 1 table

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Hasan.

Asked: July 10, 2017 - 6:54 pm UTC

Last updated: October 21, 2017 - 1:18 am UTC

Version: 12.1.0.2

Viewed 10K+ times! This question is

You Asked

Hi Tom,

I have multiple csv-files in a directory. This directory will be updated with new csv-files. I need to load all csv files with sqloader in 1 table. So all the files have the same columns only different data.

This is how my control file looks:

load data 
infile 'test.csv' "str '\r\n'"
append
into table TABLE1
fields terminated by ','
OPTIONALLY ENCLOSED BY '"' AND '"'
trailing nullcols
           ( COLUMN1 CHAR(4000),
             COLUMN2 CHAR(4000),
             COLUMN3 CHAR(4000)
           ) 



And here is my batch file (used generic names for demostration purposes):

@echo off
sqlldr 'username/pw@dbip' 
CONTROL=test.ctl LOG=C:\Users\Desktop\sqloader_files\test.log 
BAD=C:\Users\Desktop\sqloader_files\test.bad skip=1 
pause


This works fine whith one csv file, however i need to iterate through all files and call sql loader for each file.When i am finished, i just move all the csv files in a different folder and then execute the batch file again when new files come along. Do you think this is a good solution?

I need to do this in Windows, however i am not familiar with any scripting language. Is it possible to do this only with a for loop in cmd? Or do we need to write a script with powershell or some other scripting language in windows? Would appreciate if someone could help me

thx in advance

and Chris said...

You can specify multiple input files in your control file!

Provided they have the same record format, they can all go in the same table. You can do this by listing them out explicitly:

infile 'file1.txt'
infile 'file2.txt'
...


Or using wildcards (? for a single character, * for any number):

infile 'file?.txt'


For example:

C:\temp>dir test*
 Volume in drive C is System
 Volume Serial Number is E6AB-58D8

 Directory of C:\temp

12/07/2017  15:22               127 test.ctl
12/07/2017  15:27                14 test1.txt
12/07/2017  15:27                14 test2.txt
               3 File(s)            155 bytes
               0 Dir(s)  153,746,407,424 bytes free

C:\temp>type test*

test.ctl


load data
infile 'test*.txt'
truncate
into table t
fields terminated by ','
OPTIONALLY ENCLOSED BY '"' AND '"'
 ( x, y )
test1.txt


r1,c2
r2,c2

test2.txt


r3,c2
r4,c2

C:\temp>sqlldr control=test.ctl userid=chris/chris@db


SQL*Loader: Release 12.1.0.2.0 - Production on Wed Jul 12 15:29:08 2017

Copyright (c) 1982, 2014, Oracle and/or its affiliates.  All rights reserved.

Path used:      Conventional
Commit point reached - logical record count 2
Commit point reached - logical record count 4

Table T:
  4 Rows successfully loaded.

Check the log file:
  test.log
for more information about the load.


Or you could build a process to concatenate all the files into one:

C:\temp>type test1.txt test2.txt > big_test.txt

test1.txt



test2.txt



C:\temp>type big_test.txt
r1,c2
r2,c2
r3,c2
r4,c2


Then process that.

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

How to specify skip Rows from multiple files

Raghunath Panigrahi, October 20, 2017 - 2:23 pm UTC

Thanks for your answer, Chris.

I impelemented the solution and it worked well.
But the only problem I couldn't solve is as follows.

My input files have a skip row i.e. the 1st row of all of my input files are the data headers.

Table T:

CREATE TABLE T (
X NUMBER,
Y VARCHAR2(10));


TEST.CTL

OPTIONS (SKIP = 1)
load data
infile 'test*.txt'
truncate
into table t
fields terminated by ','
OPTIONALLY ENCLOSED BY '"' AND '"'
 ( X, Y )


test1.txt

X,Y
1,r1
2,r2


test2.txt

X,Y
3,r3
4,r4


After invoking the sqlloader program, a .bad file generated
named test2.bad with below content.

X,Y


Error in the log file:

Data File test2.txt -

Record 3: Rejected - Error on table T, column X.
ORA-01722: invalid number


Can you please help me with this problem as I want to skip 1st row from every input file.
Connor McDonald
October 21, 2017 - 1:18 am UTC

You could use a WHEN clause,

LOAD DATA
INTO TABLE blah
WHEN [some condition]

but for me, whenever a SQL Loader control file starts to get in any way complicated, I'll try fall back to using an external table, which gives me the full power of SQL interface on the incoming data.