【问题标题】:Using sqlldr to load csv file with sequence使用 sqlldr 加载带有序列的 csv 文件
【发布时间】:2018-02-19 15:17:31
【问题描述】:

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

比如说我想使用命令

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 文件的批量插入)

【问题讨论】:

  • 您使用的是哪个版本的 Oracle?对于 12.1 及更高版本,您可以使用标识列或使用序列的默认值:oracle-base.com/articles/12c/…
  • @KaushikNayak 不幸的是版本 11。v11 不可能吗?

标签: oracle sql-loader


【解决方案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>

或者,您可以使用 sequence(Oracle 对象)。这有点“复杂”(你也需要一个函数)但是 - 如果你需要它,我可以创建一个示例。说吧。

【讨论】:

  • 啊!我想我 6 年前用过它,完全忘记了这个东西。 +1
  • 嗨,谢谢,但我需要为每个 csv 增加序列。因此,第一个 CSV 插入中的所有行都将有 1 作为 ID,但第二个 CSV 将有 2 作为所有行。这可能吗?由于线程安全,我需要使用序列,以便我可以区分同一个表中的每个批量插入
  • 我创建了一个新示例并编辑了我的答案;请看一下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多