使用sqlldr加载带序列的csv文件

时间:2018-02-19 02:43:10

标签: oracle sql-loader

是否可以使用sqlldr加载csv文件并同时使用序列?

比如说我想使用命令

sqlldr id/pass@'ip:1521/ORCL' data=path\csv_test.csv  
control=path\control.ctl log=path\log.log bad=path\bad.csv

csv文件加载到数据库中,但同时使用序列创建一个额外的列,该列为每个csv文件插入递增(因此csv文件的每个批量插入)

1 个答案:

答案 0 :(得分:1)

当然有一个选择;它被称为序列。有关它的更多信息,请参见Field List Reference文档。

这是一个例子。

数据将被加载到TEST表中:

SQL> create table test
  2  (id    number,
  3   ename varchar2(20)
  4  );

Table created.

SQL>

序列将用于ID列。控制文件如下所示:

load data
infile *
replace
into table test
(
id     sequence,
ename  char terminated by whitespace
)
begindata
Little
Foot
Stack
Over
Flow

加载会话:

M:\a1_maknuto>sqlldr scott/tiger@orcl control=test21.ctl

SQL*Loader: Release 11.2.0.2.0 - Production on Pon Vel 19 07:20:29 2018

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.

Commit point reached - logical record count 4
Commit point reached - logical record count 5

结果:

SQL> select * From test;

        ID ENAME
---------- --------------------
         1 Little
         2 Foot
         3 Stack
         4 Over
         5 Flow

SQL>

[编辑:啊哈,所有行应该共享相同的“序列”]

好的,然后尝试这样的事情(注意用于ID列的表达式):

load data
infile *
append
into table test
(
id     expression "userenv('sessionid')",
ename  char(30) terminated by whitespace  
)
begindata
Little
Foot
Stack
Over
Flow

一些加载会话:

SQL> $sqlldr scott/tiger@orcl control=test21.ctl

SQL*Loader: Release 11.2.0.2.0 - Production on Pon Vel 19 08:13:23 2018

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.

Commit point reached - logical record count 4
Commit point reached - logical record count 5

SQL> select * From test;

        ID ENAME
---------- --------------------
   4530297 Little
   4530297 Foot
   4530297 Stack
   4530297 Over
   4530297 Flow

SQL> $sqlldr scott/tiger@orcl control=test21.ctl

SQL*Loader: Release 11.2.0.2.0 - Production on Pon Vel 19 08:13:30 2018

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.

Commit point reached - logical record count 4
Commit point reached - logical record count 5

SQL> select * From test;

        ID ENAME
---------- --------------------
   4530297 Little
   4530297 Foot
   4530297 Stack
   4530297 Over
   4530297 Flow
   4530301 Little
   4530301 Foot
   4530301 Stack
   4530301 Over
   4530301 Flow

10 rows selected.

SQL>

或者,您可以使用序列(Oracle对象)。这有点“复杂”(你也需要一个功能)但是 - 如果你需要它,我可以创建一个例子。这么说。