Skip to Main Content
  • Questions
  • SQL Loader to load multiple tables from CSV input file

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Pradeep.

Asked: December 08, 2016 - 12:12 pm UTC

Last updated: December 08, 2016 - 4:16 pm UTC

Version: 12.1

Viewed 1000+ times

You Asked

I have data file which has comma separated values :

col1,col2,col3,col4,Key1,Value1,Key2,Value2,Key3,Value3............

Requirement is to load col1-col4 inTab1(col1,col2,col3,col4)
AND second part of data is key/Value pair, number of key/value pair may vary across the data rows.

These key/value pair data I want to load in second table Tab2:
Row1 of Tab2 : Key1,Value1
Row3 of Tab2 : Key2,Value2
Row4 of Tab2 : Key3,Value3
Row5 of Tab2 : Key4,Value4

Until the end of row.

I did some research but could find a way to handle this load.

Appreciate your help !

and Chris said...

If you use an external table you can then use unpivot to convert your columns to rows.

For example, given the following file:

a,b,c,d,k1,v1,k2,v2
e,f,g,h,k1,v1,k2,v2,k3,v3,k4,v4


You can create an external table with the "missing field values are null" clause:

create table t (
  c1 varchar2(2),
  c2 varchar2(2),
  c3 varchar2(2),
  c4 varchar2(2),
  k1 varchar2(2),
  v1 varchar2(2),
  k2 varchar2(2),
  v2 varchar2(2),
  k3 varchar2(2),
  v3 varchar2(2),
  k4 varchar2(2),
  v4 varchar2(2)
) organization external (
  type oracle_loader
  default directory tmp
  access parameters
  (
    records delimited by newline
    fields terminated by ','
    missing field values are null (
      c1 , c2 , c3 , c4 ,
      k1 , v1 , k2 , v2 ,
      k3 , v3 , k4 , v4
    )
  ) 
  location ('test.csv')
);


Then use the unpivot operator to turn the key/value pairs into rows:

select * from t
unpivot ((k, v) for c in (
  (k1, v1) as 'R1',
  (k2, v2) as 'R2',
  (k3, v3) as 'R3',
  (k4, v4) as 'R4'
 )
);

C1 C2 C3 C4 C  K  V
-- -- -- -- -- -- --
a  b  c  d  R1 k1 v1
a  b  c  d  R2 k2 v2
e  f  g  h  R1 k1 v1
e  f  g  h  R2 k2 v2
e  f  g  h  R3 k3 v3
e  f  g  h  R4 k4 v4


For more on unpivoting, read:

https://blogs.oracle.com/sql/entry/how_to_convert_rows_to#unpivot

If you must use SQL*Loader for some reason, then load the CSV into a staging table first. Then you can use unpivot on this to load the data into the real table.

Rating

  (1 rating)

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

Comments

Thanks Chris

Pradeep Kumar, December 09, 2016 - 5:05 am UTC