使用sql*plus输出xls和html两种格式文件

   我们可以在SQL*PLUS中手工运行AWR,ASH的脚本生成html报表,下面就介绍一下通过SQL*PLUS输出html,xls两种格式的文件,构建更友好的输出,满足用户多样性需求。

在SQLPLUS中有个命令(具体可以参考官方文档SQLPLUS部分)
SET MARK[UP] HTML [ON | OFF] [HEAD text] [BODY text] [TABLE text] [ENTMAP {ON | OFF}] [SPOOL {ON | OFF}] [PRE[FORMAT] {ON | OFF}]
一:首先在SQLPLUS中设置
set mark html on spool on  entmap off  pre off
这样设置过后,利用spool 导出为html,SQLPLUS将会自动的为我们创建HTML格式,
注意:如果设置pre 为on,那么输出的不是HTML格式,默认为off,
entmap 默认为on ,它会将>换成HTML中的&gt来显示,所以我将其设置为off


二:为了格式化输出,我们需要对输出内容格式化
set echo off                         这样设置之后不会在HTML报表中显示执行过的SQL语句
set feedback off                  这样设置过后不会在HTML报表中显示已经处理多少行
set heading on                   设置标题显示
set termout off                    关闭在屏幕上的输出,这样可以加快spool执行速度(只有在调用脚本时该选项才能生效)
set linesize 200                 设置行宽度为120
set pagesize 1000             设置一页显示1000行
set trimout off                    去掉 每行后面多余的空格


三:通过调用脚本生成所需的html或者xls文件

sqlplus / as sysdba

SQL*Plus: Release 11.2.0.3.0 Production on Mon May 18 10:45:58 2015

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


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Real Application Clusters, OLAP, Data Mining
and Real Application Testing options

SQL> @get_tablespace_info.sql
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Real Application Clusters, OLAP, Data Mining
and Real Application Testing options

more get_tablespace_info.sql

SET MARKUP HTML ON SPOOL ON pre off entmap off
SET ECHO OFF
SET TERMOUT OFF
SET TRIMOUT OFF
set feedback off
set heading on
set linesize 200
set pagesize 10000
col tablespace_name format a15
col total_space format a10
col free_space format a10
col used_space format a10
col used_rate format 99.99
spool /home/oracle/tablespace_use_info.html
select a.tablespace_name,a.total_space_Mb||'m' total_space,b.free_space_Mb||'m'
free_space,a.total_space_Mb-b.free_space_Mb||'m' used_space,
(1-(b.free_space_Mb/a.total_space_Mb))*100 used_rate,a.total_blocks,b.free_blocks from                    
(select tablespace_name,sum(bytes)/1024/1024 total_space_Mb,sum(blocks) total_blocks from dba_data_files
group by tablespace_name) a,
(select tablespace_name, sum((bytes)/1024/1024) free_space_Mb,sum(blocks) free_blocks from dba_free_space
group by tablespace_name) b
where a.tablespace_name=b.tablespace_name order by used_rate desc;
spool off
exit

##上面的脚本会把结果集输出为html格式,spool /home/oracle/tablespace_use_info.xls则把结果集输出为excel格式


四:输出结果

html结果集如下:



excel结果集如下:





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

渔夫数据库笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值