我在 Oracle 中有一个包含 6 列的表和一个包含 5 个“列”的逗号分隔文本文件
我需要将年份放在每行的第一列中。年份不在文本文件中。对于附加到表中的每一行来说,它都是相同的。此过程将每年运行一次。
示例文本文件:
2541-1,36,00000,Some Words Here,00000000
1425-4,25,15245,Some Other Words,45786524
6548-8,12,30210,Different Words,885411246
如何使用 SQL*Loader 将第一列设置为年份?
我想要的结果:
A 栏 | B 栏 | C 栏 | D 栏 | E 栏 | F 栏 |
---|---|---|---|---|---|
2023 | 2541-1 | 36 | 00000 | 这里有几句话 | 00000000 |
2023 | 1425-4 | 25 | 15245 | 其他一些话 | 45786524 |
2023 | 6548-8 | 12 | 30210 | 不同的词 | 885411246 |
一个选项是“加载”一个常量。
表格 - 最初 - 空:
SQL> desc test
Name Null? Type
----------------------------------------- -------- ----------------------------
COLA NUMBER
COLB VARCHAR2(7)
COLC NUMBER
COLD VARCHAR2(5)
COLE VARCHAR2(25)
COLF VARCHAR2(10)
COLG NUMBER
COLH NUMBER
COLI NUMBER
SQL> select * From test;
no rows selected
控制文件:
load data
infile *
replace
into table test
fields terminated by ','
trailing nullcols
( cola constant "2023",
colb,
colc,
cold,
cole,
colf,
colg,
colh,
coli
)
begindata
2541-1,36,00000,Some Words Here,00000000,42564,63514,78546
1425-4,25,15245,Some Other Words,45786524,452654,156324,185647
6548-8,12,30210,Different Words,885411246,251624,846102,152026
加载会话:
SQL> $sqlldr scott/tiger@pdb1 control=test16.ctl log=test16.log
SQL*Loader: Release 21.0.0.0.0 - Production on Mon Mar 18 19:34:15 2024
Version 21.3.0.0.0
Copyright (c) 1982, 2021, Oracle and/or its affiliates. All rights reserved.
Path used: Conventional
Commit point reached - logical record count 2
Commit point reached - logical record count 3
Table TEST:
3 Rows successfully loaded.
Check the log file:
test16.log
for more information about the load.
结果:
SQL> select * From test;
COLA COLB COLC COLD COLE COLF COLG COLH COLI
---------- ------- ---------- ----- ------------------------- ---------- ---------- ---------- ----------
2023 2541-1 36 00000 Some Words Here 00000000 42564 63514 78546
2023 1425-4 25 15245 Some Other Words 45786524 452654 156324 185647
2023 6548-8 12 30210 Different Words 885411246 251624 846102 152026
SQL>
另一个选项是在创建该表时为
cola
设置默认值:
SQL> drop table test;
Table dropped.
SQL> create table test
2 (cola number default extract(year from sysdate),
3 colb varchar2(7),
4 colc number,
5 cold varchar2(5),
6 cole varchar2(25),
7 colf varchar2(10),
8 colg number,
9 colh number,
10 coli number);
Table created.
控制文件:与之前相同,只是完全删除
cola
行:
<snip>
trailing nullcols
( colb,
colc,
<snip>
加载会话和结果:
SQL> $sqlldr scott/tiger@pdb1 control=test16.ctl log=test16.log
SQL*Loader: Release 21.0.0.0.0 - Production on Mon Mar 18 19:38:09 2024
Version 21.3.0.0.0
Copyright (c) 1982, 2021, Oracle and/or its affiliates. All rights reserved.
Path used: Conventional
Commit point reached - logical record count 2
Commit point reached - logical record count 3
Table TEST:
3 Rows successfully loaded.
Check the log file:
test16.log
for more information about the load.
SQL> select * From test;
COLA COLB COLC COLD COLE COLF COLG COLH COLI
---------- ------- ---------- ----- ------------------------- ---------- ---------- ---------- ----------
2024 2541-1 36 00000 Some Words Here 00000000 42564 63514 78546
2024 1425-4 25 15245 Some Other Words 45786524 452654 156324 185647
2024 6548-8 12 30210 Different Words 885411246 251624 846102 152026
SQL>
另一种选择是插入“计算”值;在这种情况下,控制文件中的calculated列必须是last。
SQL> drop table test;
Table dropped.
SQL> create table test
2 (cola number,
3 colb varchar2(7),
4 colc number,
5 cold varchar2(5),
6 cole varchar2(25),
7 colf varchar2(10),
8 colg number,
9 colh number,
10 coli number);
Table created.
控制文件:
load data
infile *
replace
into table test
fields terminated by ','
trailing nullcols
( colb,
colc,
cold,
cole,
colf,
colg,
colh,
coli,
cola "extract (year from sysdate)"
)
begindata
2541-1,36,00000,Some Words Here,00000000,42564,63514,78546
1425-4,25,15245,Some Other Words,45786524,452654,156324,185647
6548-8,12,30210,Different Words,885411246,251624,846102,152026
加载会话和结果:
SQL> $sqlldr scott/tiger@pdb1 control=test16.ctl log=test16.log
SQL*Loader: Release 21.0.0.0.0 - Production on Mon Mar 18 19:42:46 2024
Version 21.3.0.0.0
Copyright (c) 1982, 2021, Oracle and/or its affiliates. All rights reserved.
Path used: Conventional
Commit point reached - logical record count 2
Commit point reached - logical record count 3
Table TEST:
3 Rows successfully loaded.
Check the log file:
test16.log
for more information about the load.
SQL> select * From test;
COLA COLB COLC COLD COLE COLF COLG COLH COLI
---------- ------- ---------- ----- ------------------------- ---------- ---------- ---------- ----------
2024 2541-1 36 00000 Some Words Here 00000000 42564 63514 78546
2024 1425-4 25 15245 Some Other Words 45786524 452654 156324 185647
2024 6548-8 12 30210 Different Words 885411246 251624 846102 152026
SQL>