Skip to Main Content
  • Questions
  • SQL*Loader save filename into table column

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Hasan.

Asked: July 11, 2017 - 6:41 pm UTC

Last updated: July 14, 2017 - 9:40 am UTC

Version: 12.1.0.2

Viewed 10K+ times! This question is

You Asked

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
           ( 
             INSERT_DATE EXPRESSION "current_timestamp(3)",
             FILE_NAME CHAR(4000),
             COLUMN1 CHAR(4000)
             COLUMN2 CHAR(4000)
           )

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.

Thanks in advance

and Chris said...

I'm not aware of a way to get the filename in SQL*Loader and pass it as a column. So here's a couple of options:

- Add the filename to every line in the file.
- Declare the filename as a constant for a column:

FILE_NAME CHAR(4000) constant 'file'


Then find/replace it each time before calling the script.

I'm no Windows scripting expert either. But this gives several solutions for replacing text in a file:

https://stackoverflow.com/questions/60034/how-can-you-find-and-replace-text-in-a-file-using-the-windows-command-line-envir

Rating

  (1 rating)

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

Comments

A reader, March 07, 2018 - 1:07 pm UTC

Thanks a lot.......!!!!!!!! Really helpful.....!!!!!!!