随笔-88  评论-77  文章-48  trackbacks-0
也许你还不知道 - sqlplus的小秘密(1)
===========================================================
作者: oldwain(
http://blog.itpub.net/oldwain )
发表于:2005.09.05 15:09
分类:
出处:
http://blog.itpub.net/post/6/40158
---------------------------------------------------------------

有没有为了dbms_output.put_line会"吃掉"最前面的空格而苦恼?
						代码:

[ email ] scott @ O9I . US . ORACLE . COM [/ email ]> set serveroutput on
[ email ] scott @ O9I . US . ORACLE . COM [/ email ]> exec dbms_output . put_line ( '   abc'
);
abc

PL
/ SQL procedure successfully completed
.




(俺以前曾经很苦恼为了保留空格, 尝试了加".", 加不可见字符等办法, 不过终究觉得不自然)
实际上, 只要在set serveroutput on后加上format wrapped参数, 就可以避免这个问题
						代码:

[ email ] scott @ O9I . US . ORACLE . COM [/ email ]> set serveroutput on format wrapped
[ email ] scott @ O9I . US . ORACLE . COM [/ email ]> exec dbms_output . put_line ( '   abc'
);
   
abc

PL
/ SQL procedure successfully completed
.


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

也许你还不知道 - sqlplus的小秘密(2)
===========================================================
作者: oldwain(http://blog.itpub.net/oldwain)
发表于:2005.09.06 10:06
分类:
出处:http://blog.itpub.net/post/6/40252
---------------------------------------------------------------

Sql*plus中, 不允许sql语句中间有空行, 这在从其它地方拷贝脚本到sql*plus中执行时很麻烦. 比如下面的脚本:

select deptno, empno, ename
from emp

where empno = '7788';
如果拷贝到sql*plus中执行, 就会出现错误:


scott@O9I.US.ORACLE.COM> select deptno, empno, ename
2 from emp
3
scott@O9I.US.ORACLE.COM> where empno = '7788';
SP2-0734: unknown command beginning "where empn..." - rest of line ignored.
scott@O9I.US.ORACLE.COM>

原因是sqlplus遇到空行就认为是语句结束了.
其实要改变这种现象, 只要使用SQLBLANKLINES参数就可以了
scott@O9I.US.ORACLE.COM> SET SQLBLANKLINES ON
scott@O9I.US.ORACLE.COM>
scott@O9I.US.ORACLE.COM> select deptno, empno, ename
2 from emp
3
4 where empno = '7788';

DEPTNO EMPNO ENAME
---------- ---------- ----------
20 7788 SCOTT

scott@O9I.US.ORACLE.COM>
(需要引用, 请注明出处: http://blog.itpub.net/oldwain)

也许你还不知道 - sqlplus的小秘密(3)
===========================================================
作者: oldwain(http://blog.itpub.net/oldwain)
发表于:2005.09.07 10:04
分类:
出处:http://blog.itpub.net/post/6/40364
---------------------------------------------------------------

有没有过这样的经历? 在sql*plus中敲了很长的命令后, 突然发现想不起某个列的名字了, 如果取消当前的命令,待查询后再重敲, 那太痛苦了. 当然你可以另开一个sql*plus窗口进行查询, 但这里提供的方法更简单.

比如说, 你想查工资大于4000的员工的信息, 输入了下面的语句:

scott@O9I.US.ORACLE.COM> select deptno,
2 empno,
3 ename
4 from emp
5 where
这时, 你发现你想不起来工资的列名是什么了.

这种情况下, 只要在下一行以#开头, 就可以执行一条sql*plus命令, 执行完后, 刚才的语句可以继续输入


scott@O9I.US.ORACLE.COM> 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

scott@O9I.US.ORACLE.COM>
(需要引用, 请注明出处: http://blog.itpub.net/oldwain)


也许你还不知道 - sqlplus的小秘密(4)
===========================================================
作者: oldwain(
http://blog.itpub.net/oldwain )
发表于:2005.09.08 12:54
分类:
出处:
http://blog.itpub.net/post/6/40485
---------------------------------------------------------------

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

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

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

												代码:

[ email ] scott @ O9I . US . ORACLE . COM [/ email ]> 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>"
> scott @ O9I . US . ORACLE . COM & gt
;


注意其中的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



查看生成的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文件后的样式如下:

http://blog.itpub.net/resserver.php?blogId=6&resource=sqlplushtml.JPG

现在看看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.OR


查看生成的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 ;

																				代码:



由于这段代码中没有html文件头, 所以我们可以直接作为内容插入到网页中.

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

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

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

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

posted on 2006-04-27 10:03 崛起的程序员 阅读(254) 评论(0)  编辑  收藏 所属分类: oracle

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


网站导航: