SQL*Plus 忽略 colsep 选项
SQL*Plus ignores colsep option
所以我们试图将 oracle 数据库的内容转储到一个 .CSV 文件但是当执行以下命令时
set colsep ',' echo off newpage 0 space 0 pagesize 0 linesize 3000 feed off head off trimspool on
spool C:\Users\whocares\Desktop\test.CSV
select ID, LOCATION_ID from club;
spool off
输出如下所示:
...
17499 902
17500 902
17501 902
17502 902
17503 902
17504 902
17505 902
17506 902
17507 902
17508 902
17509 902
...
而sqlplus似乎完全忽略了set colsep ','
选项。
要获得有效的 csv
输出,我们需要输出如下所示:
...
17499,902
17500,902
17501,902
17502,902
17503,902
17504,902
17505,902
17506,902
17507,902
17508,902
17509,902
...
那么如何正确使用set colsep
选项呢?
我们没有太多使用 sqlplus 的经验,并且出于某种原因 other Whosebug solutions 似乎对我们不起作用。
SET COLSEP
替换了 SET SPACE
,从 Oracle 9.2 开始就是这样(参见 Obsolete SQL*Plus Commands)。
如果您同时使用它们,则没有分隔符:
SQL> set colsep ','
SQL> select * from dept where rownum = 1;
DEPTNO,DNAME ,LOC
----------,--------------------,--------------------
10,ACCOUNTING ,NEW YORK
SQL> set space 0
SQL> select * from dept where rownum = 1;
DEPTNODNAME LOC
--------------------------------------------------
10ACCOUNTING NEW YORK
SQL> set space 1
SQL> select * from dept where rownum = 1;
DEPTNO DNAME LOC
---------- -------------------- --------------------
10 ACCOUNTING NEW YORK
SQL>
所以 - 删除 SET SPACE
.
另一种选择是连接 列。是的,这是一项乏味的工作,但它确实有效,例如
SQL> select deptno ||','|| dname ||','|| loc from dept;
DEPTNO||','||DNAME||','||LOC
---------------------------------------------------------------
10,ACCOUNTING,NEW YORK
20,RESEARCH,DALLAS
30,SALES,CHICAGO
40,OPERATIONS,BOSTON
SQL>
如果涉及到很多表,你可以写一个查询,它会为你写一个查询:
SQL> select 'select ' || listagg(column_name, '||'',''||') within group (order by column_id) ||
2 ' from ' || table_name result
3 from user_tab_columns
4 where table_name = 'DEPT'
5 group by table_name;
RESULT
--------------------------------------------------------------------------------
select DEPTNO||','||DNAME||','||LOC from DEPT
SQL>
现在 copy/paste 结果 和 运行 结果。
所以我们试图将 oracle 数据库的内容转储到一个 .CSV 文件但是当执行以下命令时
set colsep ',' echo off newpage 0 space 0 pagesize 0 linesize 3000 feed off head off trimspool on
spool C:\Users\whocares\Desktop\test.CSV
select ID, LOCATION_ID from club;
spool off
输出如下所示:
...
17499 902
17500 902
17501 902
17502 902
17503 902
17504 902
17505 902
17506 902
17507 902
17508 902
17509 902
...
而sqlplus似乎完全忽略了set colsep ','
选项。
要获得有效的 csv
输出,我们需要输出如下所示:
...
17499,902
17500,902
17501,902
17502,902
17503,902
17504,902
17505,902
17506,902
17507,902
17508,902
17509,902
...
那么如何正确使用set colsep
选项呢?
我们没有太多使用 sqlplus 的经验,并且出于某种原因 other Whosebug solutions 似乎对我们不起作用。
SET COLSEP
替换了 SET SPACE
,从 Oracle 9.2 开始就是这样(参见 Obsolete SQL*Plus Commands)。
如果您同时使用它们,则没有分隔符:
SQL> set colsep ','
SQL> select * from dept where rownum = 1;
DEPTNO,DNAME ,LOC
----------,--------------------,--------------------
10,ACCOUNTING ,NEW YORK
SQL> set space 0
SQL> select * from dept where rownum = 1;
DEPTNODNAME LOC
--------------------------------------------------
10ACCOUNTING NEW YORK
SQL> set space 1
SQL> select * from dept where rownum = 1;
DEPTNO DNAME LOC
---------- -------------------- --------------------
10 ACCOUNTING NEW YORK
SQL>
所以 - 删除 SET SPACE
.
另一种选择是连接 列。是的,这是一项乏味的工作,但它确实有效,例如
SQL> select deptno ||','|| dname ||','|| loc from dept;
DEPTNO||','||DNAME||','||LOC
---------------------------------------------------------------
10,ACCOUNTING,NEW YORK
20,RESEARCH,DALLAS
30,SALES,CHICAGO
40,OPERATIONS,BOSTON
SQL>
如果涉及到很多表,你可以写一个查询,它会为你写一个查询:
SQL> select 'select ' || listagg(column_name, '||'',''||') within group (order by column_id) ||
2 ' from ' || table_name result
3 from user_tab_columns
4 where table_name = 'DEPT'
5 group by table_name;
RESULT
--------------------------------------------------------------------------------
select DEPTNO||','||DNAME||','||LOC from DEPT
SQL>
现在 copy/paste 结果 和 运行 结果。