sqlplus和假脱机选项

时间:2016-08-26 15:42:40

标签: oracle sqlplus spool

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

"The_current_valueOf Query1" "Count1"
"The_current_valueOf Query2" "Count2"

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

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;

2 个答案:

答案 0 :(得分:2)

您可以选择将消息选为硬编码文字。 E.g:

spool D:\TRHMIBS\TRH\TRHStats.csv
SELECT 'The_current_valueOf Query1:', COUNT(*)
FROM   host.ufm
WHERE  insert_time BETWEEN TRUNC(sysdate) AND sysdate
/

答案 1 :(得分:1)

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

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

select 'The_current_valueOf Query1', count(*)
from host.ufm
where insert_time between trunc(sysdate) and sysdate

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

由于您希望将其作为CSV格式,因此您可以单独留下colsep并且只有一个嵌入了逗号的列,您可以使用连接执行此操作:

select 'The_current_valueOf Query1,' || count(*)
from host.ufm
where insert_time between trunc(sysdate) and sysdate

而不是运行三个单独的查询,你可以将它们联合在一起;不会节省处理,但意味着输出都汇集在一起​​。

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
...