SQL 加载程序 - 使用 TERMINATED 将数据加载到不同 table

SQL Loader - Load data using TERMINATED into different table

例如,在提要文件中我得到了这样的数据

HEADER|AB|C|DATAHERE
DETAIL|DATA1|DATA2|DATA3
DETAIL|DATA1|DATA2|DATA3
DETAIL|DATA1|DATA2|DATA3
DETAIL|DATA1|DATA2|DATA3
FOOTER|COUNTER

第一行应插入 table标题 第 2 行到第 5 行应插入 tableDetail 最后一行应该插入 tableFooter

这是我的 sqlloader 脚本

Load DATA
REPLACE INTO TABLE tableHeader
WHEN (1:6) = 'HEADER'
FIELDS TERMINATED BY '|'
OPTIONALLY ENCLOSED BY '"' 
TRAILING NULLCOLS
(
    COL1 FILLER,
    COL2 FILLER,
    COL3 FILLER,
    HEADER_DETAIL,
)


INTO TABLE tableDetail
WHEN (1:6) = 'DETAIL'
FIELDS TERMINATED BY '|'
OPTIONALLY ENCLOSED BY '"' 
TRAILING NULLCOLS
(
    A FILLER,
    DATA1,
    DATA2,
    DATA3
)

INTO TABLE tableFooter
WHEN (1:6) = 'FOOTER'
FIELDS TERMINATED BY '|'
OPTIONALLY ENCLOSED BY '"' 
TRAILING NULLCOLS
(
    Z FILLER,
    FOOTER_DETAIL
)

插入到 tableHeader 中的数据是正确的,但是插入到其他 table 中的数据一团糟...任何人都可以帮助我吗?

逻辑记录组件混乱。这是 mentioned in the documentation:

... Field scanning does not start over from the beginning of the record for a new INTO TABLE clause. Instead, scanning continues where it left off.

To force record scanning to start in a specific location, you use the POSITION parameter.

所以你可以用POSITION(1:7)来识别第一个填充字段长度:

LOAD DATA
REPLACE INTO TABLE tableHeader
WHEN (1:6) = 'HEADER'
FIELDS TERMINATED BY '|'
OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(
    COL1 FILLER,
    COL2 FILLER,
    COL3 FILLER,
    HEADER_DETAIL
)
INTO TABLE tableDetail
WHEN (1:6) = 'DETAIL'
FIELDS TERMINATED BY '|'
OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(
    A FILLER POSITION(1:7),
    DATA1,
    DATA2,
    DATA3
)
INTO TABLE tableFooter
WHEN (1:6) = 'FOOTER'
FIELDS TERMINATED BY '|'
OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(
    Z FILLER POSITION(1:7),
    FOOTER_DETAIL
)

这会将您的示例数据加载到我创建的一些虚拟表中的正确字段中。如果没有这些位置规范,只有 header 被加载,其余的都被丢弃为空;位置为 1:6 的数据进入了错误的列,我认为这就是你所描述的。

您可以为 WHEN 使用命名字段,但可能需要包含分隔符:

LOAD DATA
REPLACE INTO TABLE tableHeader
WHEN RECTYPE = 'HEADER|'
FIELDS TERMINATED BY '|'
OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(
    RECTYPE FILLER POSITION(1:7),
    FILL1 FILLER,
    FILL2 FILLER,
    HEADER_DETAIL
)
INTO TABLE tableDetail
WHEN RECTYPE = 'DETAIL|'
FIELDS TERMINATED BY '|'
OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(
    RECTYPE FILLER POSITION(1:7),
    DATA1,
    DATA2,
    DATA3
)
INTO TABLE tableFooter
WHEN RECTYPE = 'FOOTER|'
FIELDS TERMINATED BY '|'
OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(
    RECTYPE FILLER POSITION(1:7),
    FOOTER_DETAIL
)