我有一个临时表,现在为空。我想将数据从该平面文件加载到Oracle Temp表。在一列中,平面文件的col3 中提到为" x ",但在表中,我想将其插入为" abc "。如果可能在平面文件中从" x "中删除列值,那么如何可能?或从" x "中替换值,为" ABC "。
sql*loader允许您将SQL运算符应用于字段,因此您可以从文件中操纵值。
假设您有一张简单的桌子,例如:
create table your_table(col1 number, col2 number, col3 varchar2(3));
和类似的数据文件:
1,42,xyz
2,42,
3,42,X
然后,您可以使用案例表达式将控制文件用固定值'abc'在col3
中替换为" X"值:
load data
replace
into table your_table
fields terminated by ',' optionally enclosed by '"'
trailing nullcols
(
col1,
col2,
col3 "CASE WHEN :COL3 = 'X' THEN 'abc' ELSE :COL3 END"
)
使用该控件文件运行该文件插入三行:
select * from your_table;
COL1 COL2 COL
---------- ---------- ---
1 42 xyz
2 42
3 42 abc
'x'已更换,保留了其他值。
如果要"删除"该值而不是替换值,则可以做同样的事情,但用null为固定值:
: col3 "CASE WHEN :COL3 = 'X' THEN NULL ELSE :COL3 END"
,或者您可以使用nullif
或defaultif
:
col3 nullif(col3 = 'X')
DECODE
,对吗?
SQL> create table test (id number, col3 varchar2(20));
Table created.
SQL> $type test25.ctl
load data
infile *
replace into table test
fields terminated by ',' trailing nullcols
(
id,
col3 "decode(:col3, 'x', 'abc', :col3)"
)
begindata
1,xxx
2,yyy
3,x
4,123
SQL>
SQL> $sqlldr scott/tiger@orcl control=test25.ctl log=test25.log
SQL*Loader: Release 11.2.0.2.0 - Production on ╚et O×u 29 12:57:56 2018
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Commit point reached - logical record count 3
Commit point reached - logical record count 4
SQL> select * From test order by id;
ID COL3
---------- --------------------
1 xxx
2 yyy
3 abc
4 123
SQL>