0

我有一个示例 csv 文件,其中包含要加载到 oracle 表中的数据。样本数据就像

1,aa,b,c
2,x,yy,zzz
1,aa,b,c
2,x,yy,zzz

这是两个不同的记录,第一个字符分别为“1”和“2”,我在 db 中有一个数据表,其中包含第一个记录的列,然后是第二个记录的列。我尝试使用“WHEN”子句加载数据,但问题是它没有按顺序加载数据。它首先为“1”加载数据,然后为“2”加载数据。喜欢

ID  col1    col2    col3    col4    col5    col6
1   aa      b       c       null    null    null
1   aa      b       c       null    null    null
2   null    null    null    x       yy      zzz
2   null    null    null    x       yy      zzz

这是加载程序代码:

load data
infile 'C:\sample.csv'
APPEND 
INTO TABLE "temp"
WHEN "ID" = '1'
fields terminated by "," optionally enclosed by '"' trailing nullcols
(
Col1,
Col2,       
Col3
)
INTO TABLE "temp"
WHEN "ID" = '2'
fields terminated by "," optionally enclosed by '"' trailing nullcols
(
Col4,
Col5,       
Col6
)

我只想将数据加载为:

ID  col1    col2    col3    col4    col5    col6
1   aa      b       c       null    null    null
2   null    null    null    x       yy      zzz
1   aa      b       c       null    null    null
2   null    null    null    x       yy      zzz

任何帮助,将不胜感激。

4

3 回答 3

0

使用“CONTINUEIF”从数据文件中的多行记录组合逻辑记录。如果条件匹配,这会告诉 SQL*Loader 继续运行。在这里,如果下一行的第一个位置是“2”,则表示继续。控制文件的结构是不加载record_part号:

LOAD DATA
infile test.dat
truncate
continueif NEXT (1:1) = '2'
INTO TABLE X_test
fields terminated by ',' TRAILING NULLCOLS
(rec_part FILLER
,col1 
,col2
,col3
,col4
,col5
,col6
)

加载后:

在此处输入图像描述

如果数据总是在两行,请替换此行:

continueif NEXT (1:1) = '2'

concatenate 2

这表示每个逻辑记录始终是数据中的 2 行。

于 2014-12-26T17:34:09.733 回答
0

您可以将其加载到临时表中,添加一个序列号列。然后将其加载到另一个表中,根据需要拆分列。

这未经测试,但给你一个粗略的想法:

create or replace tmpsequence;

load data
infile 'C:\sample.csv'
APPEND 
INTO TABLE "temp"
fields terminated by "," optionally enclosed by '"' trailing nullcols
(
rec_id "tempsequence.nextval",
id,
Col1,
Col2,       
Col3
)

create table temp2
  select rec_id, id, case when id = "1" then col1 else null end
                   , case when id = "1" then col2 else null end
                   , case when id = "1" then col3 else null end
                   , case when id = "2" then col1 else null end
                   , case when id = "2" then col2 else null end
                   , case when id = "2" then col3 else null end
    from temp

然后你应该能够看到你想要什么

select * from temp2 order by rec_id
于 2013-06-11T18:52:36.573 回答
0

如果您通过 sqlloader 命令加载数据,请尝试添加参数 rows = 1。这将强制 sqlloader 在表中插入每一行数据,从而使文件加载顺序。默认情况下,行数是 64,这就是为什么你没有得到一个小文件的顺序加载。

希望这可以帮助..

于 2016-02-11T06:36:01.383 回答