Search code examples
oracle-databaseoracle12cexternal-tables

External table truncates trailing whitespace while reading from file


I'm trying to load the file contents to an external table. While doing this, trailing spaces are truncated.

'CREATE TABLE ' || rec.ext_table_name || ' (ROW_DATA VARCHAR2(4000)) ORGANIZATION EXTERNAL ' ||
     '(TYPE ORACLE_LOADER DEFAULT DIRECTORY ' || rec.dir_name || ' ACCESS ' || 'PARAMETERS (RECORDS ' ||
     'DELIMITED by NEWLINE NOBADFILE NODISCARDFILE ' ||
     'FIELDS REJECT ROWS WITH ALL NULL FIELDS (ROW_DATA POSITION(1:4000) char)) LOCATION (' || l_quote ||
     'temp.txt' || l_quote || ')) REJECT LIMIT UNLIMITED'

For example, with B representing a blank space, my file has:

Line1sometextBBBBBBB

My external table reads that as just:

Line1sometext

I want the file to be loaded with blanks too. How can I stop it removing the trailing whitespace?

Current Problem is :

if file has:

"this is a test value with

"

 ^   this new line is also a part of the row_data.

the problem


Solution

  • Add a trim_spec clause, specifically here NOTRIM, to change the default field trimming behaviour:

    ... FIELDS REJECT ROWS WITH ALL NULL FIELDS (ROW_DATA POSITION(1:4000) char NOTRIM)) LOCATION (...
                                                                                ^^^^^^
    

    Quick demo:

    create table t42_ext (
      row_data varchar2(4000)
    )
    organization external
    (
      type oracle_loader default directory mydir access parameters
      (
        records delimited by newline nobadfile nodiscardfile
        fields reject rows with all null fields
        (
          row_data position(1:4000) char notrim
        )
      )
      location ('temp.txt')
    )
    reject limit unlimited;
    
    Table T42_EXT created.
    
    select '<'|| row_data ||'>' from t42_ext;
    
    '<'||ROW_DATA||'>'                                                             
    --------------------------------------------------------------------------------
    <Line1sometext       >