首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >sqlplus和spool选项

sqlplus和spool选项
EN

Stack Overflow用户
提问于 2016-08-26 23:42:40
回答 2查看 4K关注 0票数 1

我有一个运行SQL*Plus命令的批处理脚本,并且我正在将输出假脱机到CSV文件。所有输出只有一列"count“。有没有办法将文本添加到结果中?

代码语言:javascript
复制
"The_current_valueOf Query1" "Count1"
"The_current_valueOf Query2" "Count2"

任何帮助/建议都将不胜感激。

代码语言:javascript
复制
set colsep ,
set pagesize 0
set trimspool on
set headsep off
set Newpage none

spool D:\TRHMIBS\TRH\TRHStats.csv
select count(*) from host.ufm where insert_time between trunc(sysdate) and sysdate
/
spool D:\TRHMIBS\TRH\TRHStats.csv APPEND

select count(*) from host.amendment where insert_time between trunc(sysdate) and sysdate and msg_source = 'AUDIT'
/
spool D:\TRHMIBS\TRH\TRHStats.csv APPEND
select count(*) from host.ufm_amendment where insert_time between trunc(sysdate) and sysdate and msg_source = 'DAS' and ext_token is null
/
spool off; 

exit;
EN

回答 2

Stack Overflow用户

发布于 2016-08-27 01:41:36

您可以只选择消息作为硬编码的文字。例如:

代码语言:javascript
复制
spool D:\TRHMIBS\TRH\TRHStats.csv
SELECT 'The_current_valueOf Query1:', COUNT(*)
FROM   host.ufm
WHERE  insert_time BETWEEN TRUNC(sysdate) AND sysdate
/
票数 2
EN

Stack Overflow用户

发布于 2016-08-27 01:42:06

这与假脱机无关。您的第二个和第三个spool命令是多余的-所有内容都会假脱机到打开的文件中,直到您将其关闭。但这是一个次要问题。

您可以使用列表达式将另一列添加到输出中,列表达式可以是字符串文字。因此,您可以将第一个查询更改为:

代码语言:javascript
复制
select 'The_current_valueOf Query1', count(*)
from host.ufm
where insert_time between trunc(sysdate) and sysdate

然后对其他两个查询执行相同的操作。

由于您希望将其设置为CSV,因此可以不使用colsep,只需在列中嵌入一个逗号,您可以使用连接来完成此操作:

代码语言:javascript
复制
select 'The_current_valueOf Query1,' || count(*)
from host.ufm
where insert_time between trunc(sysdate) and sysdate

而且,您可以将它们合并在一起,而不是运行三个单独的查询;这不会节省处理时间,但意味着所有输出都在一起。

代码语言:javascript
复制
select 'The_current_valueOf Query1,' || count(*)
from host.ufm
where insert_time between trunc(sysdate) and sysdate
union all
select 'The_current_valueOf Query1,' || count(*)
from host.amendment
where insert_time between trunc(sysdate) and sysdate
union all
...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39170021

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档