删除 SQL *Plus SPOOL 中的初始和尾随空白行(SET MARKUP CSV)

问题描述 投票:0回答:1

我正在尝试使用 SQL *Plus 将简单的 SELECT 导出到 .csv 文件。如果有更好的方法来做到这一点,我愿意,但我需要使用 SQL 并以 .csv 结尾,并且能够单击某些内容来运行整个过程,而无需手动步骤。

此脚本的输出以空行开始和结束(x'0D' 和 x'0A')。

有什么方法可以删除它们,尤其是第一个空行?我可以在流程的下一步中执行此操作,但它有可能引入错误,并且似乎应该有一种简单的方法可以从 SQL *Plus 执行此操作。

这是我的尝试:

SET MARKUP CSV ON DELIMITER "," QUOTE OFF
SET HEADING ON
SET SHOWMODE OFF
SET FEEDBACK OFF
SET VERIFY OFF
SET TERMOUT OFF
SET NEWPAGE NONE
SET PAGESIZE 0
TTITLE OFF
set echo off
set trimspool on
set trimout on
SPOOL "C:\Users\me\Downloads\sql-export2.csv" REPLACE
SELECT * 
FROM my_table;
SPOOL OFF;
EXIT;

我认为不需要这些行(有些行不应该与

SET MARKUP CSV
一起使用),但试图展示我尝试过的所有行不通的内容。

SET NEWPAGE NONE
SET PAGESIZE 0
TTITLE OFF
set echo off
set trimspool on
set trimout on

我已将其保存到 .sql 文件中,我使用

sqlplus
命令从命令提示符运行该文件。 SQL *Plus 版本是 12.2.0.1.0,因为我的驱动程序很旧。如果版本 19 可能有帮助,我可以尝试解决我的安装程序问题。

我是 SQL *Plus 新手,不太了解 ; 的用法字符,所以如果我在那里做错了什么,请纠正我的语法。

更新: 我确实确认前导空格是由

SET HEADING ON
引入的。我怀疑这是我无能为力的标题“特征”。

oracle sqlplus
1个回答
0
投票

使用 SQLcl 生成 CSV 文件比 SQLPlus 更容易。 99% 的 SQLPlus 命令都可以与 SQLcl 一起使用。要在 SQLcl 中生成 CSV 文件,您仍然可以使用

SPOOL
创建文件,
SET FEEDBACK OFF
来防止“选择 X 行”。查询后显示的消息,以及
SET SQLFORMAT CSV
轻松从查询生成 CSV 输出。使用此格式,查询结果的第一行将是 CSV 的列标题。

这是它的外观示例

SQLcl: Release 23.3 Production on Mon Jan 08 11:59:56 2024

Copyright (c) 1982, 2024, Oracle.  All rights reserved.

Last Successful login time: Mon Jan 08 2024 11:59:58 -05:00

Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.18.0.0.0

SQL> set feedback off
SQL> set sqlformat csv
SQL> spool test.csv
SQL> select * from dba_objects where rownum <= 10;
"OWNER","OBJECT_NAME","SUBOBJECT_NAME","OBJECT_ID","DATA_OBJECT_ID","OBJECT_TYPE","CREATED","LAST_DDL_TIME","TIMESTAMP","STATUS","TEMPORARY","GENERATED","SECONDARY","NAMESPACE","EDITION_NAME","SHARING","EDITIONABLE","ORACLE_MAINTAINED","APPLICATION","DEFAULT_COLLATION","DUPLICATED","SHARDED","CREATED_APPID","CREATED_VSNID","MODIFIED_APPID","MODIFIED_VSNID"
"SYS","I_FILE#_BLOCK#","",9,9,"INDEX",10-FEB-20,10-FEB-20,"2020-02-10:10:26:41","VALID","N","N","N",4,"","NONE","","Y","N","","N","N",,,,
"SYS","I_OBJ3","",38,38,"INDEX",10-FEB-20,10-FEB-20,"2020-02-10:10:26:41","VALID","N","N","N",4,"","NONE","","Y","N","","N","N",,,,
"SYS","I_TS1","",45,45,"INDEX",10-FEB-20,10-FEB-20,"2020-02-10:10:26:41","VALID","N","N","N",4,"","NONE","","Y","N","","N","N",,,,
"SYS","I_CON1","",51,51,"INDEX",10-FEB-20,10-FEB-20,"2020-02-10:10:26:41","VALID","N","N","N",4,"","NONE","","Y","N","","N","N",,,,
"SYS","IND$","",19,2,"TABLE",10-FEB-20,22-JUL-23,"2020-02-10:10:26:41","VALID","N","N","N",1,"","METADATA LINK","","Y","N","USING_NLS_COMP","N","N",,,,
"SYS","CDEF$","",31,29,"TABLE",10-FEB-20,10-FEB-20,"2020-02-10:10:26:41","VALID","N","N","N",1,"","METADATA LINK","","Y","N","USING_NLS_COMP","N","N",,,,
"SYS","C_TS#","",6,6,"CLUSTER",10-FEB-20,10-FEB-20,"2020-02-10:10:26:41","VALID","N","N","N",5,"","METADATA LINK","","Y","N","","N","N",,,,
"SYS","I_CCOL2","",58,58,"INDEX",10-FEB-20,10-FEB-20,"2020-02-10:10:26:41","VALID","N","N","N",4,"","NONE","","Y","N","","N","N",,,,
"SYS","I_PROXY_DATA$","",24,24,"INDEX",10-FEB-20,10-FEB-20,"2020-02-10:10:26:41","VALID","N","N","N",4,"","NONE","","Y","N","","N","N",,,,
"SYS","I_CDEF4","",56,56,"INDEX",10-FEB-20,10-FEB-20,"2020-02-10:10:26:41","VALID","N","N","N",4,"","NONE","","Y","N","","N","N",,,,
SQL> spool off
SQL> exit
Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.18.0.0.0
© www.soinside.com 2019 - 2024. All rights reserved.