How to read a dynamically changing file and load into Oracle?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read a dynamically changing file and load into Oracle?
# 1  
Old 05-23-2006
How to read a dynamically changing file and load into Oracle?

I have a tab delimited file which has 27 character fields. The file needs to be loaded into an Oracle table. But the challenge is that everytime the file comes it may or may not have values in all 27 fields.

PHP Code:
Column Definition of the 27 fields:

TYPEChar (1)
NAMEChar (30)
CUSTOM_VALChar (2)
CUSTOM_DATANAME1Char (30)
CUSTOM_DATATYPE1Char(1)
CUSTOM_DATAFORMAT1Char(8)
CUSTOM_DATANAME2Char (30)
CUSTOM_DATATYPE2Char(1)
CUSTOM_DATAFORMAT2Char(8)

...... and 
so on till CUSTOM_DATAFORMAT8 
For example: If today's file has 5 records, three records have values in all 27 fields and two records have values in 10 fields. The rest of them are blank or empty.

Tomorrow's file will be different...I am not sure how to deal with this dynamically changing file.

I did an extensive search on the forum, but unable to find any related hits. There are hits on how to invoke SQL through UNIX.

Any help will be highly appreciated...

Thank You,
Madhu
# 2  
Old 05-23-2006
Use SQL Loader
# 3  
Old 05-23-2006
Thank You Ambikesh..

Our requirement is only to read this file in UNIX, invoke Oracle and load it into the specific table...

Any shell script examples for this will really help me a lot....
# 4  
Old 05-23-2006
A shell script solution is simple enough but a better solution is to use an external table definition. This is defined in the database and has its own sqlldr like control file incorporated into the table definition. This then should give you access to whatever the file updates are as they occur throughout the day without invoking a shell script.

Whether or not same columns are provided in the file, as long as their location in the fixed width or CSV formatted flat file, you will be fine.
# 5  
Old 05-23-2006
Thank you tmarikle...

I did not quite understand what exactly you meant by it...

Yes...It is a tab delimited file. But the problem is that if there are no values in the rest of the columns, they are not filled with spaces or tabs...

Row1: A B C D E F G H
Row2: A B C D
Row3: A B

I am absolutely not sure how I can do this...
# 6  
Old 05-23-2006
Quote:
Originally Posted by madhunk
Thank you tmarikle...

I did not quite understand what exactly you meant by it...

Yes...It is a tab delimited file. But the problem is that if there are no values in the rest of the columns, they are not filled with spaces or tabs...

Row1: A B C D E F G H
Row2: A B C D
Row3: A B

I am absolutely not sure how I can do this...
Both sqlldr and external tables can process data in the format that you have described. I'll edit this post with some examples shortly.

Additional info:

Here is how you can define an external table using another table and a sqlldr control file to start with.

1) Create your table:

CREATE TABLE my_external_table (
my_key_no VARCHAR2(45)
,load_date DATE
,col_a VARCHAR2(30)
,col_b VARCHAR2(30)
,col_c VARCHAR2(30)
,col_d VARCHAR2(30)
,col_e VARCHAR2(30)
,col_f VARCHAR2(30)
)

2) Create a control file for sqlldr that allows for "missing" columns as per your requirement.

LOAD DATA

INTO TABLE "MY_EXTERNAL_TABLE"
APPEND

FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' AND '"'
TRAILING NULLCOLS

(
my_key_no "SUBSTRB(:my_key_no, 1, 45)"
,load_date "TO_DATE(:load_date, 'YYYYMMDD')"
,col_a "SUBSTRB(:col_a , 1, 30)"
,col_b "SUBSTRB(:col_a , 1, 30)"
,col_c "SUBSTRB(:col_a , 1, 30)"
,col_d "SUBSTRB(:col_a , 1, 30)"
,col_e "SUBSTRB(:col_a , 1, 30)"
,col_f "SUBSTRB(:col_a , 1, 30)"
)

3) Use sqlldr to define a comperable external table definition.

sqlldr user/pwd test.ctl external_table=generate_only

4) The resulting log file contains the external table DDL and examples on how to use it.

CREATE TABLE "SYS_SQLLDR_X_EXT_MY_EXTERNAL_T"
(
"MY_KEY_NO" VARCHAR(255),
"LOAD_DATE" VARCHAR(255),
"COL_A" VARCHAR(255),
"COL_B" VARCHAR(255),
"COL_C" VARCHAR(255),
"COL_D" VARCHAR(255),
"COL_E" VARCHAR(255),
"COL_F" VARCHAR(255)
)
ORGANIZATION external
(
TYPE oracle_loader
DEFAULT DIRECTORY SYS_SQLLDR_XT_TMPDIR_00000
ACCESS PARAMETERS
(
RECORDS DELIMITED BY NEWLINE CHARACTERSET WE8MSWIN1252
BADFILE 'SYS_SQLLDR_XT_TMPDIR_00000':'test.bad'
LOGFILE 'test.log_xt'
READSIZE 1048576
FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY '"' AND """ LDRTRIM
MISSING FIELD VALUES ARE NULL
REJECT ROWS WITH ALL NULL FIELDS
(
"MY_KEY_NO" CHAR(255)
TERMINATED BY "," OPTIONALLY ENCLOSED BY '"' AND """ ,
"LOAD_DATE" CHAR(255)
TERMINATED BY "," OPTIONALLY ENCLOSED BY '"' AND """ ,
"COL_A" CHAR(255)
TERMINATED BY "," OPTIONALLY ENCLOSED BY '"' AND """ ,
"COL_B" CHAR(255)
TERMINATED BY "," OPTIONALLY ENCLOSED BY '"' AND """ ,
"COL_C" CHAR(255)
TERMINATED BY "," OPTIONALLY ENCLOSED BY '"' AND """ ,
"COL_D" CHAR(255)
TERMINATED BY "," OPTIONALLY ENCLOSED BY '"' AND """ ,
"COL_E" CHAR(255)
TERMINATED BY "," OPTIONALLY ENCLOSED BY '"' AND """ ,
"COL_F" CHAR(255)
TERMINATED BY "," OPTIONALLY ENCLOSED BY '"' AND """
)
)
location
(
'test.dat'
)
)REJECT LIMIT UNLIMITED

At any rate, you can read up on external tables at asktom.oracle.com.

Last edited by tmarikle; 05-23-2006 at 05:08 PM..
# 7  
Old 05-23-2006
By the way, if you don't want to use an external table, you can still use sqlldr. You need a controlfile as follows:

Code:
LOAD DATA

INTO TABLE "MY_EXTERNAL_TABLE"
APPEND

FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' AND '"'
TRAILING NULLCOLS
(
my_key_no "SUBSTRB(:my_key_no, 1, 45)"
,load_date "TO_DATE(:load_date, 'YYYYMMDD')"
,col_a "SUBSTRB(:col_a , 1, 30)"
,col_b "SUBSTRB(:col_a , 1, 30)"
,col_c "SUBSTRB(:col_a , 1, 30)"
,col_d "SUBSTRB(:col_a , 1, 30)"
,col_e "SUBSTRB(:col_a , 1, 30)"
,col_f "SUBSTRB(:col_a , 1, 30)"
)

Then you can script your sqlldr call in Unix as follows:

Code:
sqlldr user/password control=test.ctl data=test.dat

Any column missing will be set to null. This is accompished in the "fields terminated by" statement in the control file.

Please note that this example assume CSV not tab delimited. Tab delimited use this syntax:

Code:
FIELDS TERMINATED BY X'09' OPTIONALLY ENCLOSED BY '"' AND '"'
TRAILING NULLCOLS

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to load XML file to Oracle table

Experts. I have created a oracle table as below. create table xml_tab ( File_No number , File_content Varchar2(2000), file_type xmltype ); Daily we are receiving many XML files as below. here is our sample xml file. File1 : (7 Replies)
Discussion started by: vasuvv
7 Replies

2. Shell Programming and Scripting

Parameterizing to dynamically generate the extract file from Oracle table using Shell Script

I have below 2 requirements for parameterize the generate the extract file from Oracle table using Shell Script. Could you please help me by modifying the script and show me how to execute it. First Requirement: I have a requirement where I need to parameterize to generate one... (0 Replies)
Discussion started by: hareshvikram
0 Replies

3. Shell Programming and Scripting

Script to load daily average I/O stats from a .ksh file into Oracle db

Hi can anyone help me with a script to load output of the .ksh file into an Oracle database. I have attached sample output of the information that i need to load to the database (2 Replies)
Discussion started by: LucyYani
2 Replies

4. Shell Programming and Scripting

How to read a dynamically changing file

I want to read a constantly changing file and do some operation on text found in that file. Actually that is log file of linux system and whenever i find a matching string in that file i want to create a text file with timestamp. is it possible to read that file? here is sample output of log... (7 Replies)
Discussion started by: kashif.live
7 Replies

5. UNIX for Dummies Questions & Answers

Read a file dynamically

Hi my requriment is read the file name dynamically my code is #!/bin/sh file="/c/work/loan/" Header_Trailer_move() { sed '1d;$d' $file| cat >sam.txt } Header_Trailer_move in above given path my list of files or there i have to read file dyanamically when i entered particular file name... (2 Replies)
Discussion started by: sgoud
2 Replies

6. Shell Programming and Scripting

Check value of load balance dynamically

Hi All, I want to check load balance(or CPU utilzation) on server dynamically based on that value i want to perform some operation . Means if load balance is <10 then start server . Am using prstat -a 1 commadn it is listing CPU utilzation/load balancing dynamically .. How to take this... (7 Replies)
Discussion started by: vivek1489
7 Replies

7. Shell Programming and Scripting

Can't load external file from unix into Oracle DB

Hi all, I'm traying to run a script on Unix with in it sql +. In this script I want to load a externall file that stand on my unix system into a tmp table of a oracle db, but for some reason it won't work. Find below the script. #!/bin/ksh ORACLE_SID=db1... (7 Replies)
Discussion started by: Tuut-Tuut
7 Replies

8. Shell Programming and Scripting

Load data from a flat file to oracle.

I have a flat file with records like Header 123 James Williams Finance2000 124 Pete Pete HR 1500 125 PatrickHeather Engg 3000 Footer The structure is: Eno:4 characters Name:8 characters Surname : 9 characters Dept:7 characters Sal:4characters These are sample... (1 Reply)
Discussion started by: Shivdatta
1 Replies

9. Shell Programming and Scripting

load a data from text file into a oracle table

Hi all, I have a data like, 0,R001,2,D this wants to be loaded into a oracle database table. Pl let me know how this has to be done. Thanks in advance (2 Replies)
Discussion started by: raji35
2 Replies

10. UNIX for Advanced & Expert Users

How to load comma seperated values file (*.csv) into Oracle table

Hi all I need to input values in a .csv file into my Oracle table running in Unix, I wonder what would be the command to do so... The values are recorded in an excel file and I tried using a formatted text file to do so but failed because one of the field is simply too large to fit in the... (4 Replies)
Discussion started by: handynas
4 Replies
Login or Register to Ask a Question