SQL Loader - Use Replace and Append in same control file - oracle

As mentioned in the title, i wish to have a control file to handle this case. The scenario is i have to insert record into different table. For example, WHEN (1:3) is HEA, it need to Append into table header. WHEN (1:3) is DTL it need replace into table Detail. is that possible to do this?

I have a situation where data from one file goes to three tables depending on the first field in the file. The WHEN clause looks at the first field and takes action based on that. Notice that when a 'WHEN' is met, the first field is then skipped by declaring it a filler. To answer your question, I believe you can put the APPEND or REPLACE after the INTO TABLE clause. Give it a try and let us know.
OPTIONS (DIRECT=TRUE)
UNRECOVERABLE
LOAD DATA
APPEND
INTO TABLE TABLE_A
WHEN (01) = 'CLM'
FIELDS TERMINATED BY '|' TRAILING NULLCOLS
( rec_skip filler POSITION(1)
,CLM_CLAIM_ID CHAR NULLIF(CLM_CLAIM_ID=BLANKS)
...
)
INTO TABLE TABLE_B
WHEN (01) = 'SLN'
FIELDS TERMINATED BY '|' TRAILING NULLCOLS
( rec_skip filler POSITION(1)
,SL_CLAIM_ID CHAR NULLIF(SL_CLAIM_ID=BLANKS)
...
)
INTO TABLE TABLE_C
WHEN (01) = 'COB'
FIELDS TERMINATED BY '|' TRAILING NULLCOLS
( rec_skip filler POSITION(1)
,COB_CLAIM
...
)
More info: http://docs.oracle.com/cd/B28359_01/server.111/b28319/ldr_control_file.htm#i1005657

Related

SQL* Loader mapping DataFile Fields in different tables columns

I'm trying to load data from a Datafile in different tables, I read a lot about field declaration and delimitation(Position(n:n), terminated by ). The point is than I'm not sure how to do what I need to do. Let me explain this with an example.
I have two tables (person, phone):
person_table( person_id_pk, person_name) - phone_table(person_id_pk, phone)
I have a datafile with:
$ datafile.txt
1,jack pierson,+13526985442
2,Katherine McLaren,+15264586548
My point is, when I'm declaring my ConfigFile.ctl, how do I specify than the field number 3 (phone field) should be insert or append into "phone_table", and the others two fields (person_id, person_name) should be insert or append into "person_table"
Considering than the fields are not fixed length, my reference is the field position. (Field datafile position)
I was thinking to try something like
$configfile.ctl
LOAD DATA
INFILE datafile.txt
APPEND
INTO TABLE person_table
(
person_id_pk POSITION (*) INTEGER EXTERNAL TERMINATED BY "," ,
person_name POSITION(*+1) CHAR(30) TERMINATED BY ","
)
INTO TABLE phone_table
(
person_id_fk POSITION (*) INTEGER EXTERNAL TERMINATED BY ","
phone ------> Right here is my point, how can I specify to SQL Loader than here
should be the field number 3 from datafile
)
I hope you guys get my point. it is a HUGE issue for me, because i'm dealing with CSV files which contains 60, 80, even 100 fields (columns based on Excel File). And every fields or group of fields could be in different tables.
I really appreciate the guide and help you could grant me. I'm probably wrong about my example and controlfile declarations, I haven't implemented anything yet. So I'm open to every suggest you could give me.
Your control file should look like this. The second "INTO TABLE" Uses POSITION(1) to move the logical "pointer" back to the start of the current line so it can be read again. then the name is skipped by defining it as a FILLER.
LOAD DATA
INFILE datafile.txt
APPEND
INTO TABLE person_table
FIELDS TERMINATED BY "," TRAILING NULLCOLS
(
person_id_pk INTEGER EXTERNAL,
person_name CHAR(30)
)
INTO TABLE phone_table
FIELDS TERMINATED BY "," TRAILING NULLCOLS
(
person_id_fk POSITION(1) INTEGER EXTERNAL,
x_name FILLER,
phone CHAR(12)
)

inserting sequence with sqloader

I have a weird problem with sqloader..In my database I created a sequence like this:
CREATE SEQUENCE TEST_ID_SEQ
MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 20;
And my control-file looks like this:
load data
INFILE 'C:\Users\\test.csv'
into table TABLE_NAME
append
fields terminated by ','
OPTIONALLY ENCLOSED BY '"' AND '"'
trailing nullcols
(
TEST_COLUMN_ID "TEST_ID_SEQ.NEXTVAL",
INSERT_DATE EXPRESSION "current_timestamp(3)",
COLUMN_1 CHAR(4000),
COLUMN_2 CHAR(4000),
......
)
So during the import I just want to insert a generated number in the column "TEST_COLUMN_ID".
The problem now is that if i write this line in my control-file:
TEST_COLUMN_ID "TEST_ID_SEQ.NEXTVAL",
then the wother values wont be imported correctly. Which means, all the values will be shifted to the right. For example the content of the value in COLUMN_1 will be imported in COLUMN_2 and so on..when i delete the line than it works correctly.....can someone help me?
ok the solution is to put the SEQ_NUMBER field as the last field in the control
file. If someone is interested he can see this link for more details: http://www.mathcs.emory.edu/~cheung/Courses/377/Oracle/SQL-loader/FAQ.html

loading data in table using SQL Loader

I'm loading data into my table through SQL Loader
data loading is successful but i''m getting garbage(repetitive) value in a particular column for all rows
After inserting :
column TERM_AGREEMENT is getting value '806158336' for every record
My csv file contains atmost 3 digit data for that column,but i'm forced to set my column definition to Number(10).
LOAD DATA
infile '/ipoapplication/utl_file/LBR_HE_Mar16.csv'
REPLACE
INTO TABLE LOAN_BALANCE_MASTER_INT
fields terminated by ',' optionally enclosed by '"'
(
ACCOUNT_NO,
CUSTOMER_NAME,
LIMIT,
REGION,
**TERM_AGREEMENT INTEGER**
)
create table LOAN_BALANCE_MASTER_INT
(
ACCOUNT_NO NUMBER(30),
CUSTOMER_NAME VARCHAR2(70),
LIMIT NUMBER(30),
PRODUCT_DESC VARCHAR2(30),
SUBPRODUCT_CODE NUMBER,
ARREARS_INT NUMBER(20,2),
IRREGULARITY NUMBER(20,2),
PRINCIPLE_IRREGULARITY NUMBER(20,2),
**TERM_AGREEMENT NUMBER(10)**
)
INTEGER is for binary data type. If you're importing a csv file, I suppose the numbers are stored as plain text, so you should use INTEGER EXTERNAL. The EXTERNAL clause specifies character data that represents a number.
Edit:
The issue seems to be the termination character of the file. You should be able to solve this issue by editing the INFILE line this way:
INFILE'/ipoapplication/utl_file/LBR_HE_Mar16.csv' "STR X'5E204D'"
Where '5E204D' is the hexadecimal for '^ M'. To get the hexadecimal value you can use the following query:
SELECT utl_raw.cast_to_raw ('^ M') AS hexadecimal FROM dual;
Hope this helps.
I actually solved this issue on my own.
Firstly, thanks to #Gary_W AND #Alessandro for their inputs.Really appreciate your help guys,learned some new things in the process.
Here's the new fragment which worked and i got the correct data for the last column
LOAD DATA
infile '/ipoapplication/utl_file/LBR_HE_Mar16.csv'
REPLACE
INTO TABLE LOAN_BALANCE_MASTER_INT
fields terminated by ',' optionally enclosed by '"'
(
ACCOUNT_NO,
CUSTOMER_NAME,
LIMIT,
REGION,
**TERM_AGREEMENT INTEGER Terminated by Whitspace**
)
'Terminated by whitespace' - I went through some threads of SQL Loader and i used 'terminated by whitespace' in the last column of his ctl file. it worked ,this time i didn't even had to use 'INTEGER' or 'EXTERNAL' or EXPRESSION '..' for conversion.
Just one thing, now can you guys let me now what could possibly be creating issue ?what was there in my csv file in that column and how by adding this thing solved the issue ?
Thanks.

using multiple conditions in sql loader

I was trying to load records from a file to an oracle table based on conditions. Since OR operator and WHEN IN statements do not work in sql loader, I tried multiple insert to a table. However, only the records that match the first condition were loaded in the table and the records that matched the second condition were not loaded. My control file looks like below:
Options (BINDSIZE = 7340032)
Load Data
APPEND
INTO TABLE TEMP_GLOBAL_ONE_FEE_REBATE WHEN ACT_TYPE = 'SR'
FIELDS TERMINATED BY '|' TRAILING NULLCOLS
(
RPT_YEAR,
RPT_MONTH,
........
........
)
INTO TABLE TEMP_GLOBAL_ONE_FEE_REBATE WHEN ACT_TYPE = 'SL'
FIELDS TERMINATED BY '|' TRAILING NULLCOLS
(
RPT_YEAR,
RPT_MONTH,
........
........
)
** As mentioned, only those records with act_type = 'SR' were loaded and those records with act_type = 'SL' were not loaded.
Any idea how to go on this? Thank you.
Your problem is that the first INTO command reads the file from beginning to end, and then the second INTO command picks up where the first one finished - which is the end of the file in your case.
To achieve what you are trying to do, you're gonna have to use two seperate sql loader commands. See this post on AskTom for reference -
https://asktom.oracle.com/pls/apex/f?p=100:11:::YES:RP:P11_QUESTION_ID:3181887000346205200
A more elegant solution would be reading the data from the file using a pl/sql procedure and UTL_FILE package, but this is only worth the trouble if the import is something that happens a lot, and not a one time thing.
You should use POSITION(1) in the first column of each field list:
To force record scanning to start in a specific location, you use the POSITION parameter.
Control file
Options (BINDSIZE = 7340032)
Load Data
APPEND
INTO TABLE TEMP_GLOBAL_ONE_FEE_REBATE WHEN ACT_TYPE = 'SR'
FIELDS TERMINATED BY '|' TRAILING NULLCOLS
(
RPT_YEAR POSITION(1),
RPT_MONTH,
........
........
)
INTO TABLE TEMP_GLOBAL_ONE_FEE_REBATE WHEN ACT_TYPE = 'SL'
FIELDS TERMINATED BY '|' TRAILING NULLCOLS
(
RPT_YEAR POSITION(1),
RPT_MONTH,
........
........
)
Sample data
2015|01|SL
2015|02|SL
2015|03|SL
2015|03|SR
2015|04|SR
2015|04|XX
This will load 2 rows with 'SR', 3 rows with 'SL', and discard one row.
References
SQL*Loader with multiple WHENs is rejecting all rows, the Ask Tom queston mentioned in the accepted answer
Distinguishing Different Input Record Formats in SQL*Loader Control File
Loading Data into Multiple Tables in SQL*Loader Control File Reference

Loading text file in oracle from unix system

I am having text file contaning field in below manner.
"64252368","7489040","305762",
"64285217","12132108","787341",
I am using a below control file.
OPTIONS (SKIP=1)
LOAD DATA
TRUNCATE INTO TABLE test_table
FIELDS TERMINATED BY '",'
(
LEARNEVENT_ID,
ORGANIZATION,
COURSE_ID
)
But, I am getting the error:
Record 1: Rejected - Error on table test_table, column LEARNEVENT_ID
ORA-01722: invalid number
Kindly help me on it.
You need to change your ctl file to include OPTIONALLY ENCLOSED BY option.
OPTIONS (SKIP=1)
LOAD DATA
TRUNCATE INTO TABLE test_table
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
(
LEARNEVENT_ID,
ORGANIZATION,
COURSE_ID
)
I'd recommend reading up on SQL*Loader.
The problem lays with the encapsulation of the numbers with the quotes " " and your fields terminated by '",' simply does not strip the quotes.
Try this
OPTIONS(SKIP=1)
LOAD DATA
TRUNCATE INTO TABLE test_table
FIELDS TERMINATED BY ','
TRAILING NULLCOLS
(
LEARNEVENT_ID "replace ( :LEARNEVENT_ID ,'"', '')",
ORGINAZATION "replace ( :ORGINAZATION ,'"', '')",
COURSE_ID "replace ( :COURSE_ID ,'"', '')"
)

Resources