I need to import different csv-files into 1 table. I need to use the sqlloader.(Oracle Version 12.1.0.2) This is my control-file:
load data
append
into table SAMP_TABLE
fields terminated by ','
OPTIONALLY ENCLOSED BY '"' AND '"'
trailing nullcols
(
FILE_NAME "MyFileName",
INSERT_DATE EXPRESSION "current_timestamp(3)"
)
And this is my batch-file (I need to do it in a windows-machine):
@echo off
IF NOT EXIST C:\Users\test\csvFiles
IF NOT EXIST C:\Users\test\logfiles
for %%F in ("C:\Users\test\*.csv") do (
SET tmpFile=%%F
SET newFile=%tmpFile:~0,-3%.log
sqlldr db_user/db_pw@db_ip CONTROL='C:\Users\test.ctl' LOG='C:\Users\logfiles\%newFile%' "DATA=%%F" skip=1
move %%F C:\Users\test\csvFiles
)
pause
What it does is, it creates 2 folders at the beginning: "csvFiles" (to move the csv-file to this folder after it is loaded) and "logfiles" (to move the created log-file). (With DATA=%%F i can pass the csv-file to the control-file)
You can see that the first 2 columns on my control-file are "INSERT DATE" which inserts the current_timestamp and "FILE_NAME".
Now my problem now is that I dont know how to pass the file-name of the csv-file(which will be loaded) to the control-file. I want for each import csv-file to insert the file-name of this csv-file into this column. I searched for some solutions but some of them are in UNIX, however I need to do it in Windows.
I would appreciate if someone could help me, since I am not very familiar with batch-scripting or scripting in general.
My solution so far is to create the control-file on the fly and pass the filename:
I am trying to create the control-file on the fly but it is not working. Here is my batch-file ( I have csv-files in my directory so the loop should work):
IF NOT EXIST C:\Users\test\csvFiles
IF NOT EXIST C:\Users\test\logfiles
for %%F in ("C:\Users\test\*.csv") do (
echo load data >test1.ctl
echo append >>test1.ctl
echo into table SAMP_TABLE >>test1.ctl
echo fields terminated by ',' >>test1.ctl
echo OPTIONALLY ENCLOSED BY '"' AND '"' >>test1.ctl
echo trailing nullcols >>test1.ctl
echo ( >>test1.ctl
echo FILE_NAME constant "%FF", >>test1.ctl
echo INSERT_DATE EXPRESSION "current_timestamp(3)", >>test1.ctl
echo >>test1.ctl
echo ) >>test1.ctl
SET tmpFile=%%F
SET newFile=%tmpFile:~0,-3%.log
sqlldr db_user/db_pw@db_ip CONTROL='C:\Users\test1.ctl' LOG='C:\Users\logfiles\%newFile%' "DATA=%%F" skip=1
move %%F C:\Users\test\csvFiles
)
pause
It works only without a for-loop, i dont know why. I would appreciate if someone could help me..when i execute the batch-file it closes automatically again..