konhon

忘掉過去,展望未來。找回自我,超越自我。
逃避不一定躲的过, 面对不一定最难过, 孤单不一定不快乐, 得到不一定能长久, 失去不一定不再拥有, 可能因为某个理由而伤心难过, 但我却能找个理由让自己快乐.

Google

BlogJava 首页 新随笔 联系 聚合 管理
  203 Posts :: 0 Stories :: 61 Comments :: 0 Trackbacks

也许你还不知道 - 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.

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

posted on 2005-10-17 05:53 konhon 优华 阅读(405) 评论(0)  编辑  收藏 所属分类: Oracle

只有注册用户登录后才能发表评论。


网站导航: