如何使用 SQLLDR 基于标头数据加载 CSV 文件



>我有一个.csv文件,其中第一行总是包含header信息。但是标题列的位置不是固定的。例如

file 1 has below mentioned data

Name,ID,Mailing Details

X,1,US

file 2 has below mentioned data

Mailing Details,Name,ID

UK,Y,10

在这里,如果我们看到列名是互换的,因此我无法定义静态控制文件。 所以任何人都可以帮助我,比如如何编写我的控制文件来搜索标题列名称并将其放入表中。

my table structure is

ID,Name,Mailing_Details

非常感谢。

假设这 3 列和 6 种组合;shell 脚本中的 elsif 梯子可用于构建此处的询问

希望这将有助于达到您想要的要求

for file in 1.txt
do
f1=`head "${file}" -n1 | tr A-Z a-z | sed 's/,//g'`

if [ $f1 == "idnamemailing_details" ] 
then
echo "logic 1 -> ${file}"
echo "
load data 
infile '${file}' 
badfile 'badfile1.csv'
discardfile 'discard1.csv' 
append
into table schema.table
fields terminated by ',' 
( id, name, mailing_details ) " > loader.ctl
sleep 1
sqlldr user/pwd@schema  control=loader.ctl 
break
elif [ $f1 == "idmailing_detailsname" ] 
then
echo "logic 2 -> ${file}"
echo "
load data 
infile '${file}' 
badfile 'badfile1.csv'
discardfile 'discard1.csv' 
append
into table schema.table
fields terminated by ',' 
( id, mailing_details,name ) " > loader.ctl
sleep 1
sqlldr user/pwd@schema  control=loader.ctl 
break
elif [ $f1 == "namemailing_detailsid" ] 
then
echo "logic 3 -> ${file}"
echo "
load data 
infile '${file}' 
badfile 'badfile1.csv'
discardfile 'discard1.csv' 
append
into table schema.table
fields terminated by ',' 
( name, mailing_details ,id) " > loader.ctl
sleep 1
sqlldr user/pwd@schema  control=loader.ctl 
break
elif [ $f1 == "nameidmailing_details" ] 
then
echo "logic 4 -> ${file}"
echo "
load data 
infile '${file}' 
badfile 'badfile1.csv'
discardfile 'discard1.csv' 
append
into table schema.table
fields terminated by ',' 
( name, id, mailing_details ) " > loader.ctl
sleep 1
sqlldr user/pwd@schema  control=loader.ctl 
break
elif [ $f1 == "mailing_detailsidname" ] 
then
echo "logic 5 -> ${file}"
echo "
load data 
infile '${file}' 
badfile 'badfile1.csv'
discardfile 'discard1.csv' 
append
into table schema.table
fields terminated by ',' 
( mailing_details,id,name ) " > loader.ctl
sleep 1
sqlldr user/pwd@schema  control=loader.ctl 
break
elif [ $f1 == "mailing_detailsnameid" ] 
then
echo "logic 6 -> ${file}"
echo "
load data 
infile '${file}' 
badfile 'badfile1.csv'
discardfile 'discard1.csv' 
append
into table schema.table
fields terminated by ',' 
( mailing_details, name,id  ) " > loader.ctl
sleep 1
sqlldr user/pwd@schema  control=loader.ctl 
break
fi
done

SQLLDR 无法自行执行此操作...

我认为您有几种选择:

  1. 如果格式组合太多;使用脚本处理第一行并动态生成控制文件。
  2. 如果格式数量有限;使用脚本,请查看第一行并将格式与控制文件匹配。
  3. 寻找其他解决方案...也许放弃 SQLLDR 并使用外部表?

您可以尝试使用BOUNDFILLER or FILLER,如下所示。

边界填充或填充器

是一个数据文件映射字段,它不 对应于数据库列。填充字段是分配的值 从它们映射到的数据字段

.

load data
infile 'file1.csv'           
infile 'file2.csv'
append
into table SCHEMA.TABLE
fields terminated by ';' optionally enclosed by '"'
trailing nullcols
(
source_field1     BOUNDFILLER,
source_field2     BOUNDFILLER,
source_field3     BOUNDFILLER,
destination_field1 ":source_field1",
destination_field2 ":source_field2",
destination_field3 ":source_field3");

更多细节可以在这里找到

最新更新