从平面文件导入数据到表时缺少右括号

问题描述 投票:1回答:1

我有load data infile ....一个平面文件。我想从这个平面文件中将数据加载到表格选项卡中。我想在表格的第col6列中传递几个值,如'ab','cd','ef'。当我像这样在平面文件中编写代码时

load data infile <source-path>
into tab
fields terminated by ','
(
 col1 "TRIM(:col1)" ,
 ............
 ...........
 col6 "('ab','cd','ef')",
 ..........)

但是当我把这个文件加载到表中然后我发现了一个错误ORA-00907: Missing Right Parenthesis。如何解决此错误,以便我可以在表格选项卡的col6中插入'ab','cd','ef'的值。

oracle sql-loader
1个回答
2
投票

你可以使用a multitable insert,在同一个表中有三个插入:

load data infile <source-path>
into tab
fields terminated by ','
(
 col1 "TRIM(:col1)" ,
 ............
 ...........
 col6 CONSTANT 'ab',
 ..........)
into tab
fields terminated by ','
(
 col1 POSITION(1) "TRIM(:col1)" ,
 ............
 ...........
 col6 CONSTANT 'cd',
 ..........)
into tab
fields terminated by ','
(
 col1 POSITION(1) "TRIM(:col1)" ,
 ............
 ...........
 col6 CONSTANT 'ef',
 ..........)

POSITION(1)重置到记录的开头,因此它会在每次插入时再次看到来自源记录的相同值。 Read more


或者,您可以插入到临时表中,文件中的每个记录都有一行,并且完全排除常量值col6 - 您可以使用SQL * Loader:

load data infile <source-path>
into staging_tab
fields terminated by ','
(
 col1 "TRIM(:col1)" ,
 ............
 ...........
 col5 ...
 col7 ...
 ..........)

...或as an external table;然后通过查询登台表并与包含常量值的CTE交叉连接插入到您的真实表中:

insert into tab (col1, col2, ..., col6, ...)
with constants (col6) as (
            select 'ab' from dual
  union all select 'cd' from dual
  union all select 'ef' from dual
)
select st.col1, st.col2, ..., c.col6, ...
from staging_tab st
cross join constants c;

对于临时表中的每一行,您将在实际表中获得三行,一行用于CTE中的每个虚拟行。您可以使用集合而不是CTE执行相同的操作:

insert into tab (col1, col2, col6)
select st.col1, st.col2, c.column_value
from staging_tab st
cross join table(sys.odcivarchar2list('ab', 'cd', 'ef')) c;

这次,您为集合中的每个元素获取一行 - 由表集合子句扩展为多行。结果是一样的。

© www.soinside.com 2019 - 2024. All rights reserved.