Oracle:导入 CSV 文件

发布于 2024-11-11 14:14:19 字数 214 浏览 4 评论 0原文

我已经搜索了一段时间,但似乎找不到答案,所以这里...

我有一个 CSV 文件,我想将其导入到 Oracle (9i/10i) 中的表中。

稍后我计划将该表用作其他用途的查找。

这实际上是我正在研究的一种解决方法,因为使用 IN 子句查询超过 1000 个值是不可能的。

这是如何使用 SQLPLUS 完成的?

感谢您抽出时间! :)

I've been searching for a while now but can't seem to find answers so here goes...

I've got a CSV file that I want to import into a table in Oracle (9i/10i).

Later on I plan to use this table as a lookup for another use.

This is actually a workaround I'm working on since the fact that querying using the IN clause with more that 1000 values is not possible.

How is this done using SQLPLUS?

Thanks for your time! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

灯角 2024-11-18 14:14:19

您可以使用的另一个解决方案是 SQL Developer。

有了它,您就可以从 csv 文件导入(也可以使用其他分隔文件)。

只需打开表视图,然后:

  • 选择操作
  • 导入数据
  • 查找您的文件
  • 选择您的选项。

您可以选择让 SQL Developer 为您执行插入操作、创建 SQL 插入脚本或为 SQL Loader 脚本创建数据(我自己还没有尝试过此选项)。

当然,如果您只能使用命令行,那么所有这些都是没有意义的,但是如果您能够在本地使用 SQL Developer 进行测试,那么您始终可以部署生成的插入脚本(例如)。

只是在这两个已经很好的答案中添加另一个选项。

Another solution you can use is SQL Developer.

With it, you have the ability to import from a csv file (other delimited files are available).

Just open the table view, then:

  • choose actions
  • import data
  • find your file
  • choose your options.

You have the option to have SQL Developer do the inserts for you, create an sql insert script, or create the data for a SQL Loader script (have not tried this option myself).

Of course all that is moot if you can only use the command line, but if you are able to test it with SQL Developer locally, you can always deploy the generated insert scripts (for example).

Just adding another option to the 2 already very good answers.

云裳 2024-11-18 14:14:19

SQL Loader 帮助将 csv 文件加载到表中:SQL*Loader

如果您只想要 sqlplus,那么它会变得有点复杂。您需要找到 sqlloader 脚本和 csv 文件,然后运行 ​​sqlldr 命令。

SQL Loader helps load csv files into tables: SQL*Loader

If you want sqlplus only, then it gets a bit complicated. You need to locate your sqlloader script and csv file, then run the sqlldr command.

稚气少女 2024-11-18 14:14:19

另一种解决方案是使用外部表: http://www.orafaq.com/node/848

当您必须非常频繁且快速地执行此导入时,请使用此选项。

An alternative solution is using an external table: http://www.orafaq.com/node/848

Use this when you have to do this import very often and very fast.

画尸师 2024-11-18 14:14:19

SQL Loader 是最佳选择。
我最近从 csv 文件加载了我的表格,这是这个概念的新手,想分享一个例子。

LOAD DATA
    infile '/ipoapplication/utl_file/LBR_HE_Mar16.csv'
    REPLACE
    INTO TABLE LOAN_BALANCE_MASTER_INT
    fields terminated by ',' optionally enclosed by '"'
    (
    ACCOUNT_NO,
    CUSTOMER_NAME,
    LIMIT,
    REGION

    )

将控制文件和 csv 放在服务器上的同一位置。
找到 sqlldr exe 并调用它。

sqlldr 用户 ID/密码@数据库名称 control=
例如:sqlldr abc/xyz@ora control=load.ctl

希望有帮助。

SQL Loader is the way to go.
I recently loaded my table from a csv file,new to this concept,would like to share an example.

LOAD DATA
    infile '/ipoapplication/utl_file/LBR_HE_Mar16.csv'
    REPLACE
    INTO TABLE LOAN_BALANCE_MASTER_INT
    fields terminated by ',' optionally enclosed by '"'
    (
    ACCOUNT_NO,
    CUSTOMER_NAME,
    LIMIT,
    REGION

    )

Place the control file and csv at the same location on the server.
Locate the sqlldr exe and invoce it.

sqlldr userid/passwd@DBname control=
Ex : sqlldr abc/xyz@ora control=load.ctl

Hope it helps.

岁月静好 2024-11-18 14:14:19

我想分享 2 个技巧:(技巧 1)创建 csv 文件(技巧 2)将 csv 文件中的行加载到表中。

====[ (提示 1) SQLPLUS 从 Oracle 表创建 csv 文件 ]====

我使用 SQLPLUS 和以下命令:

set markup csv on
set lines 1000
set pagesize 100000 linesize 1000
set feedback off 
set trimspool on
spool /MyFolderAndFilename.csv
Select *  from MYschema.MYTABLE  where MyWhereConditions ;
spool off
exit

====[提示 2 SQLLDR 将 csv 文件加载到表中 ]= ===

我使用 SQLLDR 和 csv(逗号分隔)文件将 csv 文件中的行添加(追加)到表中。
该文件在字段之间有 , 文本字段在文本之前和之后有 "
关键:如果最后一列为空,则行末尾有一个 ,

csv 文件中的数据行示例:

11,"aa",1001
22,"bb',2002
33,"cc",
44,"dd",4004
55,"ee',

这是控制文件:

LOAD DATA
APPEND 
INTO TABLE MYSCHEMA.MYTABLE
fields terminated by ',' optionally enclosed by '"'
TRAILING NULLCOLS
(
CoulmnName1,
CoulmnName2,
CoulmnName3
)

这是在 Linux 中执行 sqlldr 的命令。如果您在 Windows 中运行,请使用 \ 而不是 /c:

sqlldr userid=MyOracleUser/MyOraclePassword@MyOracleServerIPaddress:port/MyOracleSIDorService  DATA=datafile.csv  CONTROL=controlfile.ctl  LOG=logfile.log  BAD=notloadedrows.bad

祝您好运!

I would like to share 2 tips: (tip 1) create a csv file (tip 2) Load rows from a csv file into a table.

====[ (tip 1) SQLPLUS to create a csv file form an Oracle table ]====

I use SQLPLUS with the following commands:

set markup csv on
set lines 1000
set pagesize 100000 linesize 1000
set feedback off 
set trimspool on
spool /MyFolderAndFilename.csv
Select *  from MYschema.MYTABLE  where MyWhereConditions ;
spool off
exit

====[tip 2 SQLLDR to load a csv file into a table ]====

I use SQLLDR and a csv ( comma separated ) file to add (APPEND) rows form the csv file to a table.
the file has , between fields text fields have " before and after the text
CRITICAL: if last column is null there is a , at the end of the line

Example of data lines in the csv file:

11,"aa",1001
22,"bb',2002
33,"cc",
44,"dd",4004
55,"ee',

This is the control file:

LOAD DATA
APPEND 
INTO TABLE MYSCHEMA.MYTABLE
fields terminated by ',' optionally enclosed by '"'
TRAILING NULLCOLS
(
CoulmnName1,
CoulmnName2,
CoulmnName3
)

This is the command to execute sqlldr in Linux. If you run in Windows use \ instead of / c:

sqlldr userid=MyOracleUser/MyOraclePassword@MyOracleServerIPaddress:port/MyOracleSIDorService  DATA=datafile.csv  CONTROL=controlfile.ctl  LOG=logfile.log  BAD=notloadedrows.bad

Good luck !

万劫不复 2024-11-18 14:14:19

有人要求我发布框架的链接!这是我在 Open World 2012 上展示的。这是完整的博客文章,演示了如何使用外部表构建解决方案。

Somebody asked me to post a link to the framework! that I presented at Open World 2012. This is the full blog post that demonstrates how to architect a solution with external tables.

一场春暖 2024-11-18 14:14:19

从 Oracle 18c 开始,您可以使用 内联外部表

内联外部表支持将外部表的运行时定义作为 SQL 语句的一部分,而无需将外部表创建为数据字典中的持久对象。

对于内联外部表,可以在运行时在 SELECT 语句中使用与使用 CREATE TABLE 语句创建外部表相同的语法。在查询块的 FROM 子句中指定内联外部表。包含内联外​​部表的查询还可以包含用于联接、聚合等的常规表。

INSERT INTO target_table(time_id, prod_id, quantity_sold, amount_sold)
SELECT time_id, prod_id, quantity_sold, amount_sold
FROM   EXTERNAL (   
    (time_id        DATE NOT NULL,     
     prod_id        INTEGER NOT NULL,
     quantity_sold  NUMBER(10,2),
     amount_sold    NUMBER(10,2))     
    TYPE ORACLE_LOADER     
    DEFAULT DIRECTORY data_dir1
    ACCESS PARAMETERS (
      RECORDS DELIMITED BY NEWLINE
      FIELDS TERMINATED BY '|')     
   LOCATION ('sales_9.csv') REJECT LIMIT UNLIMITED) sales_external;

From Oracle 18c you could use Inline External Tables:

Inline external tables enable the runtime definition of an external table as part of a SQL statement, without creating the external table as persistent object in the data dictionary.

With inline external tables, the same syntax that is used to create an external table with a CREATE TABLE statement can be used in a SELECT statement at runtime. Specify inline external tables in the FROM clause of a query block. Queries that include inline external tables can also include regular tables for joins, aggregation, and so on.

INSERT INTO target_table(time_id, prod_id, quantity_sold, amount_sold)
SELECT time_id, prod_id, quantity_sold, amount_sold
FROM   EXTERNAL (   
    (time_id        DATE NOT NULL,     
     prod_id        INTEGER NOT NULL,
     quantity_sold  NUMBER(10,2),
     amount_sold    NUMBER(10,2))     
    TYPE ORACLE_LOADER     
    DEFAULT DIRECTORY data_dir1
    ACCESS PARAMETERS (
      RECORDS DELIMITED BY NEWLINE
      FIELDS TERMINATED BY '|')     
   LOCATION ('sales_9.csv') REJECT LIMIT UNLIMITED) sales_external;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文