且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将CSV文件数据加载到Oracle中的表变量(索引表)中

更新时间:2023-01-21 20:15:17

要将文本文件中的大量数据加载到Oracle中,请此处).

For loading large amounts of data from a text file into Oracle, the SQL*Loader utility is a good choice. The software is included in the Oracle client installation (which you can download for example here).

假设您要将数据导入具有结构的目标表(target_table)

Assuming you're importing the data into a target table (target_table) having the structure

CREATE TABLE target_table (
     from_party VARCHAR2(15) NOT NULL,
     to_party   VARCHAR2(15) NOT NULL,
     created    DATE,
     updated    DATE,
     status     CHAR(1),
     tries      NUMBER(1)
)

,并使用具有以下结构的两列源数据文件(在路径/path/to/party_import.csv中)

and using a two-column source data file (in path /path/to/party_import.csv) with the following structure

OldID,NewID
015110044200015,099724838000015
069167641100015,099724838000015
016093943300015,099728485000015
033264160300015,099728485000015
035968914300015,099728485000015
087580324300015,099728485000015

您可以将控制文件与一起使用以下内容:

you can use a control file with the following contents:

OPTIONS (SKIP=1)
LOAD DATA
INFILE '/path/to/party_import.csv'
BADFILE 'import.bad'
INSERT
INTO TABLE target_table
fields terminated by ","  TRAILING NULLCOLS
(
  from_party,
  to_party,
  created sysdate
)

运行SQL * Loader

要运行SQL * Loader,可以调用以下命令:

Running SQL*Loader

To run SQL*Loader, you can invoke the following command:

sqlldr username/pw@db_connection control=/path/to/control_file.ctl

这假定已预先完成以下设置:

This assumes that following setup has been done beforehand:

  • 已安装Oracle客户端(包括SQL * Loader)
  • sqlldr.exe在路径中,或使用了可执行文件的绝对路径
  • 您已经通过Oracle的Net配置助手或通过手动提供tnsnames.ora文件并设置TNS_ADMIN环境变量来配置数据库连接(db_connection)(示例
  • Oracle client (including SQL*Loader) has been installed
  • sqlldr.exe is in path, or the absolute path to the executable is used
  • You have configured the database connection (db_connection), either through Oracle's Net configuration assistant or by manually providing a tnsnames.ora file and setting the TNS_ADMIN environment variable (example here)

默认情况下,仅在处理完整个文件后才提交事务.如果您想提交例如每1000行,您可以使用ROWS选项执行此操作:

By default, the transaction is committed only after the whole file has been processed. If you want to commit e.g. every 1000 rows, you can use the ROWS option to do this:

sqlldr username/pw@db_connection control=/path/to/control_file.ctl ROWS=1000