sql loader : Load string greater than 4000 characters using sql loader - oracle

I need to load a CSV file into table using sql loader but the lenth of a column is exceeding the Max length of varchar2 data type.
Create table statement :
CREATE TABLE TEST_PIPE_SEP (FILE_NM VARCHAR2(3000), KEY_COL VARCHAr2(4000), DESCR VARCHAR2(4000), RUN_DATE DATE );
CTL file :
load data
into table test_pipe_sep
append
fields terminated by ','
TRAILING NULLCOLS
(
FILE_NM char(4000) "trim(:FILE_NM)",
KEY_COL char(4000) "trim(:KEY_COL)",
DESCR "trim(:DESCR)",
RUN_DATE "to_date(sysdate, 'dd-mon-yyyy hh24:mi:ss')"
)
CSV sample record :
sample_file_name.csv,"B"|"STRESS_TESTING_SCENARIO_ID"|"TRANCHE_COLLATERAL_TYPE"|"TRANCHE_GUARANTEE_TYPE"|"BS_TYPE"|"CONTRACT_REFERENCE"|"CONTRACT_TYPE"|,Not Present in file2
I just not paste the full text here as it will become lengthy but you can append the length of middle column in CSV or KEY_COL field value to more than 4000 for testing.
SQLLDR :
sqlldr userid=$SQL_CREDENTIALS control=new_test_3.ctl data=data/$filename log=logs/$filename.log bad=logs/$filename.bad skip=1
Please suggest if there is any way where we can load a string of length more than 4000 using SQL LOADER or is there any workaround for this kind of problem.
I am on :
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

I did it ! :)
Change the column data type to CLOB as below :
CREATE TABLE TEST_PIPE_SEP (FILE_NM VARCHAR2(3000), KEY_COL CLOB, DESCR VARCHAR2(4000), RUN_DATE DATE );
Then the CTL I used to load data into CLOB column is as below :
load data
into table test_pipe_sep
append
fields terminated by ','
TRAILING NULLCOLS
(
FILE_NM char(4000) "trim(:FILE_NM)",
KEY_COL CHAR(30000) optionally ENCLOSED BY '<' AND '>',
DESCR "trim(:DESCR)",
RUN_DATE "to_date(sysdate, 'dd-mon-yyyy hh24:mi:ss')"
)

Related

Oracle SQL loader control file creation for date and time

I am using below CTL file to load data into table
Load data
Append
Into table abc
Fields terminated by ',' optionally enclosed by '"'
Trailing nullcols
(
R_date date 'mm/dd/yyyy hh:mm:ss'
)
Csv file value is as
R_date
09/12/2023 12:30:34
08/11/2023 22;30:45
In table abc r_date column datatype is date.
Ora-01840 input value not long enough for date format.
Noting we have written in above file
I think you want:
R_date date "mm/dd/yyyy hh24:mi:ss"

How to load string into a clob column using SQL Loader?

See table and control file in the bottom. It works if my LOAD_CLOB column data type is varchar2 instead of clob. If the column is clob datatype, it throws an error.
SQL*Loader-309: No SQL string allowed as part of HEBE_AHRA_DETAILS field specification
But I need the datatype to be clob because the string will be long.
Create table Table_load
(Row_id varchar2(20),
Load_clob clob);
Control File
OPTIONS (SKIP=1, DIRECT=true)
UNRECOVERABLE
load data
infile 'LOAD.txt'
badfile 'BAD.bad'
discardfile 'Discard.dsc'
replace
into table Table_load
fields terminated by "|" TRAILING NULLCOLS
( ROW_ID ":ROW_ID",
LOAD_CLOB "'Current Row ID '||:ROW_ID"
)

Wait...are types NUMBER and VARCHAR2 no longer allowed in sqlldr?

Bizarre syntax error:
LOAD DATA
INFILE z.dat
INTO TABLE table1
REPLACE
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(
COL1 NUMBER(10),
COL2
)
Is yielding:
Expecting "," or ")", found "NUMBER".
COL1 NUMBER(10),
^
Works with type DECIMAL. Backend - Oracle12c. OS - RHEL.
Did I miss the memo?
SQL*Loader: Release 12.2.0.1.0
Even more: would not allow type VARCHAR2, but VARCHAR is ok. Has anyone encountered this behavior/bug?
This comes through:
LOAD DATA
INFILE 'z.dat'
INTO TABLE table1
(
col1 VARCHAR(10),
col2 INTEGER
)
This - does not:
LOAD DATA
INFILE 'z.dat'
INTO TABLE table1
(
col1 VARCHAR2(10),
col2 INTEGER
)
SQL*Loader-350: Syntax error at line 7.
Expecting valid column specification, "," or ")", found "VARCHAR2".
col1 VARCHAR2(10),
^
The funkiest fact is that I was even using an example from Oracle's docs (albeit 10g), and it still won't get through.
Did you ever manage to use those datatypes?
Generally, we've used
INTEGER EXTERNAL for numbers
CHAR for strings
Besides, that's what documentation in SQL Loader Field List Reference suggests; have a look at list of available datatypes ((https://docs.oracle.com/en/database/oracle/oracle-database/12.2/sutil/oracle-sql-loader-field-list-contents.html#GUID-DB309002-461D-42F7-8C94-727B32FA8B85) and you'll see that there are no NUMBER nor VARCHAR2 there.
It seems you misinterpreted reality :)

sqlloader with constraint NOT NULL in oracle table

I'm entering values from a CSV file to an Oracle table using a SQL*Loader script. In this table there are fields with NOT NULL constraints. In my CSV file the corresponding field is "" and I would like put a blank string into the Oracle table when that happens.
This is my control file:
LOAD DATA
infile 'F:\tar.csv'
REPLACE
INTO TABLE tar
fields terminated by ',' optionally enclosed by '"'
TRAILING NULLCOLS
(IDTAR ,
DATABACKUP DATE "YYYY-MM-DD",
PAESE ,
R_ELEM NULLIF (R_ELEM=BLANKS))
and this is the error in the log file:
ORA-01400: cannot insert NULL into ("MY_SCHEMA"."TAR"."PAESE")
How can I avoid the error by supplying a different value?
You can apply an SQL operator, such as NVL(:PAESE, 'XXX'). Notice the colon before the reference to the field name. In situ:
LOAD DATA
infile 'gian.csv'
REPLACE
INTO TABLE tar
fields terminated by ',' optionally enclosed by '"'
TRAILING NULLCOLS
(
IDTAR,
DATABACKUP DATE "YYYY-MM-DD",
PAESE "NVL(:PAESE, 'XXX')",
R_ELEM NULLIF (R_ELEM=BLANKS)
)
With a dummy table:
create table tar (
idtar number,
databackup date,
paese varchar2(10) not null,
r_elem varchar2(10)
);
and CSV, where the 3rd and 4th lines have trailing spaces for the nullif() clause:
1,2017-08-01,A,B
2,2017-08-02,C,
3,2017-08-03,,
4,2017-08-04,"",
then running with that control file gets:
SQL*Loader: Release 11.2.0.4.0 - Production on Fri Aug 4 19:39:23 2017
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
Commit point reached - logical record count 4
and the log says:
...
Column Name Position Len Term Encl Datatype
------------------------------ ---------- ----- ---- ---- ---------------------
IDTAR FIRST * , O(") CHARACTER
DATABACKUP NEXT * , O(") DATE YYYY-MM-DD
PAESE NEXT * , O(") CHARACTER
SQL string for column : "NVL(:PAESE, 'XXX')"
R_ELEM NEXT * , O(") CHARACTER
NULL if R_ELEM = BLANKS
Table TAR:
4 Rows successfully loaded.
0 Rows not loaded due to data errors.
0 Rows not loaded because all WHEN clauses were failed.
0 Rows not loaded because all fields were null.
...
Querying the table shows all four rows were loaded:
set null "<null>"
select * from tar;
IDTAR DATABACKU PAESE R_ELEM
---------- --------- ---------- ----------
1 01-AUG-17 A B
2 02-AUG-17 C <null>
3 03-AUG-17 XXX <null>
4 04-AUG-17 XXX <null>
Obviously replace 'XXX' with the actual default string you want to use. You said 'a blank string', so you could use "NVL(:PAESE, ' ')" to insert a single space character for instance. You can't use an empty string though, as that is the same as null as far as Oracle is concerned.

load decimal into oracle table with sql loader

I have this table in oracle :
CREATE TABLE mytable
(
TSTAMP Date,
prmc1 VARCHAR2(30),
prmc2 VARCHAR2(30),
prmc3 VARCHAR2(30),
prmc4 VARCHAR2(30),
prmc5 NUMBER,
prmc5 NUMBER,
prmc6 NUMBER
)
the control file is below :
load data
append
into table mytable
fields terminated by ',' TRAILING NULLCOLS
( tstamp DATE "YYYY-MM-DD HH24:MI" TERMINATED BY ",",
prmc1 ":prmc1",
prmc2 ":prmc2",
prmc3 ":prmc3",
prmc4 ":prmc4",
prmc5 INTEGER ":prmc5",
prmc6 INTEGER ":prmc6"
)
the value of the column prmc5 in the csv file is -106.436
how do i load this into the table?
You just need to specify it as DECIMAL EXTERNAL
I am also removing specifying the format as the column itself.
load data
append
into table mytable
fields terminated by ',' TRAILING NULLCOLS
( tstamp DATE "YYYY-MM-DD HH24:MI" TERMINATED BY ",",
prmc1 ,
prmc2 ,
prmc3 ,
prmc4 ,
prmc5 DECIMAL EXTERNAL,
prmc6 DECIMAL EXTERNAL
)
More details with some Example here

Resources