Skip to Main Content
  • Questions
  • Oracle SQL Loader - How to do field count check and reject records?

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Saravanakumar.

Asked: May 14, 2016 - 5:00 pm UTC

Last updated: May 15, 2016 - 3:47 am UTC

Version: 11.2.0

Viewed 1000+ times

You Asked

Here is the

Oracle Table Structure.
create table EmployeeTemp
(
Name VARCHAR2(80) NOT NULL,
Age NUMBER(3),
Gender VARCHAR2(10)
);

SQL Loader Control File
LOAD DATA
REPLACE
INTO TABLE EmployeeTemp
FIELDS TERMINATED BY "|" OPTIONALLY ENCLOSED BY '"' TRAILING NULLCOLS
(
Name CHAR(80),
Age INTEGER EXTERNAL,
Gender CHAR(10)
)

Data File:
John Williams|23|Male
Caser|78|
Brit
Sam|34||extrafield

How to reject a record which has has more fields or fewer fields in data file compared to the number of fields in Database table?

In the data file example,
a) Record 'Sam' need to be rejected as it has 4th field (Extra field ).
b) Record 'Brit' need to be rejected as it has not passed null or empty value for Gender field. ( Having fewer field information)
c) 'John Williams' & 'Caser' record has either data or null/empty values for all 3 fields is provided. So they should not be rejected.

Please suggest how to achieve my requirement and what changes needed in the Oracle SQL loader Control file ?

and Connor said...

I'd be inclined to use an external table for this


SQL> drop table ext_emp;

Table dropped.

SQL>
SQL> CREATE TABLE ext_emp
  2  (
  3    "NAME" VARCHAR2(80),
  4    "AGE" NUMBER(3),
  5    "GENDER" VARCHAR2(10),
  6    "X" VARCHAR2(100)
  7  )
  8  ORGANIZATION external
  9  (
 10    TYPE oracle_loader
 11    DEFAULT DIRECTORY TEMP
 12    ACCESS PARAMETERS
 13    (
 14      RECORDS DELIMITED BY NEWLINE CHARACTERSET WE8MSWIN1252
 15      BADFILE 'TEMP':'e.bad'
 16      LOGFILE 'TEMP':'e.log'
 17      READSIZE 1048576
 18      FIELDS TERMINATED BY "|" OPTIONALLY ENCLOSED BY '"' LDRTRIM
 19      MISSING FIELD VALUES ARE NULL
 20      REJECT ROWS WITH ALL NULL FIELDS
 21      (
 22        "NAME" CHAR(80) TERMINATED BY "|" OPTIONALLY ENCLOSED BY '"',
 23        "AGE" CHAR(255) TERMINATED BY "|" OPTIONALLY ENCLOSED BY '"',
 24        "GENDER" CHAR(10) TERMINATED BY "|" OPTIONALLY ENCLOSED BY '"',
 25        "X" CHAR(100) TERMINATED BY "|" OPTIONALLY ENCLOSED BY '"'
 26      )
 27    )
 28    location
 29    (
 30      'e.dat'
 31    )
 32  )REJECT LIMIT UNLIMITED
 33  /

Table created.

SQL>
SQL> INSERT INTO EMPLOYEETEMP
  2  (
  3    NAME,
  4    AGE,
  5    GENDER
  6  )
  7  SELECT
  8    "NAME",
  9    "AGE",
 10    "GENDER"
 11  FROM ext_emp
 12  where X is null
 13  and gender is not null;

1 row created.

SQL>
SQL>
SQL> select * from  EMPLOYEETEMP;

NAME                                                                                    AGE GENDER
-------------------------------------------------------------------------------- ---------- ----------
John Williams                                                                            23 Male

SQL>



Rating

  (2 ratings)

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

Comments

A reader, May 17, 2016 - 12:06 am UTC


Thanks

Saravanakumar Thirunavukkarasu, May 25, 2016 - 6:18 am UTC

I think the given solution works for my request. Thought its we adding additional external table, it works fine.