Oracle PL-SQL : Import multiple delimited files into table

一笑奈何 提交于 2019-12-07 03:25:27

Use SQL*Loader to get the files into a table. Assuming you have a table created a bit like:

create table FLOG
(
  FILENAME   varchar2(1000)
 ,NUM        varchar2(1000)
 ,DATA       varchar2(1000)
);

Then you can use the following control file:

LOAD DATA
INFILE 'f1.log' "str ';'"
truncate INTO TABLE flog
fields terminated by '=' TRAILING NULLCOLS
(
   filename constant 'f1'
   ,num  char 
   ,data char 
)

However, you will need a different control file for each file. This can be done by making the control file dynamically using a shell script. A sample shell script can be:

cat >flog.ctl <<_EOF
LOAD DATA
INFILE '$1.log' "str ';'"
APPEND INTO TABLE flog
fields terminated by '=' TRAILING NULLCOLS
(
filename constant '$1'
,num  char
,data char
)
_EOF

sqlldr <username>/<password>@<instance> control=flog.ctl data=$1.log

Saved as flog.sh it can then be run like:

./flog.sh f1
./flog.sh f2
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!