using the control file below to load data
LOAD DATA
INFILE '/c/Transaction.txt'
INTO TABLE tab1 APPEND WHEN (1:1) = 'D'
(RUN_ID "RUN_ID_SEQ.NEXTVAL"
,RUN_DATE_TIME "SYSDATE"
)
INTO TABLE tab2 TRUNCATE WHEN(1:1) <> 'D'
FIELDS TERMINATED BY '|' TRAILING NULLCOLS
(
DEALER_NUMBER
,TRAN_CODE
,TRAN_AMOUNT "TO_NUMBER(:PL818_TRAN_AMOUNT,'999999.99')"
,TRAN_DATE "TO_DATE(:PL818_TRAN_DATE,'DD-MM-YYYY')"
)
This is the data set
DEALER_ID|TRAN_TYPE_CODE|TRAN_AMOUNT|TRAN_DATE
203113|34|1000.50|12-07-2022
No errors during load. Data loads correctly into the first table but first two characters are skipped when loading data into second table, table data looks like this. What could be causing this?
3113 34 1000.5 12-JUL-22
Skips "20"
Sample tables:
SQL> desc tab1
Name Null? Type
----------------------------------------- -------- ----------------------------
RUN_ID NUMBER
RUN_DATE_TIME DATE
SQL> desc tab2
Name Null? Type
----------------------------------------- -------- ----------------------------
DEALER_NUMBER NUMBER
TRAN_CODE NUMBER
TRAN_AMOUNT NUMBER
TRAN_DATE DATE
SQL>
Control file; note position(1) when loading into tab2. You must use it when loading data into different tables, using the same control file:
LOAD DATA
INFILE *
INTO TABLE tab1 APPEND WHEN (1:1) = 'D'
(RUN_ID "RUN_ID_SEQ.NEXTVAL"
,RUN_DATE_TIME "SYSDATE"
)
INTO TABLE tab2 TRUNCATE WHEN(1:1) <> 'D'
FIELDS TERMINATED BY '|' TRAILING NULLCOLS
(
DEALER_NUMBER position(1) --> here
,TRAN_CODE
,TRAN_AMOUNT "TO_NUMBER(:TRAN_AMOUNT,'999999.99')"
,TRAN_DATE "TO_DATE(:TRAN_DATE,'DD-MM-YYYY')"
)
BEGINDATA
203113|34|1000.50|12-07-2022
Loading session and the result:
SQL> $sqlldr scott/tiger#orcl control=test42.ctl log=test42.log
SQL*Loader: Release 18.0.0.0.0 - Production on Sri Srp 13 08:28:19 2022
Version 18.5.0.0.0
Copyright (c) 1982, 2018, Oracle and/or its affiliates. All rights reserved.
Path used: Conventional
Commit point reached - logical record count 1
Table TAB1:
0 Rows successfully loaded.
Table TAB2:
1 Row successfully loaded.
Check the log file:
test42.log
for more information about the load.
SQL> select * from tab2;
DEALER_NUMBER TRAN_CODE TRAN_AMOUNT TRAN_DATE
------------- ---------- ----------- ----------
203113 34 1000,5 12-07-2022
--
here's your missing "20"
SQL>
Related
I would like to load a CSV file into an Oracle table. The file have a column that can contain a very long string that will exceed the maximum length for VARCHAR2. Because of that, the table I'm loading into have this column specified as BLOB.
create table MY_TABLE(
COL1 VARCHAR2(100),
COL2 VARCHAR2(100),
COL3 BLOB
);
The CSV file to load look like:
COL1,COL2,COL3
1,qwerty,<very long block of text>
2,asdfgh,<very long block of text>
3,zxcvbn,<very long block of text>
...
How can I load it using SQL Loader? All the sites I could find describe how to use BLOB in order to load separate files, ie:
LOAD DATA
INFILE 'lob_test_data.txt'
INTO TABLE lob_tab
FIELDS TERMINATED BY ','
(number_content CHAR(10),
varchar2_content CHAR(100),
date_content DATE "DD-MON-YYYY" ":date_content",
clob_filename FILLER CHAR(100),
clob_content LOBFILE(clob_filename) TERMINATED BY EOF,
blob_filename FILLER CHAR(100),
blob_content LOBFILE(blob_filename) TERMINATED BY EOF)
Source: https://oracle-base.com/articles/10g/load-lob-data-using-sql-loader
How can I load into BLOB from my single CSV file? I would expect that below code would work, but it throws a syntax error:
load data
inflie '/path/to/file.csv'
into table MY_TABLE
fields terminated by ','
(
COL1 CHAR(100),
COL2 CHAR(100),
COL3 BLOB
)
In order to load a very long text, you should use CLOB instead of BLOB. The latter is for storing binary objects, such as video files, pictures or sound files.
The way to load depends whether the CLOB content is stored on your CSV file or you are using an external file locator.
CLOB INSIDE CSV FILE
Take this example. I create a csv example file with a very long text:
$ cat testclob.txt
1;aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
My control file looks like
load data
infile '/home/ftpcpl/testclob.txt'
into table test_perf.t
append
fields terminated by ';'
(
c1 integer ,
c2 char(10000)
)
Then I load the file
$ sqlldr test_perf/Oracle_1 control=/home/ftpcpl/testclob.ctl log=/home/ftpcpl/testclob.log
SQL*Loader: Release 12.2.0.1.0 - Production on Tue Aug 11 14:23:21 2020
Copyright (c) 1982, 2017, Oracle and/or its affiliates. All rights reserved.
Path used: Conventional
Commit point reached - logical record count 1
Table TEST_PERF.T:
1 Row successfully loaded.
Check the log file:
/home/ftpcpl/testclob.log
for more information about the load.
and the log file looks like
Table TEST_PERF.T:
1 Row 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
.
CLOB AS EXTERNAL FILE LOCATOR
If the CLOB is inside another file, then you should load the file as:
load data
infile *
replace
into table t
fields terminated by ',' optionally enclosed by '"' trailing nullcols
(
id integer external,
fname filler,
c LOBFILE(fname) TERMINATED BY EOF
)
begindata
1,"c:\temp\demo.out"
2,"c:\temp\load_emoji.sql"
SQL> create table t ( id int, c clob );
Table created.
$ sqlldr userid=scott/tigercontrol=c:\temp\c.ctl
SQL*Loader: Release 12.2.0.1.0 - Production on Fri Sep 8 15:49:55 2017
Copyright (c) 1982, 2017, Oracle and/or its affiliates. All rights reserved.
Path used: Conventional
Commit point reached - logical record count 1
Commit point reached - logical record count 2
Table T:
2 Rows successfully loaded.
Check the log file:
c.log
for more information about the load.
SQL> select id, length(c) from t;
ID LENGTH(C)
---------- ----------
1 17640
2 1418
I am loading CSV file data into table called EMPLOYEE using SQL*Loader.
My CSV file data is separated by pipe (|):
EMPID|EMPNAME_ADDRESS|SALARY|GRADE
123|Rams Hyd|1000|A1
124|Sand MUM|2,000|A2
125|"PRASANNA qwer trasf\"501 advv vvd, qee ggg\trfe \411005 THE|3,00,000|A3
and my control file is:
LOAD DATA
Insert INTO TABLE EMPLOYEE
Fields terminated by "|" Optionally enclosed by '"' TRAILING NULLCOLS
(
EMPID,
EMPNAME,
SALARY,
GRADE
)
When I load the data using above control file, the first two record are loading fine and for the third record i am getting error like as mentioned below.
no terminator found after TERMINATED and ENCLOSED field
Please suggest changes to be done to load the data properly.
Here's a way to load such data.
First, a target table (I hope it makes sense; you should have provided it):
SQL> create table employee
2 (empid number,
3 empname varchar2(200),
4 salary number,
5 grade varchar2(10)
6 );
Table created.
SQL>
Control file: note the SKIP option (which skips the header line), as well as REPLACE function call for the SALARY column (to remove superfluous commas which are formatting stuff; add them later, in the presentation layer).
options (skip = 1)
load data
infile *
replace
into table employee
fields terminated by "|" TRAILING NULLCOLS
(
empid,
empname,
salary "replace(:salary, ',', null)",
grade)
begindata
EMPID|EMPNAME_ADDRESS|SALARY|GRADE
123|Rams Hyd|1000|A1
124|Sand MUM|2,000|A2
125|"PRASANNA qwer trasf\"501 advv vvd, qee ggg\trfe \411005 THE|3,00,000|A3
Loading session:
SQL> $sqlldr scott/tiger control=test06.ctl log=test06.log
SQL*Loader: Release 11.2.0.2.0 - Production on Uto Tra 9 19:37:46 2019
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Commit point reached - logical record count 2
Commit point reached - logical record count 3
SQL> select * From employee;
EMPID EMPNAME SALARY GRADE
---------- ---------------------------------------- ---------- ----------
123 Rams Hyd 1000 A1
124 Sand MUM 2000 A2
125 "PRASANNA qwer trasf\"501 advv vvd, qee 300000 A3
ggg\trfe \411005 THE
SQL>
I have the following issue using SQL Loader to load a small table, my process populates a field which can be populated for possitive or negative decimal numbers, and the problem is that when the number is possitive, the process rounds with 2 positions, but if it's negative only use 1 position. I need 2 position, and this is the simple code:
UNRECOVERABLE LOAD DATA
TRUNCATE
INTO TABLE [SCHEMA].TABLE1
FIELDS TERMINATED BY '|'
OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(
FIELD1 "trim(:FIELD1 )"
)
Example:
Source: 12345,78 Target: 1234,78
Source: -12345,78 Target: -1234,8
Edit1: It just happend with number with 7 or more integer length (X < -1000000), but I can insert this numbers with regular insert instead of SQLLoader.
Edit2: I have noticed that it is not a problem with sql*loader because the .ctl is already wrong. This file is generated with python and SQL with this properties:
set colsep |
set headsep off
set pagesize 0
set trimspool on
set linesize 10000
set termout off
set feedback off
set arraysize 250
But I dont know where it is defined the lenght of each field, and this looks the error.
I'd say that you're not looking correctly. Data is, probably, stored OK, but you don't see it. Here's an example:
SQL> create table test (field1 number);
Table created.
SQL>
Control file:
load data
infile *
replace into table test
fields terminated by '|' trailing nullcols
(
field1
)
begindata
12345,78
-12345,78
987654321
-987654321
987654321,01234
-987654321,01234
111333444,887766
-112333444,887766
Loading session & the result:
SQL> $sqlldr scott/tiger#orcl control=test26.ctl log=test26.log
SQL*Loader: Release 11.2.0.2.0 - Production on Uto Kol 21 08:52:10 2018
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Commit point reached - logical record count 7
Commit point reached - logical record count 8
SQL> select * from test;
FIELD1
----------
12345,78
-12345,78
987654321
-987654321
987654321
-987654321
111333445
-112333445
8 rows selected.
SQL>
Ooops! Seems to be wrong, eh? But, if you fix the column format ...
SQL> col field1 format 999g999g999g990d000000
SQL> select * from test;
FIELD1
-----------------------
12.345,780000
-12.345,780000
987.654.321,000000
-987.654.321,000000
987.654.321,012340
-987.654.321,012340
111.333.444,887766
-112.333.444,887766
8 rows selected.
SQL>
See? Everything is here.
I guess that, if you used a GUI, you wouldn't have such problems. The former sentence doesn't mean that you should NOT use SQL*Plus - on the contrary; just ... learn how to use it properly :)
[EDIT, after reading the comment]
It is SET NUMFORMAT you might be looking for:
SQL> select -12334.789123 from dual;
-12334.789123
-------------
-12334,789
SQL> set numformat 999g999g990d000000
SQL> select -12334.789123 from dual;
-12334.789123
-------------------
-12.334,789123
SQL>
I am running the following to load data into a table
OPTIONS (Skip=1)
LOAD DATA
INFILE 'D:\EPM\import\test.txt'
APPEND
INTO TABLE HYP.HS_MEMBER_D
FIELDS TERMINATED BY "|"
TRAILING NULLCOLS
(
DIMENSION,
PARENT,
CHILD,
ALIAS,
ATTRB01
)
This is working fine but I want to keep the ATTRB01 field as a static value, I want to load "Alloc" for all records, is there a way in this script to load a static value even though I am loading from the file for the other fields?
You'd load a constant, such as
OPTIONS (Skip=1)
LOAD DATA
INFILE 'D:\EPM\import\test.txt'
APPEND
INTO TABLE HYP.HS_MEMBER_D
FIELDS TERMINATED BY "|"
TRAILING NULLCOLS
(
DIMENSION constant Account,
PARENT,
CHILD,
ALIAS,
ATTRB01 constant 'Alloc' --> this
)
Here's an example: test table:
SQL> desc test
Name Null? Type
----------------------------------------- -------- -------------
ID NUMBER
ATTRB01 VARCHAR2(20)
Control file:
load data
infile *
replace
into table test
fields terminated by ","
trailing nullcols
(
id,
attrb01 constant 'Alloc'
)
begindata
1,xxx
2,yyy
3,zzz
Loading session & the result:
SQL> $sqlldr scott/tiger control=test01.ctl log=test01.log
SQL*Loader: Release 11.2.0.2.0 - Production on Sri Kol 15 21:08:59 2018
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Commit point reached - logical record count 2
Commit point reached - logical record count 3
SQL> select * From test;
ID ATTRB01
---------- --------------------
1 Alloc
2 Alloc
3 Alloc
try by using set at end
OPTIONS (Skip=1)
LOAD DATA
INFILE 'D:\EPM\import\test.txt'
APPEND
INTO TABLE HYP.HS_MEMBER_D
FIELDS TERMINATED BY "|"
TRAILING NULLCOLS
(
DIMENSION constant Account,
PARENT,
CHILD,
ALIAS,
ATTRB01
)
set ATTRB01='default_value'
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.