Oracle SQLLDR Discard rows using WHEN clause not working - oracle

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

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

Why I can't use sign '<' or '>' in when clause?

I use sql loader for load data in my database. For example, I want load records only from certain range, but I get this message:
My control file:
LOAD DATA
INFILE 'C:\sql\empl.dat'
BADFILE 'C:\sql\empl.bad'
DISCARDFILE 'C:\sql\empl.dat.dsc'
INTO TABLE LOAD_EMP_N
WHEN empno<'5600'
FIELDS TERMINATED BY "|"
(empno,ename CHAR, job CHAR,mgr,
hiredate DATE "DD-MM-YYYY", sal,comm,
deptno)
If I change when clause
WHEN empnŠ¾='5600' -- all work!!
I can't understand what is the problem?
Quote from the SQL*Loader manual
A field condition is a statement about a field in a logical record that evaluates as true or false. It is used in the WHEN, NULLIF, and DEFAULTIF clauses.
...
operator A comparison operator for either equal or not equal.
So in other words: the when condition in a SQL*Loader control files only supports = or <>

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

Hive external table on data containing newline

I have a few txt files on which I want to create an external table.
Unfortunately, the content of the files also contains the string "\n" from time to time. It seems that Hive interprets this as a newline, even though it's not a newline in the original file and is just part of the text.
Can I catch this problem in Hive without having to alter the original txt files?
You can put any other delimiter at end of each line(other than \n and your field separator).And than can register that delimiter in table properties.
Eg: Let's say I have record like this
1,2,3,aniit\n,4\n
In this record aniit\n is a string and \n is string.So hive makes it two record.To avoid this ,you can add any other delimiter at end.Like
1,2,3,aniit\n,4\n||
Here '||' is Line delimiter and my create table will look like :
create external table if not exists table1
(
col1 int,
col2 int,
col3 int,
col4 string,
col5 string
)
row format delimited fields terminated by ','
lines terminated by '||'
stored as textfile
location '/tmp/table1';

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