Loading text file in oracle from unix system - oracle

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 ,'"', '')"
)

Related

Can we change Case of input data in SQL LOADER ctl file?

I am trying to insert data from csv to a table using sql loader.
My csv has data in small Caps .But I want the data in the table to be in Large cap for some columns For some Columns it should be InitCap.
INFILE 'abc.csv'
INSERT INTO TABLE T_DETAILS
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' TRAILING NULLCOLS
(
INITCAP(f_name),
UPPER(f_code)```
But i am getting error like
> **SQL*Loader-350: Syntax error at line 16. Expecting "," or ")", found "(".
> INITCAP(f_name),**
> ^
You can, but not like that. Should've been
load data
infile 'abc.csv'
insert
into table t_details
fields terminated by ',' optionally enclosed by '"' trailing nullcols
(f_name "initcap(:f_name)",
f_code "upper(:f_code)"
)

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.

SQL Loader - Use Replace and Append in same control file

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

SQL Loader - How do I escape an terminating character

Friends,
I have had a client data sent to me in that is requested to have fields terminated by '|'
Problem is, some of the filed values also have the "|" as a character that we need to preserve.
Problem rows in my datafile look like this (the '|' in the addres "1|34-36 ..." is the issue)
ID | Address |UPDATEDATE
1423 | 1|34-36 White Street |02/01/199
my .ctl looks like this
options(errors=1000)
load data
into table client_address APPEND
fields terminated by '|'
TRAILING NULLCOLS
(
ID INTEGER EXTERNAL,
If all fields in data file are width-aligned, you could use this .ctl file:
load data
into table client_address APPEND
fields terminated by '~'
TRAILING NULLCOLS
(
line BOUNDFILLER,
ID "to_number(trim(substr(:line,1,8)))",
Address "trim(substr(:line,10,26))",
UPDATEDATE "to_date(trim(substr(:line,37)),'mm/dd/yyyy')"
)
EDIT :
If fields are not width-aligned, but Address is the only field which can contain '|' char inside, then use this .ctl file
load data
into table client_address APPEND
fields terminated by '~'
TRAILING NULLCOLS
(
line BOUNDFILLER,
ID "to_number(trim(regexp_substr(:line,'^[^|]*')))",
Address "trim(regexp_replace(:line,'^[^|]*\|(.*)\|[^|]*$','\1'))",
UPDATEDATE "to_date(trim(regexp_substr(:line,'[^|]*$')),'mm/dd/yyyy')"
)

Oracle SQLLDR Discard rows using WHEN clause not working

I am trying to load a text file, only those rows where 3rd column 'c_nbr' Doesn't END with 'ABCD'. Since WHEN clause is really primitive I could not use, trim/substring with it.
My second choice was to put the last 4 chars of c_nbr field to a column called 'MSGCOL' and use that column in my WHEN clause, like " WHEN MSGCOL <> 'ABCD' ".
The MSGCOL is getting value "ABCD" where column 'c_nbr' ends with "ABCD", but the WHEN clause is not DISCARDing them.
Why it's not working? How can I achieve my goal? Can I run SQL Query from sqlldr control file itself? Like "Delete from tbl_load where c_nbr like '%ABCD';" at the end of loading commands?
SQLLDR Control File:-
OPTIONS (ERRORS=9999)
LOAD DATA
INFILE '052140.csv'
BADFILE '052140.BAD'
DISCARDFILE '052140.DIS'
INFILE '055913.csv'
BADFILE '055913.BAD'
DISCARDFILE '055913.DIS'
APPEND INTO TABLE tbl_load
WHEN MSGCOL <> 'ABCD'
FIELDS TERMINATED BY ',' optionally enclosed by '"' trailing nullcols
(
id "TRIM(UPPER(:ID))",
pid,
c_nbr "TRIM(:c_nbr)",
a_nbr "SUBSTR(TRIM(:a_nbr), 1, 25)",
P_REASON2 FILLER,
MSGCOL EXPRESSION "substr(trim(:c_nbr), length(trim(:c_nbr))-3, 4)"
)

Resources