我有一个.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
非常感谢。
答案 0 :(得分:1)
假设这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
答案 1 :(得分:0)
SQLLDR不能单独执行此操作...
我认为您有几种选择:
答案 2 :(得分:-1)
您可以尝试使用BOUNDFILLER or FILLER
,如下所示。
BOUNDFILLER或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");
更多详细信息,请here