SQL 如何使用 SQLPLUS 假脱机到 CSV 格式的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/643137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 01:24:46  来源:igfitidea点击:

How do I spool to a CSV formatted file using SQLPLUS?

sqloraclecsvsqlplus

提问by Daniel C. Sobral

I want to extract some queries to a CSV output format. Unfortunately, I can't use any fancy SQL client or any language to do it. I must use SQLPLUS.

我想将一些查询提取为 CSV 输出格式。不幸的是,我不能使用任何花哨的 SQL 客户端或任何语言来做到这一点。我必须使用 SQLPLUS。

How do I do it?

我该怎么做?

采纳答案by BobC

If you are using 12.2, you can simply say

如果您使用的是 12.2,您可以简单地说

set markup csv on
spool myfile.csv

回答by Gabe

You could also use the following, although it does introduce spaces between fields.

您也可以使用以下内容,尽管它确实在字段之间引入了空格。

set colsep ,     -- separate columns with a comma
set pagesize 0   -- No header rows
set trimspool on -- remove trailing blanks
set headsep off  -- this may or may not be useful...depends on your headings.
set linesize X   -- X should be the sum of the column widths
set numw X       -- X should be the length you want for numbers (avoid scientific notation on IDs)

spool myfile.csv

select table_name, tablespace_name 
  from all_tables
 where owner = 'SYS'
   and tablespace_name is not null;

Output will be like:

输出将类似于:

    TABLE_PRIVILEGE_MAP           ,SYSTEM                        
    SYSTEM_PRIVILEGE_MAP          ,SYSTEM                        
    STMT_AUDIT_OPTION_MAP         ,SYSTEM                        
    DUAL                          ,SYSTEM 
...

This would be a lot less tedious than typing out all of the fields and concatenating them with the commas. You could follow up with a simple sed script to remove whitespace that appears before a comma, if you wanted.

这比输入所有字段并用逗号连接它们要少得多。如果需要,您可以使用一个简单的 sed 脚本来删除出现在逗号之前的空格。

Something like this might work...(my sed skills are very rusty, so this will likely need work)

像这样的东西可能会起作用......(我的 sed 技能非常生疏,所以这可能需要工作)

sed 's/\s+,/,/' myfile.csv 

回答by Hallison Batista

I use this command for scripts which extracts data for dimensional tables (DW). So, I use the following syntax:

我将此命令用于提取维度表 (DW) 数据的脚本。所以,我使用以下语法:

set colsep '|'
set echo off
set feedback off
set linesize 1000
set pagesize 0
set sqlprompt ''
set trimspool on
set headsep off

spool output.dat

select '|', <table>.*, '|'
  from <table>
where <conditions>

spool off

And works. I don't use sed for format the output file.

和作品。我不使用 sed 来格式化输出文件。

回答by Karlos

I see a similar problem...

我看到了类似的问题...

I need to spool CSV file from SQLPLUS, but the output has 250 columns.

我需要从 SQLPLUS 假脱机 CSV 文件,但输出有 250 列。

What I did to avoid annoying SQLPLUS output formatting:

我做了什么来避免烦人的 SQLPLUS 输出格式:

set linesize 9999
set pagesize 50000
spool myfile.csv
select x
from
(
select col1||';'||col2||';'||col3||';'||col4||';'||col5||';'||col6||';'||col7||';'||col8||';'||col9||';'||col10||';'||col11||';'||col12||';'||col13||';'||col14||';'||col15||';'||col16||';'||col17||';'||col18||';'||col19||';'||col20||';'||col21||';'||col22||';'||col23||';'||col24||';'||col25||';'||col26||';'||col27||';'||col28||';'||col29||';'||col30 as x
from (  
      ...  here is the "core" select
     )
);
spool off

the problem is you will lose column header names...

问题是您将丢失列标题名称...

you can add this:

你可以添加这个:

set heading off
spool myfile.csv
select col1_name||';'||col2_name||';'||col3_name||';'||col4_name||';'||col5_name||';'||col6_name||';'||col7_name||';'||col8_name||';'||col9_name||';'||col10_name||';'||col11_name||';'||col12_name||';'||col13_name||';'||col14_name||';'||col15_name||';'||col16_name||';'||col17_name||';'||col18_name||';'||col19_name||';'||col20_name||';'||col21_name||';'||col22_name||';'||col23_name||';'||col24_name||';'||col25_name||';'||col26_name||';'||col27_name||';'||col28_name||';'||col29_name||';'||col30_name from dual;

select x
from
(
select col1||';'||col2||';'||col3||';'||col4||';'||col5||';'||col6||';'||col7||';'||col8||';'||col9||';'||col10||';'||col11||';'||col12||';'||col13||';'||col14||';'||col15||';'||col16||';'||col17||';'||col18||';'||col19||';'||col20||';'||col21||';'||col22||';'||col23||';'||col24||';'||col25||';'||col26||';'||col27||';'||col28||';'||col29||';'||col30 as x
from (  
      ...  here is the "core" select
     )
);
spool off

I know it`s kinda hardcore, but it works for me...

我知道它有点硬核,但它对我有用......

回答by Lalit Kumar B

With newer versions of client tools, there are multiple options to format the query output. The rest is to spool it to a file or save the output as a file depending on the client tool. Here are few of the ways:

对于较新版本的客户端工具,有多种选项可以格式化查询输出。剩下的就是根据客户端工具将其假脱机到文件或将输出保存为文件。以下是几种方法:

  • SQL*Plus
  • SQL*Plus

Using the SQL*Plus commands you could format to get your desired output. Use SPOOLto spool the output to a file.

使用 SQL*Plus 命令,您可以格式化以获得所需的输出。使用SPOOL将输出假脱机到一个文件。

For example,

例如,

SQL> SET colsep ,
SQL> SET pagesize 20
SQL> SET trimspool ON
SQL> SET linesize 200
SQL> SELECT * FROM scott.emp;

     EMPNO,ENAME     ,JOB      ,       MGR,HIREDATE ,       SAL,      COMM,    DEPTNO
----------,----------,---------,----------,---------,----------,----------,----------
      7369,SMITH     ,CLERK    ,      7902,17-DEC-80,       800,          ,        20
      7499,ALLEN     ,SALESMAN ,      7698,20-FEB-81,      1600,       300,        30
      7521,WARD      ,SALESMAN ,      7698,22-FEB-81,      1250,       500,        30
      7566,JONES     ,MANAGER  ,      7839,02-APR-81,      2975,          ,        20
      7654,MARTIN    ,SALESMAN ,      7698,28-SEP-81,      1250,      1400,        30
      7698,BLAKE     ,MANAGER  ,      7839,01-MAY-81,      2850,          ,        30
      7782,CLARK     ,MANAGER  ,      7839,09-JUN-81,      2450,          ,        10
      7788,SCOTT     ,ANALYST  ,      7566,09-DEC-82,      3000,          ,        20
      7839,KING      ,PRESIDENT,          ,17-NOV-81,      5000,          ,        10
      7844,TURNER    ,SALESMAN ,      7698,08-SEP-81,      1500,          ,        30
      7876,ADAMS     ,CLERK    ,      7788,12-JAN-83,      1100,          ,        20
      7900,JAMES     ,CLERK    ,      7698,03-DEC-81,       950,          ,        30
      7902,FORD      ,ANALYST  ,      7566,03-DEC-81,      3000,          ,        20
      7934,MILLER    ,CLERK    ,      7782,23-JAN-82,      1300,          ,        10

14 rows selected.

SQL>
  • SQL Developer Version pre 4.1
  • SQL Developer 4.1 之前的版本

Alternatively, you could use the new /*csv*/hintin SQL Developer.

或者,您可以使用新的提示SQL开发人员/*csv*/

/*csv*/

For example, in my SQL Developer Version 3.2.20.10:

例如,在我的SQL Developer 版本 3.2.20.10 中

enter image description here

在此处输入图片说明

Now you could save the output into a file.

现在您可以将输出保存到文件中。

  • SQL Developer Version 4.1
  • SQL 开发人员版本 4.1

New in SQL Developer version 4.1, use the following just like sqlplus command and run as script. No need of the hint in the query.

SQL Developer 4.1 版中的新功能,使用下面的就像 sqlplus 命令一样并作为脚本运行。不需要查询中的提示。

SET SQLFORMAT csv

Now you could save the output into a file.

现在您可以将输出保存到文件中。

回答by Doc

I know this is an old thread, however I noticed that no one mentioned the underline option, which can remove the underlines under the column headings.

我知道这是一个旧线程,但是我注意到没有人提到下划线选项,它可以删除列标题下的下划线。

set pagesize 50000--50k is the max as of 12c
set linesize 10000   
set trimspool on  --remove trailing blankspaces
set underline off --remove the dashes/underlines under the col headers
set colsep ~

select * from DW_TMC_PROJECT_VW;

回答by Tony Andrews

It's crude, but:

这很粗糙,但是:

set pagesize 0 linesize 500 trimspool on feedback off echo off

select '"' || empno || '","' || ename || '","' || deptno || '"' as text
from emp

spool emp.csv
/
spool off

回答by ConcernedOfTunbridgeWells

You can explicitly format the query to produce a delimited string with something along the lines of:

您可以显式格式化查询以生成带有以下内容的分隔字符串:

select '"'||foo||'","'||bar||'"'
  from tab

And set up the output options as appropriate. As an option, the COLSEP variable on SQLPlus will let you produce delimited files without having to explicitly generate a string with the fields concatenated together. However, you will have to put quotes around strings on any columns that might contain embedded comma characters.

并根据需要设置输出选项。作为一个选项,SQLPlus 上的 COLSEP 变量将允许您生成分隔文件,而无需显式生成将字段连接在一起的字符串。但是,您必须在可能包含嵌入逗号字符的任何列的字符串周围加上引号。

回答by CC.

prefer to use "set colsep" in sqlplus prompt instead of editing col name one by one. Use sed to edit the output file.

更喜欢在 sqlplus 提示中使用“set colsep”,而不是一一编辑 col 名称。使用 sed 编辑输出文件。

set colsep '","'     -- separate columns with a comma
sed 's/^/"/;s/$/"/;s/\s *"/"/g;s/"\s */"/g' $outfile > $outfile.csv

回答by René Nyffenegger

I have once written a little SQL*Plus script that uses dbms_sqland dbms_outputto create a csv (actually an ssv). You can find it on my githup repository.

我曾经写过一个小的 SQL*Plus 脚本,它使用dbms_sqldbms_output创建一个 csv(实际上是一个 ssv)。您可以在我的 gitup 存储库中找到它。