也許你還不知道 - sqlplus的小秘密(4)
這個(gè)也許不算什么秘密, 很多人大概都知道, 不過(guò)用過(guò)的人也許不多.
在8.1.7版本(也許是816? 不太確定)以后, sql*plus中有一個(gè)set markup html的命令, 可以將sql*plus的輸出以html格式展現(xiàn).
">scott@O9I.US.ORACLE.COM> 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, 當(dāng)在屏幕上輸出的時(shí)候, 我們看不出與不加spool on有什么區(qū)別, 但是當(dāng)我們使用spool filename 輸出到文件的時(shí)候, 會(huì)看到spool文件中出現(xiàn)了<html><body>等tag.
">scott@O9I.US.ORACLE.COM> spool c:emp.htm
<br>
">scott@O9I.US.ORACLE.COM> /
<br>
<p>
<table border='1' width='90%' align='center' summary='Script output'>
......此處省略
">scott@O9I.US.ORACLE.COM> spool off
<br>
查看生成的emp.htm文件的內(nèi)容:
<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> /
<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> spool off
<br>
</body>
</html>
用ie打開(kāi)emp.htm文件后的樣式如下:
現(xiàn)在看看spool off的情況下:
">scott@O9I.US.ORACLE.COM> set markup html on spool off
<br>
">scott@O9I.US.ORACLE.COM> spool c:emp2.htm
<br>
">scott@O9I.US.ORACLE.COM> /
<br>
<p>
<table border='1' width='90%' align='center' summary='Script outpu
......此處省略
">scott@O9I.US.ORACLE.COM> spool off
<br>
">scott@O9I.US.ORACLE.COM>
查看生成的emp2.htm文件的內(nèi)容:
">scott@O9I.US.ORACLE.COM> /
<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> spool off
由于這段代碼中沒(méi)有html文件頭, 所以我們可以直接作為內(nèi)容插入到網(wǎng)頁(yè)中, 現(xiàn)在我們就可以把這段代碼放到下面作為示例:
EMPNO | ENAME |
---|---|
7369 | SMITH |
7499 | ALLEN |
總結(jié): 如果要生成一個(gè)完整的html文件, 就使用spool on選項(xiàng), 如果只是要內(nèi)容部分(用來(lái)添加到一個(gè)現(xiàn)有的網(wǎng)頁(yè)中), 那么就使用spool off選項(xiàng).
另外, set markup html還有很多選項(xiàng)可以用來(lái)定制生成的html的各個(gè)部分, 例如head, body, table等, 這里不再逐一說(shuō)明, 詳細(xì)信息可以參考SQL*Plus User's Guide and Reference.
適用場(chǎng)景: 當(dāng)需要定時(shí)更新一個(gè)從數(shù)據(jù)庫(kù)中獲取內(nèi)容的靜態(tài)頁(yè)面時(shí), 這種方法絕對(duì)是快捷的并且容易實(shí)現(xiàn)的.