如何用sql loader中的另一个值替换值?

时间:2016-12-30 23:30:57

标签: sql sql-loader

我想替换将加载到domain_label列的值,如下所示:

如果3 cntg03替换为cntg03

如果2 cntg02替换为cntg02

如果1 cntg01替换为cntg01

这可能吗?

我有以下控制文件:

LOAD DATA 
INFILE u/local/r2x/data/rda_prod_cntg.dat
TRUNCATE 
INTO TABLE R2X1.RDA_PROD_CNTG
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
(
ITEM, 
GLOBAL_DOMAIN_TYPE CONSTANT 'S',
GLOBAL_ID CONSTANT 10,
GLOBAL_DOMAIN_DESC CONSTANT 'Sales Forecast',
filler2 FILLER,
filler3 FILLER,
filler4 FILLER,
filler5 FILLER,
filler6 FILLER,
filler7 FILLER,
filler8 FILLER,
filler9 FILLER,
filler10 FILLER,
filler11 FILLER,
filler12 FILLER,
filler13 FILLER,
filler14 FILLER,
filler15 FILLER,
filler16 FILLER,
filler17 FILLER,
filler18 FILLER,
filler19 FILLER,
filler20 FILLER,
DOMAIN_ID,
DOMAIN_LABEL,
filler23 FILLER,
filler24 FILLER,
filler25 FILLER, 
filler26 FILLER 
)

1 个答案:

答案 0 :(得分:1)

您可以为该列使用case表达式。

DOMAIN_LABEL "case when :DOMAIN_LABEL = '3 cntg03' then 'cntg03'
                   when :DOMAIN_LABEL = '2 cntg02' then 'cntg02'
                   when :DOMAIN_LABEL = '1 cntg01' then 'cntg01'
              end"

如果必须对多个值进行此操作,并且模式类似,则可以使用substr函数并从第3个字符开始获取所有字符,

DOMAIN_LABEL "substr(:DOMAIN_LABEL,3)"

完整的脚本将是

LOAD DATA 
INFILE u/local/r2x/data/rda_prod_cntg.dat
TRUNCATE 
INTO TABLE R2X1.RDA_PROD_CNTG
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
(
ITEM, 
GLOBAL_DOMAIN_TYPE CONSTANT 'S',
GLOBAL_ID CONSTANT 10,
GLOBAL_DOMAIN_DESC CONSTANT 'Sales Forecast',
filler2 FILLER,
filler3 FILLER,
filler4 FILLER,
filler5 FILLER,
filler6 FILLER,
filler7 FILLER,
filler8 FILLER,
filler9 FILLER,
filler10 FILLER,
filler11 FILLER,
filler12 FILLER,
filler13 FILLER,
filler14 FILLER,
filler15 FILLER,
filler16 FILLER,
filler17 FILLER,
filler18 FILLER,
filler19 FILLER,
filler20 FILLER,
DOMAIN_ID,
DOMAIN_LABEL "substr(:DOMAIN_LABEL,3)",
/* or you can use a case expression
 DOMAIN_LABEL "case when :DOMAIN_LABEL = '3 cntg03' then 'cntg03'
                    when :DOMAIN_LABEL = '2 cntg02' then 'cntg02'
                    when :DOMAIN_LABEL = '1 cntg01' then 'cntg01'
               end",
*/

filler23 FILLER,
filler24 FILLER,
filler25 FILLER, 
filler26 FILLER 
)