I have a sample csv file that contains data that i want to load in oracle table. The sample data is like
1,aa,b,c
2,x,yy,zzz
1,aa,b,c
2,x,yy,zzz
These are two different records distinguished by first character as '1' and '2', for which I am having a data table in db, that contains the columns of first record and then columns of second record. I tried to load the data using the 'WHEN' clause, but the problem is it don't load data sequentially. It load data for '1' first and then for '2'. like
ID col1 col2 col3 col4 col5 col6
1 aa b c null null null
1 aa b c null null null
2 null null null x yy zzz
2 null null null x yy zzz
Here's the loader code:
load data
infile 'C:\sample.csv'
APPEND
INTO TABLE "temp"
WHEN "ID" = '1'
fields terminated by "," optionally enclosed by '"' trailing nullcols
(
Col1,
Col2,
Col3
)
INTO TABLE "temp"
WHEN "ID" = '2'
fields terminated by "," optionally enclosed by '"' trailing nullcols
(
Col4,
Col5,
Col6
)
I just want to load the data as:
ID col1 col2 col3 col4 col5 col6
1 aa b c null null null
2 null null null x yy zzz
1 aa b c null null null
2 null null null x yy zzz
Any help would be appreciated.
You could load it into a temporary table adding a column that is a sequence number. Then you load it into another table splitting up the columns as you want.
This is not tested but to give you a rough idea:
create or replace tmpsequence;
load data
infile 'C:\sample.csv'
APPEND
INTO TABLE "temp"
fields terminated by "," optionally enclosed by '"' trailing nullcols
(
rec_id "tempsequence.nextval",
id,
Col1,
Col2,
Col3
)
create table temp2
select rec_id, id, case when id = "1" then col1 else null end
, case when id = "1" then col2 else null end
, case when id = "1" then col3 else null end
, case when id = "2" then col1 else null end
, case when id = "2" then col2 else null end
, case when id = "2" then col3 else null end
from temp
Then you should be able to see what you want with
select * from temp2 order by rec_id
Use "CONTINUEIF" to assemble logical records from multi-line records in your data file. This tells SQL*Loader to keep going if a condition is matched. Here, it says to continue if the first position on the next line is a '2'. The control file is structured to not load the record_part number:
LOAD DATA
infile test.dat
truncate
continueif NEXT (1:1) = '2'
INTO TABLE X_test
fields terminated by ',' TRAILING NULLCOLS
(rec_part FILLER
,col1
,col2
,col3
,col4
,col5
,col6
)
After loading:
If the data will ALWAYS be on two lines, replace this line:
continueif NEXT (1:1) = '2'
with
concatenate 2
which says each logical record is always 2 rows in the data.
If you are loading data through sqlloader command, then try adding the parameter rows = 1. This will force sqlloader to insert each row of data in the table thus making the file load sequential. By default the row count is 64 which is why you are not getting a sequential load for a small file .
Hope this helps..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.