使用条件控制器 sqlldr 从多行加载数据
Load data from multiple lines with condition controller sqlldr
我有一个包含如下所述数据的 csv 文件:
- Data1|data2|data3....
- Data4|data5|data6....
- Ctr|1|2
- Lst|1|30
- Lst|1|40
- Lst|1|50
- Data7|data8....
- Ctr|2|3
- Lst|2|60
- Lst|2|70
我有一个 table controle ( data_type varchar,
Id_control 变量,
Type_liste 变量,
Id_subcontrol 变量)
我正在使用 sql 加载程序来填充 table,我期望的结果是:
- data_type | Id_control|Type_liste| Id_subcontrol
- Ctrl | 1 | NULL | 2
- Lst | 1 | 30 | NULL
- Lst | 1 | 40 | NULL
- Lst | 1 | 50 | NULL
- Ctr | 2 | NULL | 3
- Lst | 2 | 60 | NULL
- Lst | 2 | 70 | NULL
我试过了,但是第二部分 return 0 行已加载
LOAD DATA
CHARACTERSET UTF8
TRUNCATE
INTO TABLE controle
WHEN (1:4) = 'Ctrl|'
FIELDS TERMINATED BY "|"
TRAILING NULLCOLS
(
Data_type CHAR,
Id_control CHAR,
Id_subcontrol CHAR
)
INTO TABLE controle
WHEN (1:3) = 'Lst'
FIELDS TERMINATED BY "|"
TRAILING NULLCOLS
(
Data_type CHAR,
Id_control CHAR,
type_list CHAR
)
有什么想法吗?
提前致谢。
作为the docs say:
A key point when using multiple INTO TABLE clauses is that field
scanning continues from where it left off when a new INTO TABLE clause
is processed. The remainder of this section details important ways to
make use of that behavior. It also describes alternative ways of using
fixed field locations or the POSITION parameter.
因此在处理 Lst 条件时,它会继续查找当前行上的列。
您可以通过使用 position
定义第一个字段来重置此行的开头:
LOAD DATA
INFILE *
TRUNCATE
INTO TABLE controle
WHEN Data_type = 'Ctr'
FIELDS TERMINATED BY "|"
TRAILING NULLCOLS
(
Data_type CHAR,
Id_control CHAR,
Id_subcontrol CHAR
)
INTO TABLE controle
WHEN Data_type = 'Lst'
FIELDS TERMINATED BY "|"
TRAILING NULLCOLS
(
Data_type POSITION(1:3) CHAR,
Id_control CHAR,
type_list CHAR
)
BEGINDATA
Data1|data2|data3
Data4|data5|data6
Ctr|1|2
Lst|1|30
Lst|1|40
Lst|1|50
Data7|data8|data9
Ctr|2|3
Lst|2|60
Lst|2|70
我有一个包含如下所述数据的 csv 文件:
- Data1|data2|data3....
- Data4|data5|data6....
- Ctr|1|2
- Lst|1|30
- Lst|1|40
- Lst|1|50
- Data7|data8....
- Ctr|2|3
- Lst|2|60
- Lst|2|70
我有一个 table controle ( data_type varchar, Id_control 变量, Type_liste 变量, Id_subcontrol 变量)
我正在使用 sql 加载程序来填充 table,我期望的结果是:
- data_type | Id_control|Type_liste| Id_subcontrol
- Ctrl | 1 | NULL | 2
- Lst | 1 | 30 | NULL
- Lst | 1 | 40 | NULL
- Lst | 1 | 50 | NULL
- Ctr | 2 | NULL | 3
- Lst | 2 | 60 | NULL
- Lst | 2 | 70 | NULL
我试过了,但是第二部分 return 0 行已加载
LOAD DATA
CHARACTERSET UTF8
TRUNCATE
INTO TABLE controle
WHEN (1:4) = 'Ctrl|'
FIELDS TERMINATED BY "|"
TRAILING NULLCOLS
(
Data_type CHAR,
Id_control CHAR,
Id_subcontrol CHAR
)
INTO TABLE controle
WHEN (1:3) = 'Lst'
FIELDS TERMINATED BY "|"
TRAILING NULLCOLS
(
Data_type CHAR,
Id_control CHAR,
type_list CHAR
)
有什么想法吗? 提前致谢。
作为the docs say:
A key point when using multiple INTO TABLE clauses is that field scanning continues from where it left off when a new INTO TABLE clause is processed. The remainder of this section details important ways to make use of that behavior. It also describes alternative ways of using fixed field locations or the POSITION parameter.
因此在处理 Lst 条件时,它会继续查找当前行上的列。
您可以通过使用 position
定义第一个字段来重置此行的开头:
LOAD DATA
INFILE *
TRUNCATE
INTO TABLE controle
WHEN Data_type = 'Ctr'
FIELDS TERMINATED BY "|"
TRAILING NULLCOLS
(
Data_type CHAR,
Id_control CHAR,
Id_subcontrol CHAR
)
INTO TABLE controle
WHEN Data_type = 'Lst'
FIELDS TERMINATED BY "|"
TRAILING NULLCOLS
(
Data_type POSITION(1:3) CHAR,
Id_control CHAR,
type_list CHAR
)
BEGINDATA
Data1|data2|data3
Data4|data5|data6
Ctr|1|2
Lst|1|30
Lst|1|40
Lst|1|50
Data7|data8|data9
Ctr|2|3
Lst|2|60
Lst|2|70