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

Missing right parenthesis while import data from flat files to table

我有 load data infile .... 一个平面文件。我想从这个平面文件中将数据加载到 table tab 中。我想在列中传递一些值,例如 'ab'、'cd'、'ef' col6 的 table。当我像这样在平面文件中编写代码时

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

但是当我将此文件加载到 table 时,我发现了一个错误 ORA-00907: Missing Right Parenthesis。如何解决此错误以便我可以插入值 'ab'、'cd'、'ef' 在 table tabcol6 中。

您可以使用 a multitable insert,将三个插入到同一个 table:

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.


或者,您可以插入暂存 table,文件中的每条记录只有一行,并完全排除 constant-value col6 - 您可以使用 SQL*加载器:

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

... 或 as an external table;然后通过使用包含常量值的 CTE 查询暂存 table 和 cross-joining 来插入您的真实 table:

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;

对于临时 table 中的每一行,您将在真实 table 中获得三行,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;

这次您为集合中的每个元素获得一行 - table 集合子句将其扩展为多行。结果是一样的。