oldwain随便写
===========================================================
===========================================================

也许你还不知道 - sqlplus的小秘密(4)

这个也许不算什么秘密, 很多人大概都知道, 不过用过的人也许不多.

在8.1.7版本(也许是816? 不太确定)以后, sql*plus中有一个set markup html的命令, 可以将sql*plus的输出以html格式展现.


scott@O9I.US.ORACLE.COM> set markup html on spool on
">scott@O9I.US.ORACLE.COM&gt; select empno, ename from emp where rownum<3;
<br>
<p>
<table border='1' width='90%' align='center' summary='Script output'>
<tr>
<th scope="col">
EMPNO
</th>
<th scope="col">
ENAME
</th>
</tr>
<tr>
<td align="right">
7369
</td>
<td>
SMITH
</td>
</tr>
<tr>
<td align="right">
7499
</td>
<td>
ALLEN
</td>
</tr>
</table>
<p>

注意其中的spool on, 当在屏幕上输出的时候, 我们看不出与不加spool on有什么区别, 但是当我们使用spool filename 输出到文件的时候, 会看到spool文件中出现了<html><body>等tag.

">scott@O9I.US.ORACLE.COM&gt; spool c:emp.htm
<br>
">scott@O9I.US.ORACLE.COM&gt; /
<br>
<p>
<table border='1' width='90%' align='center' summary='Script output'>
......此处省略

">scott@O9I.US.ORACLE.COM&gt; spool off
<br>

查看生成的emp.htm文件的内容:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=WINDOWS-936">
<meta name="generator" content="SQL*Plus 9.2.0">
<style type='text/css'> body {font:10pt Arial,Helvetica,sans-serif; color:black; background:White;} p {font:10pt Arial,Helvetica,sans-serif; color:black; background:White;} table,tr,td {font:10pt Arial,Helvetica,sans-serif; color:Black; background:#f7f7e7; padding:0px 0px 0px 0px; margin:0px 0px 0px 0px;} th {font:bold 10pt Arial,Helvetica,sans-serif; color:#336699; background:#cccc99; padding:0px 0px 0px 0px;} h1 {font:16pt Arial,Helvetica,Geneva,sans-serif; color:#336699; background-color:White; border-bottom:1px solid #cccc99; margin-top:0pt; margin-bottom:0pt; padding:0px 0px 0px 0px;} h2 {font:bold 10pt Arial,Helvetica,Geneva,sans-serif; color:#336699; background-color:White; margin-top:4pt; margin-bottom:0pt;} a {font:9pt Arial,Helvetica,sans-serif; color:#663300; background:#ffffff; margin-top:0pt; margin-bottom:0pt; vertical-align:top;}</style><title>SQL*Plus Report</title>
</head>
<body>
">scott@O9I.US.ORACLE.COM&gt; /
<br>
<p>
<table border='1' width='90%' align='center' summary='Script output'>
<tr>
<th scope="col">
EMPNO
</th>
<th scope="col">
ENAME
</th>
</tr>
<tr>
<td align="right">
7369
</td>
<td>
SMITH
</td>
</tr>
<tr>
<td align="right">
7499
</td>
<td>
ALLEN
</td>
</tr>
</table>
<p>

">scott@O9I.US.ORACLE.COM&gt; spool off
<br>
</body>
</html>

用ie打开emp.htm文件后的样式如下:

现在看看spool off的情况下:

">scott@O9I.US.ORACLE.COM&gt; set markup html on spool off
<br>
">scott@O9I.US.ORACLE.COM&gt; spool c:emp2.htm
<br>
">scott@O9I.US.ORACLE.COM&gt; /
<br>
<p>
<table border='1' width='90%' align='center' summary='Script outpu
......此处省略
">scott@O9I.US.ORACLE.COM&gt; spool off
<br>
">scott@O9I.US.ORACLE.COM&gt;

查看生成的emp2.htm文件的内容:

">scott@O9I.US.ORACLE.COM&gt; /
<br>
<p>
<table border='1' width='90%' align='center' summary='Script output'>
<tr>
<th scope="col">
EMPNO
</th>
<th scope="col">
ENAME
</th>
</tr>
<tr>
<td align="right">
7369
</td>
<td>
SMITH
</td>
</tr>
<tr>
<td align="right">
7499
</td>
<td>
ALLEN
</td>
</tr>
</table>
<p>

">scott@O9I.US.ORACLE.COM&gt; spool off

由于这段代码中没有html文件头, 所以我们可以直接作为内容插入到网页中, 现在我们就可以把这段代码放到下面作为示例:

EMPNO ENAME
7369 SMITH
7499 ALLEN

总结: 如果要生成一个完整的html文件, 就使用spool on选项, 如果只是要内容部分(用来添加到一个现有的网页中), 那么就使用spool off选项.

另外, set markup html还有很多选项可以用来定制生成的html的各个部分, 例如head, body, table等, 这里不再逐一说明, 详细信息可以参考SQL*Plus User's Guide and Reference.

适用场景: 当需要定时更新一个从数据库中获取内容的静态页面时, 这种方法绝对是快捷的并且容易实现的.

(需要引用, 请注明出处: http://oldwain.itpub.net)

oldwain 发表于:2005.09.08 12:54 ::分类: ( Oracle ) ::阅读:(2788次) :: 评论 (3) ::收藏此页到365Key
re: 也许你还不知道 - sqlplus的小秘密(4) [回复]

还可以直接用excel打开

mugen 评论于: 2005.09.08 22:54
re: 也许你还不知道 - sqlplus的小秘密(4) [回复]

这个真不错!

kaycat 评论于: 2005.09.19 20:26
re: 也许你还不知道 - sqlplus的小秘密(4) [回复]

一下看了4个小秘密tongue

netbanker 评论于: 2005.11.09 04:15

发表评论
标题

在此添加评论
表情符号: smile laughing tongue angry crying sad wassat wink

称呼

邮箱地址(可选)

个人主页(可选)

 authimage


自我介绍
切换风格
新闻聚合
博客日历
文章归档...
最新发表...
最新评论...
最多阅读文章...
最多评论文章...
博客统计...
Blog信息
赞助商
网站链接...
其它资源
我的网摘...