[英]Export from Oracle SQL to CSV from Command
I have a script that exports data (including CLOBs) from Oracle SQL to CSV via the command line. 我有一个脚本,可通过命令行将数据(包括CLOB)从Oracle SQL导出到CSV。 When I execute the script then view the data via Python, I get the correct number of columns for each row, along with the proper data. 当我执行脚本然后通过Python查看数据时,我获得了每一行的正确列数以及正确的数据。 However, there are extra spaces in every cell. 但是,每个单元格中都有多余的空间。 My approach: 我的方法:
file.sql : file.sql :
spool "/home/username/file.csv";
SELECT /*csv*/ '"'||COLUMN_1||'"',
'"'||COLUMN_2||'"',
'"'||DBMS_LOB.substr(COLUMN_3,3000)||'"',
...
'"'||COLUMN_N||'"'
FROM TABLE;
spool off;
file.sh : file.sh :
export ORACLE_SID=MYSID
sqlplus / as sysdba <<EOF
conn myUser/myPass
set heading off
set termout off
set trimout off
set newpage none
set pages 0
set feedback off
set echo off
set verify off
set trimspool on
set sqlprompt ""
set colsep ","
set linesize 32767
@/home/username/file.sql
exit
EOF
When I attempt to trim spaces via concatenation, the entire thing falls apart (unequal column lengths, breaks where there should be none, etc.): 当我尝试通过串联修剪空间时,整个对象都崩溃了(不相等的列长,在不应有的地方中断等等):
Modified file.sql : 修改的file.sql :
spool "/home/username/file.csv";
SELECT /*csv*/ '"'||COLUMN_1||'"'||','
'"'||COLUMN_2||'"'||','
'"'||DBMS_LOB.substr(COLUMN_3,3000)||'"'||','
...
'"'||COLUMN_N||'"'
FROM TABLE;
spool off;
What am I doing wrong here? 我在这里做错了什么? I've also attempted a few other concatenation approaches, like using ||'","'||
我还尝试了其他一些串联方法,例如使用||'","'||
instead, but that didn't seem to work either. 相反,但这似乎也不起作用。
Check this out: from this post: https://community.oracle.com/thread/2357292 检查一下:从这篇文章: https : //community.oracle.com/thread/2357292
Give each selected column an alias name. 给每个选定的列起一个别名。 It then worked for me. 然后它为我工作。
SELECT /*csv*/ '"'||COLUMN_1||'"' COL1,
'"'||COLUMN_2||'"' COL2,
'"'||DBMS_LOB.substr(COLUMN_3,3000)||'"' COL3,
...
'"'||COLUMN_N||'"' COLN
FROM TABLE;
EDIT: Added my example from running in Toad. 编辑:从我在Toad中运行添加了我的示例。
In Toad, I run this: 在Toad中,我运行以下命令:
set heading off
set termout off
set trimout off
set newpage none
set pages 0
set feedback off
set echo off
set verify off
set trimspool on
set trimout on
set tab off
set sqlprompt ""
set colsep ","
set linesize 32767
spool c:\temp\efs.txt;
SELECT '"'||'A'||'"',
'"'||'B'||'"',
'"'||'C'||'"'
FROM dual;
spool off;
and get: 并得到:
"A" ,"B" ,"C"
When I add the column aliases: 当我添加列别名时:
SELECT '"'||'A'||'"' a,
'"'||'B'||'"' b,
'"'||'C'||'"' c
FROM dual;
I get: 我得到:
"A","B","C"
See this thread for another method and explanation (which is how we do it): https://community.oracle.com/thread/1081068 请参阅此线程以获取另一种方法和说明(这是我们的操作方法): https : //community.oracle.com/thread/1081068
How about: 怎么样:
select col1 || ',' || col2 || ',' || col3 from tab1;
or 要么
select '"' || col1 || '",' || '"' || col2 || '",' || '"' || finalcol || '"' from tab1;
eg 例如
SQL> select '"' || AD_ID || '",' || '"' || AD_SOURCETEXT || '",' || '"' || PRODUCT_ID || '"' from pm.print_media where rownum < 4;
'"'||AD_ID||'",'||'"'||AD_SOURCETEXT||'",'||'"'||PRODUCT_ID||'"'
--------------------------------------------------------------------------------
"11001","TIGER2 Mousepad
Product Number: 2056 Price: $8 Today's Sale Price
"12001","TIGER2 Mousepad
Product Number: 2056 Price: $8 Today's Sale Price
"13001","TIGER2 3106 Keyboard
Product Number: 3106
* KB 101/EN keyboard is a sta
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.