Decode360's Blog

          業精于勤而荒于嬉 QQ:150355677 MSN:decode360@hotmail.com

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 ::  :: 管理 ::
            397 隨筆 :: 33 文章 :: 29 評論 :: 0 Trackbacks
          sqlplus的小秘密[轉]
          ?
          ??? 今天看到一個帖子,學了幾個SQLPLUS的小技巧。說實話還挺不錯的,確實沒有花時間看過Oracle關于SQLPLUS的文檔,總覺得有點浪費時間,呵呵,不過這樣知識就永遠掌握得不牢靠,忘記了要去查,不過要能抽出時間看這種文檔,估計要等1-2年之后了。
          ??? 轉載自: http://www.itpub.net/viewthread.php?tid=424236&extra=page%3D1%26amp%3Bfilter%3Ddigest
          ?
          ?
          ?
          1、顯示空格
          ?
          有沒有為了dbms_output.put_line會"吃掉"最前面的空格而苦惱?
          ?
          SQL> set serveroutput on
          SQL> exec dbms_output.put_line('?? abc');
          abc
          ?
          PL/SQL procedure successfully completed.
          ?
          ?
          (俺以前曾經很苦惱為了保留空格, 嘗試了加".", 加不可見字符等辦法, 不過終究覺得不自然)
          實際上, 只要在set serveroutput on后加上format wrapped參數, 就可以避免這個問題
          ?
          SQL> set serveroutput on format wrapped
          SQL> exec dbms_output.put_line('?? abc');
          ?? abc
          ?
          PL/SQL procedure successfully completed.
          ?
          ?
          ?
          2、SQL語句中的空行
          ?
          Sql*plus中, 不允許sql語句中間有空行, 這在從其它地方拷貝腳本到sql*plus中執行時很麻煩. 比如下面的腳本:
          ?
          select deptno, empno, ename
          from emp
          ?
          where empno = '7788';
          如果拷貝到sql*plus中執行, 就會出現錯誤:
          ?

          SQL> select deptno, empno, ename
          ? 2? from emp
          ? 3
          SQL> where empno = '7788';
          SP2-0734: unknown command beginning "where empn..." - rest of line ignored.
          ?
          原因是sqlplus遇到空行就認為是語句結束了.
          其實要改變這種現象, 只要使用SQLBLANKLINES參數就可以了.
          ?
          SQL> SET SQLBLANKLINES ON
          SQL>
          select deptno, empno, ename
          ? 2? from emp
          ? 3
          ? 4? where empno = '7788';
          ?
          ??? DEPTNO????? EMPNO ENAME
          ---------- ---------- ----------
          ??????? 20?????? 7788 SCOTT
          ?
          ?
          ?
          3、 # 的應用
          ?
          ?
          ??? 有沒有過這樣的經歷? 在sql*plus中敲了很長的命令后, 突然發現想不起某個列的名字了, 如果取消當前的命令,待查詢后再重敲, 那太痛苦了. 當然你可以另開一個sql*plus窗口進行查詢, 但這里提供的方法更簡單。這種情況下, 只要在下一行以#開頭, 就可以執行一條sql*plus命令, 執行完后, 剛才的語句可以繼續輸入
          ?

          SQL> select deptno,
          ? 2? empno,
          ? 3? ename
          ? 4? from emp
          ? 5? where
          ? 6? #desc emp
          Name????????????????????????????????????? Null???? Type
          ----------------------------------------- -------- --------------
          ?
          EMPNO???????????????????????????????????? NOT NULL NUMBER(4)
          ENAME????????????????????????????????????????????? VARCHAR2(10)
          JOB??????????????????????????????????????????????? VARCHAR2(9)
          MGR??????????????????????????????????????????????? NUMBER(4)
          HIREDATE?????????????????????????????????????????? DATE
          SAL??????????????????????????????????????????????? NUMBER(7,2)
          COMM?????????????????????????????????????????????? NUMBER(7,2)
          DEPTNO???????????????????????????????????????????? NUMBER(2)
          ?
          ? 6? sal > 4000;
          ?
          ??? DEPTNO????? EMPNO ENAME
          ---------- ---------- ----------
          ??????? 10?????? 7839 KING
          ?
          ?
          ?
          ?
          4、以html格式展現輸出

          ?
          sql*plus中有一個set markup html的命令, 可以將sql*plus的輸出以html格式展現
          ?

          SQL> set markup html on spool on
          SQL&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>
          ?
          SQL&gt;

          注意其中的spool on, 當在屏幕上輸出的時候, 我們看不出與不加spool on有什么區別, 但是當我們使用spool filename 輸出到文件的時候, 會看到spool文件中出現了<html><body>等tag.
          ?
          SQL&gt; spool c:\emp.htm
          <br>
          SQL&gt; /
          <br>
          <p>
          <table border='1' width='90%' align='center' summary='Script output'>
          ... ...
          ?
          SQL&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>
          SQL&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>
          ?
          SQL&gt; spool off
          <br>
          </body>
          </html>
          ?
          用ie打開emp.htm文件后的樣式如下:
          ?
          ? sqlplus
          ?
          ?
          現在看看spool off的情況下:
          ?
          SQL&gt; set markup html on spool off
          <br>
          SQL&gt; spool c:\emp2.htm
          <br>
          SQL&gt; /
          <br>
          <p>
          <table border='1' width='90%' align='center' summary='Script outpu
          ... ...
          SQL&gt; spool off
          <br>
          ?
          查看生成的emp2.htm文件的內容:
          ?
          SQL&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>
          ?
          SQL&gt; spool off
          ?
          由于這段代碼中沒有html文件頭, 所以我們可以直接作為內容插入到網頁中

          總結: 如果要生成一個完整的html文件, 就使用spool on選項, 如果只是要內容部分(用來添加到一個現有的網頁中), 那么就使用spool off選項.
          ?
          另外, set markup html還有很多選項可以用來定制生成的html的各個部分, 例如head, body, table等, 這里不再逐一說明, 詳細信息可以參考SQL*Plus User's Guide and Reference.
          ?
          適用場景: 當需要定時更新一個從數據庫中獲取內容的靜態頁面時, 這種方法絕對是快捷的并且容易實現的.
          posted on 2008-12-30 20:01 decode360 閱讀(403) 評論(0)  編輯  收藏 所屬分類: 05.SQL
          主站蜘蛛池模板: 曲麻莱县| 江安县| 罗源县| 丹凤县| 白银市| 康定县| 依安县| 肃南| 衡阳县| 波密县| 竹溪县| 简阳市| 聂荣县| 兰州市| 教育| 博客| 洛南县| 仲巴县| 恩施市| 昭觉县| 全椒县| 凤城市| 定西市| 六安市| 大埔区| 奉节县| 喀喇沁旗| 阿勒泰市| 万载县| 石屏县| 密山市| 桑日县| 乌鲁木齐县| 宜宾县| 剑川县| 马边| 黑河市| 绍兴县| 泰州市| 醴陵市| 化州市|