Skip to Main Content
  • Questions
  • SQLLDR with multiple when conditions

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Ankit.

Asked: September 03, 2018 - 7:28 pm UTC

Last updated: September 06, 2018 - 1:05 am UTC

Version: 12.1.0.1

Viewed 10K+ times! This question is

You Asked

Hello,

we have a requirement to load multiple data based on different conditions. The details are as below,

Create:

create table table_a (empno number, empname varchar2(50),salary number, status varchar2(30))
/

Control file:
options  ( skip=1 )
LOAD DATA
INFILE "C:\Users\data.txt"
APPEND 
INTO TABLE table_a WHEN Grade = 'Clerk' 
FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(empno,
empname,
salary,
grade
)
INTO TABLE table_a WHEN Grade = 'Peon' 
FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(empno,
empname,
salary,
grade
)
INTO TABLE table_a WHEN Grade = 'Officer' 
FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(empno,
empname,
salary,
grade
)
INTO TABLE table_a WHEN Grade = 'Manager' 
FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(empno,
empname,
salary,
grade
)

Data file:
"empno"|"empname"|"salary"|"grade"
"1"|"ABC1"|1000|"Clerk"
"2"|"ABC2"|1000|"Clerk"
"3"|"ABC3"|500|"Peon"
"4"|"ABC4"|500|"Peon"
"5"|"ABC5"|2000|"Officer"
"6"|"ABC6"|2000|"Officer"
"7"|"ABC7"|2000|"Officer"
"8"|"ABC8"|2000|"Officer"
"9"|"ABC9"|2000|"Officer"
"10"|"ABC10"|5000|"Manager"


While running sqlldr command, getting below error,

Record 10: Discarded - failed all WHEN clauses.

please suggest how to implement multiple when conditions in 1 control file.

Thanks

and Connor said...

I assume GRADE and STATUS are synonymous here :-)

See our previous answer

https://asktom.oracle.com/pls/apex/asktom.search?tag=sqlloader-with-multiple-whens-is-rejecting-all-rows

for why that is the case. Although the doc link in that answer is outdated, it is now:

https://docs.oracle.com/cd/E11882_01/server.112/e22490/ldr_control_file.htm#SUTIL1124

Adding "position(1)" after each empno should fix you up. Here's my sqlldr output when I did that:

SQL*Loader: Release 12.2.0.1.0 - Production on Tue Sep 4 14:22:19 2018

Copyright (c) 1982, 2018, Oracle and/or its affiliates.  All rights reserved.

Path used:      Conventional
Commit point reached - logical record count 9
Commit point reached - logical record count 10

Table TABLE_A:
  2 Rows successfully loaded.

Table TABLE_A:
  2 Rows successfully loaded.

Table TABLE_A:
  5 Rows successfully loaded.

Table TABLE_A:
  1 Row successfully loaded.


Rating

  (2 ratings)

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

Comments

continuation with previous post

Ankit Bhatt, September 04, 2018 - 10:56 am UTC

Very nice answer but just wanted to know that how to set the position in control file for each and every columns.

could you please elaborate more on it?
Connor McDonald
September 05, 2018 - 4:57 am UTC

Well....it is in the links I mentioned.

But your control file will look like this:

LOAD DATA
INFILE "C:\Users\data.txt"
APPEND 
INTO TABLE table_a WHEN Grade = 'Clerk' 
FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(empno postition(1),      <=============
empname,
salary,
grade
)
INTO TABLE table_a WHEN Grade = 'Peon' 
FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(empno postition(1),      <=============
empname,
salary,
grade
)


etc etc

Ankit Bhatt, September 05, 2018 - 5:41 am UTC

Thanks a lot Connor!!! it perfectly worked.

Appreciate your help.
Connor McDonald
September 06, 2018 - 1:05 am UTC

glad we could help

More to Explore

Utilities

All of the database utilities are explained in the Utilities guide.