使用SQLLOADER將多行資料合併為一行進行載入

redhouser發表於2012-09-17

SQL*LOADER支援將多行資料(物理行)合併為一行(邏輯行)處理,本文進行測試驗證。

1,語法說明:
1.1,使用CONCATENATE合併固定行數為一行
format:
CONCATENATE  integer

1.2,使用CONTINUEIF根據條件將多行合併為一行
format:
CONTINUEIF THIS/NEXT [PRESERVE] (pos1:pos2) {=|<>|!=} {str|X'hex_str'}
CONTINUEIF LAST [PRESERVE]  {=|<>|!=} {str|X'hex_str'}

比如,如果每個邏輯記錄的第二列到第五列為“$|$|”,如下格式將資料中包含換行符的多行資料合併為一行:
CONTINUEIF NEXT PRESERVE (2:5) != '$|$|'

2,測試:
2.0測試表

create table multi_line(
x1 varchar2(100),x2 varchar2(100),
x3 varchar2(100),x4 varchar2(100),
x5 varchar2(100),x6 varchar2(100));


2.1測試
test1:
cat normal_line.ctl

LOAD DATA
INFILE *
INTO TABLE multi_line TRUNCATE
fields terminated by ',' optionally enclosed by '"'
trailing nullcols
( x1,
  x2,
  x3,
  x4,
  x5,
  x6
)

BEGINDATA
a1,b1,c1,
a2,b2,c2,
a3,b3,c3,
a4,b4,c4,
a5,b5,c5,
a6,b6,c6,
a7,b7,c7,
a8,b8,c8,
a9,b9,c9,


set linesize 100
set pagesize 0
col x1 for a10
col x2 for a10
col x3 for a10
col x4 for a10
col x5 for a10
col x6 for a10
select * from multi_line;
SQL> select * from multi_line;
a1         b1         c1
a2         b2         c2
a3         b3         c3
a4         b4         c4
a5         b5         c5
a6         b6         c6
a7         b7         c7
a8         b8         c8
a9         b9         c9

9 rows selected.


test2:
cat fixed_num_lines.ctl

LOAD DATA
INFILE *
concatenate 2
INTO TABLE multi_line TRUNCATE
fields terminated by ',' optionally enclosed by '"'
trailing nullcols
( x1,
  x2,
  x3,
  x4,
  x5,
  x6
)

BEGINDATA
a1,b1,c1,
a2,b2,c2,
a3,b3,c3,
a4,b4,c4,
a5,b5,c5,
a6,b6,c6,
a7,b7,c7,
a8,b8,c8,
a9,b9,c9,


set linesize 100
set pagesize 0
col x1 for a10
col x2 for a10
col x3 for a10
col x4 for a10
col x5 for a10
col x6 for a10
SQL> select * from multi_line;
a1         b1         c1         a2         b2         c2
a3         b3         c3         a4         b4         c4
a5         b5         c5         a6         b6         c6
a7         b7         c7         a8         b8         c8
a9         b9         c9


test3:
cat conif_lines.ctl

LOAD DATA
INFILE *
continueif next (1:2) = '--'
INTO TABLE multi_line TRUNCATE
fields terminated by ',' optionally enclosed by '"'
trailing nullcols
( x1,
  x2,
  x3,
  x4,
  x5,
  x6
)

BEGINDATA
a1,b1,c1,
--a2,b2,c2,
a3,b3,c3,
--a4,b4,c4,
a5,b5,c5,
a6,b6,c6,
--a7,b7,c7,
a8,b8,c8,
a9,b9,c9,


set linesize 100
set pagesize 0
col x1 for a10
col x2 for a10
col x3 for a10
col x4 for a10
col x5 for a10
col x6 for a10
SQL> select * from multi_line;
           b1         c1         a2         b2         c2
           b3         c3         a4         b4         c4
           b5         c5
           b6         c6         a7         b7         c7
           b8         c8
           b9         c9

6 rows selected.

--&gtx1 is null!


test4:
cat conif_lines.ctl

LOAD DATA
INFILE *
continueif next preserve (1:2) = '--'
INTO TABLE multi_line TRUNCATE
fields terminated by ',' optionally enclosed by '"'
trailing nullcols
( x1,
  x2,
  x3,
  x4,
  x5,
  x6
)

BEGINDATA
a1,b1,c1,
--a2,b2,c2,
a3,b3,c3,
--a4,b4,c4,
a5,b5,c5,
a6,b6,c6,
--a7,b7,c7,
a8,b8,c8,
a9,b9,c9,


set linesize 100
set pagesize 0
col x1 for a10
col x2 for a10
col x3 for a10
col x4 for a10
col x5 for a10
col x6 for a10
SQL> select * from multi_line;
a1         b1         c1         --a2       b2         c2
a3         b3         c3         --a4       b4         c4
a5         b5         c5
a6         b6         c6         --a7       b7         c7
a8         b8         c8
a9         b9         c9

6 rows selected.


test5:
cat conif_lines.ctl

LOAD DATA
INFILE *
continueif next preserve (1:2) = '--'
INTO TABLE multi_line TRUNCATE
fields terminated by ',' optionally enclosed by '"'
trailing nullcols
( x1,
  x2,
  x3,
  x4 "replace(:x4,'--','')",
  x5,
  x6
)

BEGINDATA
a1,b1,c1,
--a2,b2,c2,
a3,b3,c3,
--a4,b4,c4,
a5,b5,c5,
a6,b6,c6,
--a7,b7,c7,
a8,b8,c8,
a9,b9,c9,


set linesize 100
set pagesize 0
col x1 for a10
col x2 for a10
col x3 for a10
col x4 for a10
col x5 for a10
col x6 for a10
SQL> select * from multi_line;
a1         b1         c1         a2         b2         c2
a3         b3         c3         a4         b4         c4
a5         b5         c5
a6         b6         c6         a7         b7         c7
a8         b8         c8
a9         b9         c9

6 rows selected.


test6:
cat conif_lines.ctl

LOAD DATA
INFILE *
continueif this preserve (1:2) = '--'
INTO TABLE multi_line TRUNCATE
fields terminated by ',' optionally enclosed by '"'
trailing nullcols
( x1,
  x2,
  x3,
  x4,
  x5,
  x6
)

BEGINDATA
a1,b1,c1,
--a2,b2,c2,
a3,b3,c3,
--a4,b4,c4,
a5,b5,c5,
a6,b6,c6,
--a7,b7,c7,
a8,b8,c8,
a9,b9,c9,


set linesize 100
set pagesize 0
col x1 for a10
col x2 for a10
col x3 for a10
col x4 for a10
col x5 for a10
col x6 for a10
SQL> select * from multi_line;
a1         b1         c1
--a2       b2         c2         a3         b3         c3
--a4       b4         c4         a5         b5         c5
a6         b6         c6
--a7       b7         c7         a8         b8         c8
a9         b9         c9

6 rows selected.


test7:
cat conif_lines.ctl

LOAD DATA
INFILE *
continueif last  (= '-')
INTO TABLE multi_line TRUNCATE
fields terminated by ',' optionally enclosed by '"'
trailing nullcols
( x1,
  x2,
  x3,
  x4,
  x5,
  x6
)

BEGINDATA
a1,b1,c1,-
a2,b2,c2,
a3,b3,c3,-
a4,b4,c4,
a5,b5,c5,
a6,b6,c6,-
a7,b7,c7,
a8,b8,c8,
a9,b9,c9,


set linesize 100
set pagesize 0
col x1 for a10
col x2 for a10
col x3 for a10
col x4 for a10
col x5 for a10
col x6 for a10
SQL> select * from multi_line;
a1         b1         c1         -a2        b2         c2
a3         b3         c3         -a4        b4         c4
a5         b5         c5
a6         b6         c6         -a7        b7         c7
a8         b8         c8
a9         b9         c9

6 rows selected.

--&gt雖沒有preserve關鍵詞,“-”仍然保留

 

test7:
--“-”後面加空格


cat conif_lines.ctl

LOAD DATA
INFILE *
continueif last  (= '-')
INTO TABLE multi_line TRUNCATE
fields terminated by ',' optionally enclosed by '"'
trailing nullcols
( x1,
  x2,
  x3,
  x4,
  x5,
  x6
)

BEGINDATA
a1,b1,c1,-  
a2,b2,c2,
a3,b3,c3,-  
a4,b4,c4,
a5,b5,c5,
a6,b6,c6,- 
a7,b7,c7,
a8,b8,c8,
a9,b9,c9,

 

set linesize 100
set pagesize 0
col x1 for a10
col x2 for a10
col x3 for a10
col x4 for a10
col x5 for a10
col x6 for a10
SQL> select * from multi_line;
a1         b1         c1         -  a2      b2         c2
a3         b3         c3         -  a4      b4         c4
a5         b5         c5
a6         b6         c6         -  a7      b7         c7
a8         b8         c8
a9         b9         c9

6 rows selected.

--&gt雖沒有preserve關鍵詞,“-”仍然保留,而且空格也保留了


test8:
--“-”後面加空格,增加preserve

cat conif_lines.ctl

LOAD DATA
INFILE *
continueif last preserve (= '-')
INTO TABLE multi_line TRUNCATE
fields terminated by ',' optionally enclosed by '"'
trailing nullcols
( x1,
  x2,
  x3,
  x4,
  x5,
  x6
)

BEGINDATA
a1,b1,c1,-  
a2,b2,c2,
a3,b3,c3,-  
a4,b4,c4,
a5,b5,c5,
a6,b6,c6,- 
a7,b7,c7,
a8,b8,c8,
a9,b9,c9,

 

set linesize 100
set pagesize 0
col x1 for a10
col x2 for a10
col x3 for a10
col x4 for a10
col x5 for a10
col x6 for a10
SQL> select * from multi_line;
a1         b1         c1         -  a2      b2         c2
a3         b3         c3         -  a4      b4         c4
a5         b5         c5
a6         b6         c6         -  a7      b7         c7
a8         b8         c8
a9         b9         c9

6 rows selected.

--&gt雖沒有preserve關鍵詞,“-”仍然保留,而且空格也保留了

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/18922393/viewspace-743754/,如需轉載,請註明出處,否則將追究法律責任。

相關文章