使用 CSV 文件中的值更新 Oracle 表
我有一个 CSV 文件,其中包含 ID 和其他几个列。我在 oracle 中也有一个表,其中 ID 标识一行。如何才能最好地将表中的值替换为 CSV 文件中的值,同时保持其他列保持原来的状态?
这必须使用oracle本身可用的工具(即PL/SQL或SQL脚本)来完成,我不能使用“真正的”脚本语言(Python,...)或“真正的”程序。
谢谢, 托马斯
I have a CSV file which contains an ID and several other columns. I also have a table in oracle where the ID identifies a row. How can I best replace the values that are in the table with the values in the CSV file while keeping the other columns the way they were before?
This has to be done with tools available in oracle itself (i.e. PL/SQL or SQL scripts), I can not use a "real" scripting language (Python, ...) or a "real" program.
Thanks,
Thomas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
查看 Oracle 文档中的外部表。您可以将 csv 文件复制到 Oracle 服务器上,并让 Oracle 将其显示为可以运行查询的“普通”表。
那么问题就变成了将数据从一个表复制到另一个表。
外部表对于处理这样的数据加载确实非常有用。
Look at EXTERNAL TABLES in the Oracle doc. You can copy the csv file onto the Oracle server box and get Oracle to present it as a "normal" table on which you can run queries.
Then the problem just becomes one of copying data from one table to another.
External tables are really very useful for handling data loads like this.
用于将 CSV 类型数据文件加载到数据库中的“标准”Oracle 工具是 SQLLoader。它通常用于插入记录,但如果您愿意,可以将控制文件配置为运行更新。
它不是世界上最容易使用的工具(读“这是一个痛苦的屁股”),但它是标准工具包的一部分,并且它可以完成工作。
The "standard" Oracle tool for loading CSV-type datafiles into the database is SQLLoader. It's normally used to insert records, but the control file can be configured to run an update instead, if that's what you so desire.
It's not the easiest tool in the world to use (read "it's a pain in the arse"), but it's part of the standard toolkit, and it does the job.
另一种选择是逐行读入文件,使用 REGEXP_REPLACE 等解析行中的字段,然后更新相应的行。您可以像下面这样解析逗号分隔的字符串:
如果您的站点不允许使用外部表,这非常有用。
分享并享受。
Another option is to read in the file line-by-line, parse out the fields in the line using something like REGEXP_REPLACE, and update the appropriate rows. You can parse a comma-delimited string like the following:
This is useful if your site doesn't allow the use of external tables.
Share and enjoy.
使用 SQL*Loader
load_csv.ctl 文件
,其中 /path/to/mydata.csv 是您需要加载的 CSV 文件的路径,在 Windows 上
例如C:\data\mydata.csv。表名是mydatatable。这些列按照它们在 csv 文件最后一行中出现的顺序列出。
除非您有大量数据,否则这是最容易维护的获取数据的方法
如果需要定期加载数据,可以将其写入 shell 脚本并由 CRON 在 U*NX 系统上运行。
Use SQL*Loader
The load_csv.ctl file
where /path/to/mydata.csv is the path to the CSV file that you need to load, on Windows something
like C:\data\mydata.csv. The tablename is mydatatable. The columns are listed in order that they apear in the csv file on the last line.
Unless you have a very large volume of data this is the easiest to maintain way of getting the data
in. If the data needs to be loaded on a regular basis this can be worked into a shell script and run by CRON on a U*NX system.
首先创建一个表并将所有 CSV 数据放入该表中,然后写入游标以使用 ID
ex
对应的 CSV 创建的表更新您的 oracle 表,然后将 CSV 数据导入到该表中。
现在写一个光标
我认为它会起作用
First create a table and put all your CSV data into that table,and then write a cursor to update your oracle table with the CSV created table corresponding to IDs
ex
and then import your CSV data into this table.
now write a cursor
i think it will work