SQLPlus导出到CSV(输出格式问题)

发布于 2024-10-14 17:43:53 字数 1034 浏览 10 评论 0原文

我遇到了一个接口脚本的问题,该脚本应该将 ORACLE 数据库的某些表的内容导出到 CSV 文件中,然后将这些 CSV 导入到 MYSQL 数据库中。

STEP1:SQLPlus 导出到 CSV

set headsep off  
set heading off
set term off
set echo off
SET  RECSEPCHAR \n
set pagesize 0
set linesize 0
trimspool on
SET FEEDBACK OFF
spool as_ex_feature.csv
select '"AS'||'"|"'||feature_group||'"|"'||feature_desc||'"|"
    ||feature_order||'"|"'||prod_code||'"'
from MYVIEW WHERE MYCONDITIONS;
spool off;

->这一步正在生成 CSV 文件,但格式似乎不正确,因为我可以在输出中找到一些回车符。 此外,您还会在第 2 步中看到,我们定义了一个“ENCLOSED BY”值,我怎样才能将该值包含在导出格式中(现在似乎不是这种情况)。

第 2 步:MYSQL 加载

LOAD DATA INFILE 'mycsvfile' REPLACE INTO TABLE `mt_feature` 
FIELDS TERMINATED BY '|'
ENCLOSED BY '"' 
ESCAPED BY '\\'
LINES TERMINATED BY '\n';

由于某些技术原因,该脚本必须重新构建,并且 Mysql 部分没有更改,并且可以通过正确的 CSV 文件导入来正常工作。

这个问题似乎来自 SQLPlus 导出,我需要承认我对此了解不多。也许我应该使用另一种方法来生成这些文件?

如果您需要其他详细信息,请告诉我,我感觉很盲目...


在 oracle 10g、Linux、Mysql 4.x 上运行的脚本

谢谢!

I'm facing an issue with an interface script, supposed to export the content of some table of an ORACLE database into CSV file, which is then followed by an import of those CSV into a MYSQL database.

STEP1: SQLPlus export to CSV

set headsep off  
set heading off
set term off
set echo off
SET  RECSEPCHAR \n
set pagesize 0
set linesize 0
trimspool on
SET FEEDBACK OFF
spool as_ex_feature.csv
select '"AS'||'"|"'||feature_group||'"|"'||feature_desc||'"|"
    ||feature_order||'"|"'||prod_code||'"'
from MYVIEW WHERE MYCONDITIONS;
spool off;

-> this step is generating the CSV file, but the format seems incorrect, as I can find some carriage return in the output.
Also you'll see in STEP2 that we define an "ENCLOSED BY" value how could I get that one included in the export format (doesn't seem to be the case right now).

STEP 2: MYSQL load

LOAD DATA INFILE 'mycsvfile' REPLACE INTO TABLE `mt_feature` 
FIELDS TERMINATED BY '|'
ENCLOSED BY '"' 
ESCAPED BY '\\'
LINES TERMINATED BY '\n';

This script had to be rebuilt for some technical reasons and the Mysql part had not been changed and is working fine with a proper CSV file to import.

The issue seem to be coming from that SQLPlus export, where I need to admit I don't have much knowledge on. Maybe I should use another method to get those files generated?

Please let me know if you need additional details, I feel blind...


Script running on oracle 10g, Linux, Mysql 4.x

Thanks!

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

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

发布评论

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

评论(2

洋洋洒洒 2024-10-21 17:43:53

SET LINESIZE 0 无效,该值必须在 1 到 32767 之间。所以我想它会以默认行长度(即 80)包装内容,除非您已经将其设置为一个 glogin 脚本。

SET LINESIZE 0 isn't valid, the value has to be between 1 and 32767. So I imagine it's wrapping the content at the default line length, which is 80 unless you've already got it set in a glogin script.

︶ ̄淡然 2024-10-21 17:43:53

如果您在任何代码行前面加上(至少)四个空格,那么它将被正确格式化,例如

select "AS'||'"|"'||
    feature_group||'"|"'||
    feature_desc||'"|"'||
    feature_order||'"|"'||
    prod_code||'"' 
from MYVIEW 
WHERE MYCONDITIONS;

听起来您可能需要替换存储数据中的任何嵌入换行符......

SELECT "AS'||'"|"'||
    TRANSLATE(feature_group, CHR(10), '\\n') ||'"|"'||
(etc).

而且我不确定关于将线大小设置为 0。

If you prefix any lines of code with (at least) four spaces in SO then it'll be formatted correctly, e.g.

select "AS'||'"|"'||
    feature_group||'"|"'||
    feature_desc||'"|"'||
    feature_order||'"|"'||
    prod_code||'"' 
from MYVIEW 
WHERE MYCONDITIONS;

Sounds like you may need to replace any embedded newline chars in the stored data....

SELECT "AS'||'"|"'||
    TRANSLATE(feature_group, CHR(10), '\\n') ||'"|"'||
(etc).

And I'm not sure about setting the linesize to 0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文