﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-The home of Dragon-随笔分类-DB</title><link>http://www.blogjava.net/guanlong/category/951.html</link><description>so cool～haha</description><language>zh-cn</language><lastBuildDate>Mon, 19 May 2008 14:42:01 GMT</lastBuildDate><pubDate>Mon, 19 May 2008 14:42:01 GMT</pubDate><ttl>60</ttl><item><title>[转]DBCC DBREINDEX重建索引提高SQL Server性能</title><link>http://www.blogjava.net/guanlong/archive/2008/05/17/201065.html</link><dc:creator>Stephen</dc:creator><author>Stephen</author><pubDate>Sat, 17 May 2008 03:47:00 GMT</pubDate><guid>http://www.blogjava.net/guanlong/archive/2008/05/17/201065.html</guid><wfw:comment>http://www.blogjava.net/guanlong/comments/201065.html</wfw:comment><comments>http://www.blogjava.net/guanlong/archive/2008/05/17/201065.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/guanlong/comments/commentRss/201065.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/guanlong/services/trackbacks/201065.html</trackback:ping><description><![CDATA[<p style="margin: 0in 0in 0pt; text-align: center" align="center"><strong><span style="font-size: 10pt; font-family: Verdana">DBCC DBREINDEX</span></strong><strong><span style="font-size: 10pt; font-family: 宋体">重建索引提高</span></strong><strong><span style="font-size: 10pt; font-family: Verdana">SQL Server</span></strong><strong><span style="font-size: 10pt; font-family: 宋体">性能</span></strong></p>
<p style="margin: 0in 0in 0pt; text-indent: 0.25in"><span style="font-size: 10pt; font-family: 宋体">大多数</span><span style="font-size: 10pt; font-family: Verdana">SQL Server</span><span style="font-size: 10pt; font-family: 宋体">表需要索引来提高数据的访问速度，如果没有索引，</span><span style="font-size: 10pt; font-family: Verdana">SQL Server </span><span style="font-size: 10pt; font-family: 宋体">要进行表格扫描读取表中的每一个记录才能找到索要的数据。索引可以分为簇索引和非簇索引，簇索引通过重排表中的数据来提高数据的访问速度，而非簇索引则通过维护表中的数据指针来提高数据的索引。</span></p>
<p style="margin: 0in 0in 0pt"><strong><span style="font-size: 10pt; font-family: Verdana">1. </span></strong><strong><span style="font-size: 10pt; font-family: 宋体">索引的体系结构</span></strong></p>
<p style="margin: 0in 0in 0pt; text-indent: 0.25in"><span style="font-size: 10pt; font-family: 宋体">为什么要不断的维护表的索引？首先，简单介绍一下索引的体系结构。</span><span style="font-size: 10pt; font-family: Verdana">SQL Server</span><span style="font-size: 10pt; font-family: 宋体">在硬盘中用</span><span style="font-size: 10pt; font-family: Verdana">8KB</span><span style="font-size: 10pt; font-family: 宋体">页面在数据库文件内存放数据。缺省情况下这些页面及其包含的数据是无组织的。为了使混乱变为有序，就要生成索引。生成索引后，就有了索引页和数据页，数据页保存用户写入的数据信息。索引页存放用于检索列的数据值清单（关键字）和索引表中该值所在纪录的地址指针。索引分为簇索引和非簇索引，簇索引实质上是将表中的数据排序，就好像是字典的索引目录。非簇索引不对数据排序，它只保存了数据的指针地址。向一个带簇索引的表中插入数据，当数据页达到</span><span style="font-size: 10pt; font-family: Verdana">100%</span><span style="font-size: 10pt; font-family: 宋体">时，由于页面没有空间插入新的的纪录，这时就会发生分页，</span><span style="font-size: 10pt; font-family: Verdana">SQL Server </span><span style="font-size: 10pt; font-family: 宋体">将大约一半的数据从满页中移到空页中，从而生成两个半的满页。这样就有大量的数据空间。簇索引是双向链表，在每一页的头部保存了前一页、后一页地址以及分页后数据移动的地址，由于新页可能在数据库文件中的任何地方，因此页面的链接不一定指向磁盘的下一个物理页，链接可能指向了另一个区域，这就形成了分块，从而减慢了系统的速度。对于带簇索引和非簇索引的表来说，非簇索引的关键字是指向簇索引的，而不是指向数据页的本身。</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 宋体">为了克服数据分块带来的负面影响，需要重构表的索引，这是非常费时的，因此只能在需要时进行。</span><span style="font-size: 10pt; font-family: 宋体">可以通过</span><span style="font-size: 10pt; font-family: Verdana">DBCC SHOWCONTIG</span><span style="font-size: 10pt; font-family: 宋体">来确定是否需要重构表的索引。</span></p>
<p style="margin: 0in 0in 0pt"><strong>&nbsp;</strong></p>
<p style="margin: 0in 0in 0pt"><strong><span style="font-size: 10pt; font-family: Verdana">2. </span></strong><strong><span style="font-size: 10pt; font-family: Verdana">DBCC SHOWCONTIG</span></strong><strong><span style="font-size: 10pt; font-family: 宋体">用法</span></strong></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 宋体">下面举例来说明</span><span style="font-size: 10pt; font-family: Verdana">DBCC SHOWCONTIG</span><span style="font-size: 10pt; font-family: 宋体">和</span><span style="font-size: 10pt; font-family: Verdana">DBCC REDBINDEX</span><span style="font-size: 10pt; font-family: 宋体">的使用方法。以</span><span style="font-size: 10pt; font-family: 宋体">应用程序中</span><span style="font-size: 10pt; font-family: 宋体">的</span><span style="font-size: 10pt; font-family: Verdana">Employee</span><span style="font-size: 10pt; font-family: 宋体">数据</span><span style="font-size: 10pt; font-family: 宋体">表</span><span style="font-size: 10pt; font-family: 宋体">作为例子</span><span style="font-size: 10pt; font-family: 宋体">，在</span><span style="font-size: 10pt; font-family: Verdana"> SQL Server</span><span style="font-size: 10pt; font-family: 宋体">的</span><span style="font-size: 10pt; font-family: Verdana">Query analyzer</span><span style="font-size: 10pt; font-family: 宋体">输入命令：</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: green; font-family: Verdana">use database_name</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: green; font-family: Verdana">declare @table_id int</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: green; font-family: Verdana">set @table_id=object_id('Employee')</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: green; font-family: Verdana">dbcc showcontig(@table_id)</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 宋体">输出结果：</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">DBCC SHOWCONTIG scanning 'Employee' table...</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">Table: 'Employee' (1195151303); index ID: 1, database ID: 53</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">TABLE level scan performed.</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">- Pages Scanned................................: 179</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">- Extents Scanned..............................: 24</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">- Extent Switches..............................: 24</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">- Avg. Pages per Extent........................: 7.5</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">- Scan Density [Best Count:Actual Count].......: 92.00% [23:25]</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">- Logical Scan Fragmentation ..................: 0.56%</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">- Extent Scan Fragmentation ...................: 12.50%</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">- Avg. Bytes Free per Page.....................: 552.3</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">- Avg. Page Density (full).....................: 93.18%</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">DBCC execution completed. If DBCC printed error messages, contact your system administrator.</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 宋体">通过分析这些结果可以知道该表的索引是否需要重构。如下描述了每一行的意义：</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 宋体">信息</span><span style="font-size: 10pt; font-family: Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-size: 10pt; font-family: 宋体">描述</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">Pages Scanned&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-size: 10pt; font-family: 宋体">表或索引中的长页数</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">Extents Scanned&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-size: 10pt; font-family: 宋体">表或索引中的长区页数</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">Extent Switches&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DBCC</span><span style="font-size: 10pt; font-family: 宋体">遍历页时从一个区域到另一个区域的次数</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">Avg. Pages per Extent&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-size: 10pt; font-family: 宋体">相关区域中的页数</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">Scan Density[Best Count:Actual Count]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">Best Count</span><span style="font-size: 10pt; font-family: 宋体">是连续链接时的理想区域改变数，</span><span style="font-size: 10pt; font-family: Verdana">Actual Count</span><span style="font-size: 10pt; font-family: 宋体">是实际区域改变数，</span><span style="font-size: 10pt; font-family: Verdana">Scan Density</span><span style="font-size: 10pt; font-family: 宋体">为</span><span style="font-size: 10pt; font-family: Verdana">100%</span><span style="font-size: 10pt; font-family: 宋体">表示没有分块。</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">Logical Scan Fragmentation&nbsp;&nbsp; </span><span style="font-size: 10pt; font-family: 宋体">扫描索引页中失序页的百分比</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">Extent Scan Fragmentation&nbsp;&nbsp;&nbsp; </span><span style="font-size: 10pt; font-family: 宋体">不实际相邻和包含链路中所有链接页的区域数</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">Avg. Bytes Free per Page&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-size: 10pt; font-family: 宋体">扫描页面中平均自由字节数</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">Avg. Page Density (full)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-size: 10pt; font-family: 宋体">平均页密度，表示页有多满</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">&nbsp;</span><span style="font-size: 10pt; font-family: 宋体">从上面命令的执行结果可以看的出来，</span><span style="font-size: 10pt; font-family: Verdana">Best count</span><span style="font-size: 10pt; font-family: 宋体">为</span><span style="font-size: 10pt; font-family: Verdana">23 </span><span style="font-size: 10pt; font-family: 宋体">而</span><span style="font-size: 10pt; font-family: Verdana">Actual Count</span><span style="font-size: 10pt; font-family: 宋体">为</span><span style="font-size: 10pt; font-family: Verdana">25</span><span style="font-size: 10pt; font-family: 宋体">这表明</span><span style="font-size: 10pt; font-family: Verdana">orders</span><span style="font-size: 10pt; font-family: 宋体">表有分块需要重构表索引。下面通过</span><span style="font-size: 10pt; font-family: Verdana">DBCC DBREINDEX</span><span style="font-size: 10pt; font-family: 宋体">来重构表的簇索引。</span></p>
<p style="margin: 0in 0in 0pt"><strong><span style="font-size: 10pt; font-family: Verdana">3</span></strong><strong><span style="font-size: 10pt; font-family: Verdana">. DBCC DBREINDEX </span></strong><strong><span style="font-size: 10pt; font-family: 宋体">用法</span></strong></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 宋体">重建指定数据库中表的一个或多个索引。</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 宋体">语法</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">DBCC DBREINDEX</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">&nbsp;&nbsp;&nbsp; (&nbsp;&nbsp;&nbsp; [ 'database.owner.table_name'&nbsp;&nbsp;&nbsp; </span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [ , index_name</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [ , fillfactor ]</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ] </span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ] </span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">&nbsp;&nbsp;&nbsp; )&nbsp;&nbsp;&nbsp;&nbsp; </span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 宋体">参数</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">'database.owner.table_name'</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 宋体">是要重建其指定的索引的表名。</span><span style="font-size: 10pt; font-family: 宋体">数据库、所有者和表名必须符合标识符的规则。有关更多信息，请参见使用标识符。</span><span style="font-size: 10pt; font-family: 宋体">如果提供</span><span style="font-size: 10pt; font-family: Verdana"> database </span><span style="font-size: 10pt; font-family: 宋体">或</span><span style="font-size: 10pt; font-family: Verdana"> owner </span><span style="font-size: 10pt; font-family: 宋体">部分，则必须使用单引号</span><span style="font-size: 10pt; font-family: Verdana"> (') </span><span style="font-size: 10pt; font-family: 宋体">将整个</span><span style="font-size: 10pt; font-family: Verdana"> database.owner.table_name </span><span style="font-size: 10pt; font-family: 宋体">括起来。如果只指定</span><span style="font-size: 10pt; font-family: Verdana"> table_name</span><span style="font-size: 10pt; font-family: 宋体">，则不需要单引号。</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">index_name</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 宋体">是要重建的索引名。</span><span style="font-size: 10pt; font-family: 宋体">索引名必须符合标识符的规则。</span><span style="font-size: 10pt; font-family: 宋体">如果未指定</span><span style="font-size: 10pt; font-family: Verdana"> index_name </span><span style="font-size: 10pt; font-family: 宋体">或指定为</span><span style="font-size: 10pt; font-family: Verdana"> ' '</span><span style="font-size: 10pt; font-family: 宋体">，就要对表的所有索引进行重建。</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">fillfactor</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 宋体">是创建索引时每个索引页上要用于存储数据的空间百分比。</span><span style="font-size: 10pt; font-family: Verdana">fillfactor </span><span style="font-size: 10pt; font-family: 宋体">替换起始填充因子以作为索引或任何其它重建的非聚集索引（因为已重建聚集索引）的新默认值。如果</span><span style="font-size: 10pt; font-family: Verdana"> fillfactor </span><span style="font-size: 10pt; font-family: 宋体">为</span><span style="font-size: 10pt; font-family: Verdana"> 0</span><span style="font-size: 10pt; font-family: 宋体">，</span><span style="font-size: 10pt; font-family: Verdana">DBCC DBREINDEX </span><span style="font-size: 10pt; font-family: 宋体">在创建索引时将使用指定的起始</span><span style="font-size: 10pt; font-family: Verdana"> fillfactor</span><span style="font-size: 10pt; font-family: 宋体">。</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 宋体">同样在</span><span style="font-size: 10pt; font-family: Verdana">Query Analyzer</span><span style="font-size: 10pt; font-family: 宋体">中输入命令：</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; color: green; font-family: Verdana">dbcc dbreindex('database_name.dbo.Employee','',90)</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 宋体">然后再用</span><span style="font-size: 10pt; font-family: Verdana">DBCC SHOWCONTIG</span><span style="font-size: 10pt; font-family: 宋体">查看重构索引后的结果：</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">DBCC SHOWCONTIG scanning 'Employee' table...</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">Table: 'Employee' (1195151303); index ID: 1, database ID: 53</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">TABLE level scan performed.</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">- Pages Scanned................................: 178</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">- Extents Scanned..............................: 23</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">- Extent Switches..............................: 22</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">- Avg. Pages per Extent........................: 7.7</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">- Scan Density [Best Count:Actual Count].......: 100.00% [23:23]</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">- Logical Scan Fragmentation ..................: 0.00%</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">- Extent Scan Fragmentation ...................: 0.00%</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">- Avg. Bytes Free per Page.....................: 509.5</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">- Avg. Page Density (full).....................: 93.70%</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">DBCC execution completed. If DBCC printed error messages, contact your system administrator.</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 宋体">通过结果我们可以看到</span><span style="font-size: 10pt; font-family: Verdana">Scan Denity</span><span style="font-size: 10pt; font-family: 宋体">为</span><span style="font-size: 10pt; font-family: Verdana">100%</span><span style="font-size: 10pt; font-family: 宋体">。</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana">******</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 宋体">原文链接：《如何提高</span><span style="font-size: 10pt; font-family: Verdana">SQL SERVER</span><span style="font-size: 10pt; font-family: 宋体">的性能》</span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: Verdana"><a href="http://www.csdn.com.cn/database/1142.htm">http://www.csdn.com.cn/database/1142.htm</a></span></p>
<p style="margin: 0in 0in 0pt"><span style="font-size: 10pt; font-family: 宋体">作者：</span><span style="font-size: 10pt; font-family: Verdana">unknown</span></p>
<img src ="http://www.blogjava.net/guanlong/aggbug/201065.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/guanlong/" target="_blank">Stephen</a> 2008-05-17 11:47 <a href="http://www.blogjava.net/guanlong/archive/2008/05/17/201065.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Oracle经验技巧集锦[ZT]</title><link>http://www.blogjava.net/guanlong/archive/2005/12/21/24923.html</link><dc:creator>Stephen</dc:creator><author>Stephen</author><pubDate>Wed, 21 Dec 2005 03:02:00 GMT</pubDate><guid>http://www.blogjava.net/guanlong/archive/2005/12/21/24923.html</guid><wfw:comment>http://www.blogjava.net/guanlong/comments/24923.html</wfw:comment><comments>http://www.blogjava.net/guanlong/archive/2005/12/21/24923.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/guanlong/comments/commentRss/24923.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/guanlong/services/trackbacks/24923.html</trackback:ping><description><![CDATA[<STRONG>
<P><STRONG><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>1．删除表空间</FONT></STRONG></P>
<P class=code><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>DROP TABLESPACE TableSpaceName [INCLUDING CONTENTS [AND DATAFILES]]<BR>2．删除用户</FONT></P>
<P class=code><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>DROP USER User_Name CASCADE</FONT></P>
<P><STRONG><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>3．删除表的注意事项</FONT></STRONG></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>在删除一个表中的全部数据时，须使用TRUNCATE TABLE 表名;因为用DROP TABLE，DELETE * FROM 表名时，TABLESPACE表空间该表的占用空间并未释放，反复几次DROP，DELETE操作后，该TABLESPACE上百兆的空间就被耗光了。</FONT></P>
<P><STRONG><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>4．having子句的用法</FONT></STRONG></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>having子句对group by子句所确定的行组进行控制，having子句条件中只允许涉及常量,聚组函数或group by 子句中的列。</FONT></P>
<P><STRONG><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>5．外部联接"+"的用法</FONT></STRONG></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>外部联接"+"按其在"="的左边或右边分左联接和右联接.若不带"+"运算符的表中的一个行不直接匹配于带"+"预算符的表中的任何行,则前者的行与后者中的一个空行相匹配并被返回.若二者均不带’+’,则二者中无法匹配的均被返回.利用外部联接"+",可以替代效率十分低下的 not in 运算,大大提高运行速度.例如,下面这条命令执行起来很慢 </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>用外联接提高表连接的查询速度 </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>在作表连接(常用于视图)时，常使用以下方法来查询数据: </FONT></P>
<P class=code><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>SELECT PAY_NO, PROJECT_NAME <BR>FROM A <BR>WHERE A.PAY_NO NOT IN (SELECT PAY_ <BR>NO FROM B WHERE VALUE &gt;=120000); </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>---- 但是若表A有10000条记录，表B有10000条记录，则要用掉30分钟才能查完，主要因为NOT IN要进 行一条一条的比较，共需要10000*10000次比较后，才能得到结果。该用外联接后，可以缩短到1分左右的时间: </FONT></P>
<P class=code><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>SELECT PAY_NO,PROJECT_NAME<BR>FROM A,B <BR>WHERE A.PAY_NO=B.PAY_NO(+) <BR>AND B.PAY_NO IS NULL <BR>AND B.VALUE &gt;=12000; </FONT></P>
<P><FONT face=Arial><FONT size=1><FONT style="BACKGROUND-COLOR: #ffffff"><FONT color=#000000><STRONG>6．set transaction命令的用法</STRONG> </FONT></FONT></FONT></FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>在执行大事务时,有时oracle会报出如下的错误: </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>ORA-01555:snapshot too old (rollback segment too small) </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>　　这说明oracle给此事务随机分配的回滚段太小了,这时可以为它指定一个足够大的回滚段,以确保这个事务的成功执行.例如 </FONT></P>
<P class=code><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>set transaction use rollback segment roll_abc; <BR>delete from table_name where ... <BR>commit; </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>　　回滚段roll_abc被指定给这个delete事务,commit命令则在事务结束之后取消了回滚段的指定. <BR></FONT></P>
<P><FONT face=Arial><FONT size=1><FONT style="BACKGROUND-COLOR: #ffffff"><FONT color=#000000><STRONG>7．数据库重建应注意的问题</STRONG> </FONT></FONT></FONT></FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>　　在利用import进行数据库重建过程中,有些视图可能会带来问题,因为结构输入的顺序可能造成视图的输入先于它低层次表的输入,这样建立视图就会失败.要解决这一问题,可采取分两步走的方法:首先输入结构,然后输入数据.命令举例如下 (uesrname:jfcl,password:hfjf,host sting:ora1,数据文件:expdata.dmp): </FONT></P>
<P class=code><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>imp jfcl/hfjf@ora1 file=empdata.dmp rows=N <BR>imp jfcl/hfjf@ora1 file=empdata.dmp full=Y buffer=64000 <BR>commit=Y ignore=Y </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>　　第一条命令输入所有数据库结构,但无记录.第二次输入结构和数据,64000字节提交一次.ignore=Y选项保证第二次输入既使对象存在的情况下也能成功. </FONT></P>
<P class=code><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>select a.empno from emp a where a.empno not in (select empno from emp1 where job=’SALE’); </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>　　倘若利用外部联接,改写命令如下: </FONT></P>
<P class=code><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>select a.empno from emp a ,emp1 b <BR>where a.empno=b.empno(+) <BR>and b.empno is null <BR>and b.job=’SALE’; </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>可以发现,运行速度明显提高. </FONT></P>
<P><STRONG><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>8．从已知表新建另一个表：</FONT></STRONG></P>
<P class=code><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>CREATE TABLE b <BR>AS SELECT * (可以是表a中的几列) <BR>FROM a <BR>WHERE a.column = ...; </FONT></P>
<P><FONT face=Arial><FONT size=1><FONT style="BACKGROUND-COLOR: #ffffff"><FONT color=#000000><STRONG>9．查找、删除重复记录:</STRONG> </FONT></FONT></FONT></FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>法一: 用Group by语句 此查找很快的</FONT></P>
<P class=code><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>select count(num), max(name) from student --查找表中num列重复的，列出重复的记录数，并列出他的name属性<BR>group by num <BR>having count(num) &gt;1 --按num分组后找出表中num列重复，即出现次数大于一次<BR>delete from student(上面Select的) </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>这样的话就把所有重复的都删除了。-----慎重 </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>法二:当表比较大(例如10万条以上)时,这个方法的效率之差令人无法忍受,需要另想办法: </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>---- 执行下面SQL语句后就可以显示所有DRAWING和DSNO相同且重复的记录 </FONT></P>
<P class=code><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>SELECT * FROM EM5_PIPE_PREFAB <BR>WHERE ROWID!=(SELECT MAX(ROWID) FROM EM5_PIPE_PREFAB D --D相当于First,Second <BR>WHERE EM5_PIPE_PREFAB.DRAWING=D.DRAWING AND <BR>EM5_PIPE_PREFAB.DSNO=D.DSNO); </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>---- 执行下面SQL语句后就可以刪除所有DRAWING和DSNO相同且重复的记录 </FONT></P>
<P class=code><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>DELETE FROM EM5_PIPE_PREFAB <BR>WHERE ROWID!=(SELECT MAX(ROWID) FROM EM5_PIPE_PREFAB D <BR>WHERE EM5_PIPE_PREFAB.DRAWING=D.DRAWING AND <BR>EM5_PIPE_PREFAB.DSNO=D.DSNO); <BR></FONT></P>
<P><FONT face=Arial><FONT size=1><FONT style="BACKGROUND-COLOR: #ffffff"><FONT color=#000000><STRONG>10．返回表中[N，M]条记录：</STRONG> </FONT></FONT></FONT></FONT></P>
<P><BR><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>取得某列中第N大的行 </FONT></P>
<P class=code><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>select column_name from <BR>(select table_name.*,dense_rank() over (order by column desc) rank from table_name) <BR>where rank = &amp;N； </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>　假如要返回前5条记录： </FONT></P>
<P class=code><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>　　select * from tablename where rownum&lt;6;(或是rownum &lt;= 5 或是rownum != 6) </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>假如要返回第5-9条记录： </FONT></P>
<P class=code><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>select * from tablename <BR>where … <BR>and rownum&lt;10 <BR>minus <BR>select * from tablename <BR>where … <BR>and rownum&lt;5 <BR>order by name </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>选出结果后用name排序显示结果。(先选再排序) </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>注意：只能用以上符号(&lt;、&lt;=、!=)。 </FONT></P>
<P class=code><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>select * from tablename where rownum != 10;返回的是前９条记录。 </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>不能用：&gt;,&gt;=,=,Between...and。由于rownum是一个总是从1开始的伪列，Oracle 认为这种条件 不成立，查不到记录. </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>另外，这个方法更快： </FONT></P>
<P class=code><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>select * from ( <BR>select rownum r,a from yourtable <BR>where rownum &lt;= 20 <BR>order by name ) <BR>where r &gt; 10 </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>这样取出第11-20条记录!(先选再排序再选) </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>要先排序再选则须用select嵌套：内层排序外层选。 </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>rownum是随着结果集生成的，一旦生成，就不会变化了；同时,生成的结果是依次递加的，没有1就永远不会有2! </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>rownum 是在 查询集合产生的过程中产生的伪列，并且如果where条件中存在 rownum 条件的话，则: </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>1： 假如 判定条件是常量，则： </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>只能 rownum = 1, &lt;= 大于1 的自然数， = 大于1 的数是没有结果的， 大于一个数也是没有结果的 </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>即 当出现一个 rownum 不满足条件的时候则 查询结束 　　this is stop key! </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>2: 当判定值不是常量的时候 </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>若条件是 = var , 则只有当 var 为1 的时候才满足条件，这个时候不存在 stop key ,必须进行 full scan ,对每个满足其他where条件的数据进行判定 </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>选出一行后才能去选rownum=2的行…… <BR></FONT></P>
<P><FONT face=Arial><FONT size=1><FONT style="BACKGROUND-COLOR: #ffffff"><FONT color=#000000><STRONG>11．快速编译所有视图</STRONG> </FONT></FONT></FONT></FONT></P>
<P><BR><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>---- 当在把数据库倒入到新的服务器上后(数据库重建)，需要将视图重新编译一遍，因为该表空间视图到其它表空间的表的连接会出现问题，可以利用PL/SQL的语言特性，快速编译。 </FONT></P>
<P class=code><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>SQL &gt;SPOOL ON.SQL <BR>SQL &gt;SELECT ‘ALTER VIEW ‘||TNAME||’ <BR>COMPILE;’ FROM TAB; <BR>SQL &gt;SPOOL OFF </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>然后执行ON.SQL即可。 </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>SQL &gt;@ON.SQL </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>当然，授权和创建同义词也可以快速进行，如： </FONT></P>
<P class=code><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>SQL &gt;SELECT ‘GRANT SELECT ON ’<BR>||TNAME||’ TO USERNAME;’ FROM TAB; <BR>SQL &gt;SELECT ‘CREATE SYNONYM <BR>‘||TNAME||’ FOR USERNAME.’||TNAME||’;’ FROM TAB; </FONT></P>
<P><STRONG><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>12．读写文本型操作系统文件</FONT></STRONG></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>---- 在PL/SQL 3.3以上的版本中，UTL_FILE包允许用户通过PL/SQL读写操作系统文件。如下： </FONT></P>
<P class=code><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>DECALRE <BR>FILE_HANDLE UTL_FILE.FILE_TYPE; <BR>BEGIN <BR>FILE_HANDLE:=UTL_FILE.FOPEN( <BR>‘C:\’,’TEST.TXT’,’A’); <BR>UTL_FILE.PUT_LINE(FILE_HANDLE,’<BR>HELLO,IT’S A TEST TXT FILE’); <BR>UTL_FILE.FCLOSE(FILE_HANDLE); <BR>END; </FONT></P>
<P><FONT face=Arial><FONT size=1><FONT style="BACKGROUND-COLOR: #ffffff"><FONT color=#000000><STRONG>13．在数据库触发器中使用列的新值与旧值</STRONG> </FONT></FONT></FONT></FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>---- 在数据库触发器中几乎总是要使用触发器基表的列值，如果某条语句需要某列修改前的值，使用:OLD就可以了，使用某列修改后的新值，用:NEW就可以了。如:OLD.DEPT_NO,:NEW.DEPT_NO。 </FONT></P>
<P><FONT face=Arial><FONT size=1><FONT style="BACKGROUND-COLOR: #ffffff"><FONT color=#000000><STRONG>14．数据库文件的移动方法</STRONG> </FONT></FONT></FONT></FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>当想将数据库文件移动到另外一个目录下时，可以用ALTER DATABASE命令来移动(比ALTER TABLESPACE适用性强)： </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>1. 使用SERVER MANAGER关闭实例. </FONT></P>
<P class=code><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>SVRMGR &gt; connect internal; <BR>SVRMGR &gt; shutdown; <BR>SVRMGR &gt;exit; </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>2. 使用操作系统命令来移动数据库文件位置(假设这里操作系统为SOLARIS 2.6). 在UNIX中用 mv命令可以把文件移动到新的位置， </FONT></P>
<P class=code><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>#mv /ora13/orarun/document.dbf /ora12/orarun </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>3. 装载数据库并用alter database命令来改变数据库中的文件名. </FONT></P>
<P class=code><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>SVRMGR &gt; connect internal; <BR>SVRMGR &gt; startup mount RUN73; <BR>SVRMGR &gt; alter database rename file <BR>&gt; ‘/ ora13/orarun/document.dbf’ <BR>&gt; ‘/ ora12/orarun/document.dbf’; </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>4. 启动实例. </FONT></P>
<P class=code><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>SVRMGR &gt; alter database open; <BR><BR></FONT></P>
<P><FONT face=Arial><FONT size=1><FONT style="BACKGROUND-COLOR: #ffffff"><FONT color=#000000><STRONG>15．连接查询结果：</STRONG> </FONT></FONT></FONT></FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>表a 列 a1 a2 </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>记录 1 a </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>1 b </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>2 x </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>2 y </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>2 z </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>用select能选成以下结果: </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>1 ab </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>2 xyz </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>下面有两个例子： </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>1.使用pl/sql代码实现，但要求你组合后的长度不能超出oracle varchar2长度的限制 </FONT></P>
<P class=code><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>create or replace type strings_table is table of varchar2(20); <BR>/ <BR>create or replace function merge (pv in strings_table) return varchar2 <BR>is <BR>ls varchar2(4000); <BR>begin <BR>for i in 1..pv.count loop <BR>ls := ls || pv(i); <BR>end loop; <BR>return ls; <BR>end; <BR>/ <BR>create table t (id number,name varchar2(10)); <BR>insert into t values(1,'Joan'); <BR>insert into t values(1,'Jack'); <BR>insert into t values(1,'Tom'); <BR>insert into t values(2,'Rose'); <BR>insert into t values(2,'Jenny'); <BR>column names format a80; <BR>select t0.id,merge(cast(multiset(select name from t where t.id = t0.id) as strings_table)) names <BR>from (select distinct id from t) t0; <BR>drop type strings_table; <BR>drop function merge; <BR>drop table t; </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>2.纯粹用sql： </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>表dept, emp </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>要得到如下结果 </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>deptno, dname, employees </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>--------------------------------- </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>10, accounting, clark;king;miller <BR>20, research, smith;adams;ford;scott;jones <BR>30, sales, allen;blake;martin;james;turners </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>每个dept的employee串起来作为一条记录返回 </FONT></P>
<P class=code><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>This example uses a max of 6, and would need more cut n pasting to do more than that： <BR>SQL&gt; select deptno, dname, emps <BR>2 from ( <BR>3 select d.deptno, d.dname, rtrim(e.ename ||', '|| <BR>4 lead(e.ename,1) over (partition by d.deptno <BR>5 order by e.ename) ||', '|| <BR>6 lead(e.ename,2) over (partition by d.deptno <BR>7 order by e.ename) ||', '|| <BR>8 lead(e.ename,3) over (partition by d.deptno <BR>9 order by e.ename) ||', '|| <BR>10 lead(e.ename,4) over (partition by d.deptno <BR>11 order by e.ename) ||', '|| <BR>12 lead(e.ename,5) over (partition by d.deptno <BR>13 order by e.ename),', ') emps, <BR>14 row_number () over (partition by d.deptno <BR>15 order by e.ename) x <BR>16 from emp e, dept d <BR>17 where d.deptno = e.deptno <BR>18 ) <BR>19 where x = 1 <BR>20 / <BR><BR>DEPTNO DNAME EMPS </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>------- ----------- ------------------------------------------ </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>10 ACCOUNTING CLARK, KING, MILLER </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>20 RESEARCH ADAMS, FORD, JONES, ROONEY, SCOTT, SMITH </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>30 SALES ALLEN, BLAKE, JAMES, MARTIN, TURNER, WARD <BR><BR></FONT></P>
<P><FONT face=Arial><FONT size=1><FONT style="BACKGROUND-COLOR: #ffffff"><FONT color=#000000><STRONG>16．在Oracle中建一个编号会自动增加的字段,以利于查询</STRONG> </FONT></FONT></FONT></FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>1、建立序列： </FONT></P>
<P class=code><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>CREATE SEQUENCE checkup_no_seq <BR>NOCYCLE <BR>MAXVALUE 9999999999 <BR>START WITH 2; </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>2、建立触发器： </FONT></P>
<P class=code><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>CREATE OR REPLACE TRIGGER set_checkup_no <BR>BEFORE INSERT ON checkup_history <BR>FOR EACH ROW <BR>DECLARE <BR>next_checkup_no NUMBER; <BR>BEGIN <BR>--Get the next checkup number from the sequence <BR>SELECT checkup_no_seq.NEXTVAL <BR>INTO next_checkup_no <BR>FROM dual; </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>--use the sequence number as the primary key <BR>--for the record being inserted <BR>:new.checkup_no := next_checkup_no; <BR>END; </FONT></P>
<P><FONT face=Arial><FONT size=1><FONT style="BACKGROUND-COLOR: #ffffff"><FONT color=#000000><STRONG>17．查看对象的依赖关系(比如视图与表的引用)</STRONG> </FONT></FONT></FONT></FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>查看视图：dba_dependencies 记录了相关的依赖关系 </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>查东西不知道要查看哪个视图时，可以在DBA_Objects里看， </FONT></P>
<P class=code><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>select object_name from dba_objects where object_name like '%ROLE%'(假如查看ROLE相关) </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>然后DESC一下就大体上知道了。</FONT></P>
<P><FONT face=Arial><FONT size=1><FONT style="BACKGROUND-COLOR: #ffffff"><FONT color=#000000><STRONG>18．要找到某月中所有周五的具体日期</STRONG> </FONT></FONT></FONT></FONT></P>
<P class=code><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>select to_char(t.d,'YY-MM-DD') from ( <BR>select trunc(sysdate, 'MM')+rownum-1 as d <BR>from dba_objects <BR>where rownum &lt; 32) t <BR>where to_char(t.d, 'MM') = to_char(sysdate, 'MM') --找出当前月份的周五的日期<BR>and trim(to_char(t.d, 'Day')) = '星期五' </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>-------- </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>03-05-02 </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>03-05-09 </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>03-05-16 </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>03-05-23 </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>03-05-30 </FONT></P>
<P><FONT style="BACKGROUND-COLOR: #ffffff" face=Arial color=#000000 size=1>如果把where to_char(t.d, 'MM') = to_char(sysdate, 'MM')改成sysdate-90，即为查找当前月份的前三个月中的每周五的日期。</FONT></P></STRONG><img src ="http://www.blogjava.net/guanlong/aggbug/24923.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/guanlong/" target="_blank">Stephen</a> 2005-12-21 11:02 <a href="http://www.blogjava.net/guanlong/archive/2005/12/21/24923.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一条oracle的查询语句</title><link>http://www.blogjava.net/guanlong/archive/2005/12/09/23113.html</link><dc:creator>Stephen</dc:creator><author>Stephen</author><pubDate>Fri, 09 Dec 2005 03:31:00 GMT</pubDate><guid>http://www.blogjava.net/guanlong/archive/2005/12/09/23113.html</guid><wfw:comment>http://www.blogjava.net/guanlong/comments/23113.html</wfw:comment><comments>http://www.blogjava.net/guanlong/archive/2005/12/09/23113.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/guanlong/comments/commentRss/23113.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/guanlong/services/trackbacks/23113.html</trackback:ping><description><![CDATA[表user_test 结构:<BR>user_id| user_cname |&nbsp; user_age| others.....<BR>name有重复项,Id为主键,age为number,others为其他很多字段<BR>求一查询<BR>1)Name不能重复,<BR>2)如果Name有重复去年龄最大的一条数据<BR>3)要求能查到,包括ID和others其他字段<BR><BR>在csdn上的网友帮我完成了一种答案:<BR>select * from user_test where user_id in(<BR>select max(user_id) from(<BR>select a.user_id, a.user_cname , a.user_age&nbsp; from user_test a ,<BR>(select user_cname, max(user_age) as maxage from user_test group by user_cname ) b <BR>where a.user_cname = b.user_cname and a.user_age = b.maxage)<BR>&nbsp;group by user_cname)<BR><BR>这种答案能满足要求,但是好像嵌套过多,呵呵,也许会影响效率<BR><BR>我在itpub上,一位网友给了另一种答案:<BR>select * from (select user_id, user_cname, user_age, <BR>row_number() over(<BR>partition by user_cname order by user_age desc<BR>) rn<BR>from user_test)<BR>where rn =1<BR><BR>so cool ,isn't it?<img src ="http://www.blogjava.net/guanlong/aggbug/23113.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/guanlong/" target="_blank">Stephen</a> 2005-12-09 11:31 <a href="http://www.blogjava.net/guanlong/archive/2005/12/09/23113.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>字符大对象Blob </title><link>http://www.blogjava.net/guanlong/archive/2005/11/20/20624.html</link><dc:creator>Stephen</dc:creator><author>Stephen</author><pubDate>Sat, 19 Nov 2005 16:19:00 GMT</pubDate><guid>http://www.blogjava.net/guanlong/archive/2005/11/20/20624.html</guid><wfw:comment>http://www.blogjava.net/guanlong/comments/20624.html</wfw:comment><comments>http://www.blogjava.net/guanlong/archive/2005/11/20/20624.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/guanlong/comments/commentRss/20624.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/guanlong/services/trackbacks/20624.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n...&nbsp;&nbsp;<a href='http://www.blogjava.net/guanlong/archive/2005/11/20/20624.html'>阅读全文</a><img src ="http://www.blogjava.net/guanlong/aggbug/20624.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/guanlong/" target="_blank">Stephen</a> 2005-11-20 00:19 <a href="http://www.blogjava.net/guanlong/archive/2005/11/20/20624.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Oracle里时间的应用</title><link>http://www.blogjava.net/guanlong/archive/2005/04/25/3701.html</link><dc:creator>Stephen</dc:creator><author>Stephen</author><pubDate>Mon, 25 Apr 2005 05:47:00 GMT</pubDate><guid>http://www.blogjava.net/guanlong/archive/2005/04/25/3701.html</guid><wfw:comment>http://www.blogjava.net/guanlong/comments/3701.html</wfw:comment><comments>http://www.blogjava.net/guanlong/archive/2005/04/25/3701.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/guanlong/comments/commentRss/3701.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/guanlong/services/trackbacks/3701.html</trackback:ping><description><![CDATA[1、转换函数<BR>与date操作关系最大的就是两个转换函数：to_date(),to_char()<BR>to_date() 作用将字符类型按一定格式转化为日期类型：<BR>具体用法:to_date('2004-11-27','yyyy-mm-dd'),前者为字符串，后者为转换日期格式，注意，前后两者要以一对应。<BR>如;to_date('2004-11-27 13:34:43', 'yyyy-mm-dd hh24:mi:ss') 将得到具体的时间<BR><BR>多种日期格式：<BR><BR>YYYY：四位表示的年份 <BR>YYY，YY，Y：年份的最后三位、两位或一位，缺省为当前世纪 <BR>MM：01~12的月份编号 <BR>MONTH：九个字符表示的月份，右边用空格填补 <BR>MON：三位字符的月份缩写 <BR>WW：一年中的星期 <BR>D：星期中的第几天 <BR>DD：月份中的第几天 <BR>DDD：年所中的第几天 <BR>DAY：九个字符表示的天的全称，右边用空格补齐 <BR>HH，HH12：一天中的第几个小时，12进制表示法 <BR>HH24：一天中的第几个小时，取值为00~23 <BR>MI：一小时中的分钟 <BR>SS：一分钟中的秒 <BR>SSSS：从午夜开始过去的秒数 <BR><BR>to_char():将日期转按一定格式换成字符类型<BR>SQL&gt; select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') time from dual;<BR><BR>TIME<BR>-------------------<BR>2004-10-08 15:22:58<BR><BR>即把当前时间按yyyy-mm-dd hh24:mi:ss格式转换成字符类型<BR><BR>在oracle中处理日期大全 <BR><BR>&nbsp;&nbsp;TO_DATE格式&nbsp;&nbsp;<BR>Day:&nbsp;&nbsp;<BR>dd number 12&nbsp;&nbsp;<BR>dy abbreviated fri&nbsp;&nbsp;<BR>day spelled out friday&nbsp;&nbsp;<BR>ddspth spelled out, ordinal twelfth&nbsp;&nbsp;<BR>Month:&nbsp;&nbsp;<BR>mm number 03&nbsp;&nbsp;<BR>mon abbreviated mar&nbsp;&nbsp;<BR>month spelled out march&nbsp;&nbsp;<BR>Year:&nbsp;&nbsp;<BR>yy two digits 98&nbsp;&nbsp;<BR>yyyy four digits 1998&nbsp;&nbsp;<BR><BR>24小时格式下时间范围为： 0:00:00 - 23:59:59....&nbsp;&nbsp;<BR>12小时格式下时间范围为： 1:00:00 - 12:59:59 ....&nbsp;&nbsp;<BR><BR><SPAN class=bold><SPAN class=smalltxt><STRONG><FONT face="Comic Sans MS">[ZT]日期和時間函數匯總</FONT></STRONG></SPAN></SPAN><BR>1.&nbsp;&nbsp;<BR>日期和字符转换函数用法（to_date,to_char）&nbsp;&nbsp;<BR><BR>2.&nbsp;&nbsp;<BR>select to_char( to_date(222,'J'),'Jsp') from dual&nbsp;&nbsp;<BR><BR>显示Two Hundred Twenty-Two&nbsp;&nbsp;<BR><BR>3.&nbsp;&nbsp;<BR>求某天是星期几&nbsp;&nbsp;<BR>select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day') from dual;&nbsp;&nbsp;<BR>星期一&nbsp;&nbsp;<BR>select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual;&nbsp;&nbsp;<BR>monday&nbsp;&nbsp;<BR>设置日期语言&nbsp;&nbsp;<BR>ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN';&nbsp;&nbsp;<BR>也可以这样&nbsp;&nbsp;<BR>TO_DATE ('2002-08-26', 'YYYY-mm-dd', 'NLS_DATE_LANGUAGE = American')&nbsp;&nbsp;<BR><BR>4.&nbsp;&nbsp;<BR>两个日期间的天数&nbsp;&nbsp;<BR>select floor(sysdate - to_date('20020405','yyyymmdd')) from dual;&nbsp;&nbsp;<BR><BR>5. 时间为null的用法&nbsp;&nbsp;<BR>select id, active_date from table1&nbsp;&nbsp;<BR>UNION&nbsp;&nbsp;<BR>select 1, TO_DATE(null) from dual;&nbsp;&nbsp;<BR><BR>注意要用TO_DATE(null)&nbsp;&nbsp;<BR><BR>6.&nbsp;&nbsp;<BR>a_date between to_date('20011201','yyyymmdd') and to_date('20011231','yyyymmdd')&nbsp;&nbsp;<BR>那么12月31号中午12点之后和12月1号的12点之前是不包含在这个范围之内的。&nbsp;&nbsp;<BR>所以，当时间需要精确的时候，觉得to_char还是必要的&nbsp;&nbsp;<BR>7. 日期格式冲突问题&nbsp;&nbsp;<BR>输入的格式要看你安装的ORACLE字符集的类型, 比如: US7ASCII, date格式的类型就是: '01-Jan-01'&nbsp;&nbsp;<BR>alter system set NLS_DATE_LANGUAGE = American&nbsp;&nbsp;<BR>alter session set NLS_DATE_LANGUAGE = American&nbsp;&nbsp;<BR>或者在to_date中写&nbsp;&nbsp;<BR>select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual;&nbsp;&nbsp;<BR>注意我这只是举了NLS_DATE_LANGUAGE，当然还有很多，&nbsp;&nbsp;<BR>可查看&nbsp;&nbsp;<BR>select * from nls_session_parameters&nbsp;&nbsp;<BR>select * from V$NLS_PARAMETERS&nbsp;&nbsp;<BR><BR>8.&nbsp;&nbsp;<BR>select count(*)&nbsp;&nbsp;<BR>from ( select rownum-1 rnum&nbsp;&nbsp;<BR>from all_objects&nbsp;&nbsp;<BR>where rownum &lt;= to_date('2002-02-28','yyyy-mm-dd') - to_date('2002-&nbsp;&nbsp;<BR>02-01','yyyy-mm-dd')+1&nbsp;&nbsp;<BR>)&nbsp;&nbsp;<BR>where to_char( to_date('2002-02-01','yyyy-mm-dd')+rnum-1, 'D' )&nbsp;&nbsp;<BR>not&nbsp;&nbsp;<BR>in ( '1', '7' )&nbsp;&nbsp;<BR><BR>查找2002-02-28至2002-02-01间除星期一和七的天数&nbsp;&nbsp;<BR>在前后分别调用DBMS_UTILITY.GET_TIME, 让后将结果相减(得到的是1/100秒, 而不是毫秒).&nbsp;&nbsp;<BR><BR>9.&nbsp;&nbsp;<BR>select months_between(to_date('01-31-1999','MM-DD-YYYY'),&nbsp;&nbsp;<BR>to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL;&nbsp;&nbsp;<BR>1&nbsp;&nbsp;<BR><BR>select months_between(to_date('02-01-1999','MM-DD-YYYY'),&nbsp;&nbsp;<BR>to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL;&nbsp;&nbsp;<BR><BR>1.03225806451613&nbsp;&nbsp;<BR>10. Next_day的用法&nbsp;&nbsp;<BR>Next_day(date, day)&nbsp;&nbsp;<BR><BR>Monday-Sunday, for format code DAY&nbsp;&nbsp;<BR>Mon-Sun, for format code DY&nbsp;&nbsp;<BR>1-7, for format code D&nbsp;&nbsp;<BR><BR>11&nbsp;&nbsp;<BR>select to_char(sysdate,'hh:mi:ss') TIME from all_objects&nbsp;&nbsp;<BR>注意：第一条记录的TIME 与最后一行是一样的&nbsp;&nbsp;<BR>可以建立一个函数来处理这个问题&nbsp;&nbsp;<BR>create or replace function sys_date return date is&nbsp;&nbsp;<BR>begin&nbsp;&nbsp;<BR>return sysdate;&nbsp;&nbsp;<BR>end;&nbsp;&nbsp;<BR><BR>select to_char(sys_date,'hh:mi:ss') from all_objects;&nbsp;&nbsp;<BR>12.&nbsp;&nbsp;<BR>获得小时数&nbsp;&nbsp;<BR><BR>SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 2:38:40') from offer&nbsp;&nbsp;<BR>SQL&gt; select sysdate ,to_char(sysdate,'hh') from dual;&nbsp;&nbsp;<BR><BR>SYSDATE TO_CHAR(SYSDATE,'HH')&nbsp;&nbsp;<BR>-------------------- ---------------------&nbsp;&nbsp;<BR>2003-10-13 19:35:21 07&nbsp;&nbsp;<BR><BR>SQL&gt; select sysdate ,to_char(sysdate,'hh24') from dual;&nbsp;&nbsp;<BR><BR>SYSDATE TO_CHAR(SYSDATE,'HH24')&nbsp;&nbsp;<BR>-------------------- -----------------------&nbsp;&nbsp;<BR>2003-10-13 19:35:21 19&nbsp;&nbsp;<BR><BR>获取年月日与此类似&nbsp;&nbsp;<BR>13.&nbsp;&nbsp;<BR>年月日的处理&nbsp;&nbsp;<BR>select older_date,&nbsp;&nbsp;<BR>newer_date,&nbsp;&nbsp;<BR>years,&nbsp;&nbsp;<BR>months,&nbsp;&nbsp;<BR>abs(&nbsp;&nbsp;<BR>trunc(&nbsp;&nbsp;<BR>newer_date-&nbsp;&nbsp;<BR>add_months( older_date,years*12+months )&nbsp;&nbsp;<BR>)&nbsp;&nbsp;<BR>) days&nbsp;&nbsp;<BR>from ( select&nbsp;&nbsp;<BR>trunc(months_between( newer_date, older_date )/12) YEARS,&nbsp;&nbsp;<BR>mod(trunc(months_between( newer_date, older_date )),&nbsp;&nbsp;<BR>12 ) MONTHS,&nbsp;&nbsp;<BR>newer_date,&nbsp;&nbsp;<BR>older_date&nbsp;&nbsp;<BR>from ( select hiredate older_date,&nbsp;&nbsp;<BR>add_months(hiredate,rownum)+rownum newer_date&nbsp;&nbsp;<BR>from emp )&nbsp;&nbsp;<BR>)&nbsp;&nbsp;<BR><BR>14.&nbsp;&nbsp;<BR>处理月份天数不定的办法&nbsp;&nbsp;<BR>select to_char(add_months(last_day(sysdate) +1, -2), 'yyyymmdd'),last_day(sysdate) from dual&nbsp;&nbsp;<BR><BR>16.&nbsp;&nbsp;<BR>找出今年的天数&nbsp;&nbsp;<BR>select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from dual&nbsp;&nbsp;<BR><BR>闰年的处理方法&nbsp;&nbsp;<BR>to_char( last_day( to_date('02' || :year,'mmyyyy') ), 'dd' )&nbsp;&nbsp;<BR>如果是28就不是闰年&nbsp;&nbsp;<BR><BR>17.&nbsp;&nbsp;<BR>yyyy与rrrr的区别&nbsp;&nbsp;<BR>'YYYY99 TO_C&nbsp;&nbsp;<BR>------- ----&nbsp;&nbsp;<BR>yyyy 99 0099&nbsp;&nbsp;<BR>rrrr 99 1999&nbsp;&nbsp;<BR>yyyy 01 0001&nbsp;&nbsp;<BR>rrrr 01 2001&nbsp;&nbsp;<BR><BR>18.不同时区的处理&nbsp;&nbsp;<BR>select to_char( NEW_TIME( sysdate, 'GMT','EST'), 'dd/mm/yyyy hh:mi:ss') ,sysdate&nbsp;&nbsp;<BR>from dual;&nbsp;&nbsp;<BR><BR>19.&nbsp;&nbsp;<BR>5秒钟一个间隔&nbsp;&nbsp;<BR>Select TO_DATE(FLOOR(TO_CHAR(sysdate,'SSSSS')/300) * 300,'SSSSS') ,TO_CHAR(sysdate,'SSSSS')&nbsp;&nbsp;<BR>from dual&nbsp;&nbsp;<BR><BR>2002-11-1 9:55:00 35786&nbsp;&nbsp;<BR>SSSSS表示5位秒数&nbsp;&nbsp;<BR><BR>20.&nbsp;&nbsp;<BR>一年的第几天&nbsp;&nbsp;<BR>select TO_CHAR(SYSDATE,'DDD'),sysdate from dual&nbsp;&nbsp;<BR>310 2002-11-6 10:03:51&nbsp;&nbsp;<BR><BR>21.计算小时,分,秒,毫秒&nbsp;&nbsp;<BR>select&nbsp;&nbsp;<BR>Days,&nbsp;&nbsp;<BR>A,&nbsp;&nbsp;<BR>TRUNC(A*24) Hours,&nbsp;&nbsp;<BR>TRUNC(A*24*60 - 60*TRUNC(A*24)) Minutes,&nbsp;&nbsp;<BR>TRUNC(A*24*60*60 - 60*TRUNC(A*24*60)) Seconds,&nbsp;&nbsp;<BR>TRUNC(A*24*60*60*100 - 100*TRUNC(A*24*60*60)) mSeconds&nbsp;&nbsp;<BR>from&nbsp;&nbsp;<BR>(&nbsp;&nbsp;<BR>select&nbsp;&nbsp;<BR>trunc(sysdate) Days,&nbsp;&nbsp;<BR>sysdate - trunc(sysdate) A&nbsp;&nbsp;<BR>from dual&nbsp;&nbsp;<BR>)&nbsp;&nbsp;<BR><BR><BR><BR>select * from tabname&nbsp;&nbsp;<BR>order by decode(mode,'FIFO',1,-1)*to_char(rq,'yyyymmddhh24miss');&nbsp;&nbsp;<BR><BR>//&nbsp;&nbsp;<BR>floor((date2-date1) /365) 作为年&nbsp;&nbsp;<BR>floor((date2-date1, 365) /30) 作为月&nbsp;&nbsp;<BR>mod(mod(date2-date1, 365), 30)作为日.&nbsp;&nbsp;<BR>23.next_day函数&nbsp;&nbsp;<BR>next_day(sysdate,6)是从当前开始下一个星期五。后面的数字是从星期日开始算起。&nbsp;&nbsp;<BR>1 2 3 4 5 6 7&nbsp;&nbsp;<BR>日 一 二 三 四 五 六<BR><BR><SPAN class=bold><SPAN class=smalltxt><STRONG><FONT face="Comic Sans MS">oracle中有很多关于日期的函数<BR><BR></FONT></STRONG></SPAN></SPAN>在oracle中有很多关于日期的函数，如： <BR>1、add_months()用于从一个日期值增加或减少一些月份 <BR>date_value:=add_months(date_value,number_of_months) <BR>例： <BR>SQL&gt; select add_months(sysdate,12) "Next Year" from dual; <BR><BR>Next Year <BR>---------- <BR>13-11月-04 <BR><BR>SQL&gt; select add_months(sysdate,112) "Last Year" from dual; <BR><BR>Last Year <BR>---------- <BR>13-3月 -13 <BR><BR>SQL&gt; <BR><BR>2、current_date()返回当前会放时区中的当前日期 <BR>date_value:=current_date <BR>SQL&gt; column sessiontimezone for a15 <BR>SQL&gt; select sessiontimezone,current_date from dual; <BR><BR>SESSIONTIMEZONE CURRENT_DA <BR>--------------- ---------- <BR>+08:00 13-11月-03 <BR><BR>SQL&gt; alter session set time_zone='-11:00' <BR>&nbsp;&nbsp;2 / <BR><BR>会话已更改。 <BR><BR>SQL&gt; select sessiontimezone,current_timestamp from dual; <BR><BR>SESSIONTIMEZONE CURRENT_TIMESTAMP <BR>--------------- ------------------------------------ <BR>-11:00 12-11月-03 04.59.13.668000 下午 -11: <BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 00 <BR><BR>SQL&gt; <BR><BR>3、current_timestamp()以timestamp with time zone数据类型返回当前会放时区中的当前日期 <BR>timestamp_with_time_zone_value:=current_timestamp([timestamp_precision]) <BR>SQL&gt; column sessiontimezone for a15 <BR>SQL&gt; column current_timestamp format a36 <BR>SQL&gt; select sessiontimezone,current_timestamp from dual; <BR><BR>SESSIONTIMEZONE CURRENT_TIMESTAMP <BR>--------------- ------------------------------------ <BR>+08:00 13-11月-03 11.56.28.160000 上午 +08: <BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 00 <BR><BR>SQL&gt; alter session set time_zone='-11:00' <BR>&nbsp;&nbsp;2 / <BR><BR>会话已更改。 <BR><BR>SQL&gt; select sessiontimezone,current_timestamp from dual; <BR><BR>SESSIONTIMEZONE CURRENT_TIMESTAMP <BR>--------------- ------------------------------------ <BR>-11:00 12-11月-03 04.58.00.243000 下午 -11: <BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 00 <BR><BR>SQL&gt; <BR><BR>4、dbtimezone()返回时区 <BR>varchar_value:=dbtimezone <BR>SQL&gt; select dbtimezone from dual; <BR><BR>DBTIME <BR>------ <BR>-07:00 <BR><BR>SQL&gt; <BR><BR>5、extract()找出日期或间隔值的字段值 <BR>date_value:=extract(date_field from [datetime_value|interval_value]) <BR>SQL&gt; select extract(month from sysdate) "This Month" from dual; <BR><BR>This Month <BR>---------- <BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;11 <BR><BR>SQL&gt; select extract(year from add_months(sysdate,36)) "3 Years Out" from dual; <BR><BR>3 Years Out <BR>----------- <BR>&nbsp; &nbsp;&nbsp; &nbsp; 2006 <BR><BR>SQL&gt; <BR><BR>6、last_day()返回包含了日期参数的月份的最后一天的日期 <BR>date_value:=last_day(date_value) <BR>SQL&gt; select last_day(date'2000-02-01') "Leap Yr?" from dual; <BR><BR>Leap Yr? <BR>---------- <BR>29-2月 -00 <BR><BR>SQL&gt; select last_day(sysdate) "Last day of this month" from dual; <BR><BR>Last day o <BR>---------- <BR>30-11月-03 <BR><BR>SQL&gt; <BR><BR>7、localtimestamp()返回会话中的日期和时间 <BR>timestamp_value:=localtimestamp <BR>SQL&gt; column localtimestamp format a28 <BR>SQL&gt; select localtimestamp from dual; <BR><BR>LOCALTIMESTAMP <BR>---------------------------- <BR>13-11月-03 12.09.15.433000 <BR>下午 <BR><BR>SQL&gt; select localtimestamp,current_timestamp from dual; <BR><BR>LOCALTIMESTAMP CURRENT_TIMESTAMP <BR>---------------------------- ------------------------------------ <BR>13-11月-03 12.09.31.006000 13-11月-03 12.09.31.006000 下午 +08: <BR>下午 00 <BR><BR>SQL&gt; alter session set time_zone='-11:00'; <BR><BR>会话已更改。 <BR><BR>SQL&gt; select localtimestamp,to_char(sysdate,'DD-MM-YYYY HH:MI:SS AM') "SYSDATE" from dual; <BR><BR>LOCALTIMESTAMP SYSDATE <BR>---------------------------- ------------------------ <BR>12-11月-03 05.11.31.259000 13-11-2003 12:11:31 下午 <BR>下午 <BR><BR>SQL&gt; <BR><BR>8、months_between()判断两个日期之间的月份数量 <BR>number_value:=months_between(date_value,date_value) <BR>SQL&gt; select months_between(sysdate,date'1971-05-18') from dual; <BR><BR>MONTHS_BETWEEN(SYSDATE,DATE'1971-05-18') <BR>---------------------------------------- <BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;389.855143 <BR><BR>SQL&gt; select months_between(sysdate,date'2001-01-01') from dual; <BR><BR>MONTHS_BETWEEN(SYSDATE,DATE'2001-01-01') <BR>---------------------------------------- <BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;34.4035409 <BR><BR>SQL&gt; <BR><BR>9、next_day()给定一个日期值，返回由第二个参数指出的日子第一次出现在的日期值（应返回相应日子的名称字符串）<BR><BR>
<TABLE style="TABLE-LAYOUT: fixed; WORD-WRAP: break-word" height="100%" cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD vAlign=top><SPAN class=bold><SPAN class=smalltxt><STRONG><FONT face="Comic Sans MS">與周相關日期函數<BR><BR></FONT></STRONG></SPAN></SPAN>1.查询某周的第一天<BR>select trunc(decode(ww, 53, to_date(yy || '3112', 'yyyyddmm'), to_date(yy || '-' || to_char(ww * 7), 'yyyy-ddd')), 'd') last_day<BR>from (select substr('2004-32', 1, 4) yy, to_number(substr('2004-32', 6)) ww<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;from dual)<BR><BR>select trunc(to_date(substr('2003-01',1,5)||to_char((to_number(substr('2003-01',6)))*7),'yyyy-ddd'),'d')-6 first_day from dual<BR><BR>select min(v_date) from<BR>&nbsp;&nbsp;(select (to_date('200201','yyyymm') + rownum) v_date<BR>&nbsp;&nbsp;from all_tables<BR>&nbsp;&nbsp;where rownum &lt; 370)<BR>where to_char(v_date,'yyyy-iw') = '2002-49'<BR><BR>2.查询某周的最后一天<BR>select trunc(decode(ww, 53, to_date(yy || '3112', 'yyyyddmm'), to_date(yy || '-' || to_char(ww * 7), 'yyyy-ddd')), 'd') - 6 first_day<BR>&nbsp;&nbsp;from (select substr('2004-33', 1, 4) yy, to_number(substr('2004-33', 6)) ww<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; from dual)<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; <BR>select trunc(to_date(substr('2003-01',1,5)||to_char((to_number(substr('2003-01',6)))*7),'yyyy-ddd'),'d') last_day from dual<BR><BR>select max(v_date) from<BR>&nbsp;&nbsp;(select (to_date('200408','yyyymm') + rownum) v_date<BR>&nbsp;&nbsp;from all_tables<BR>&nbsp;&nbsp;where rownum &lt; 370)<BR>where to_char(v_date,'yyyy-iw') = '2004-33'<BR><BR>3.查询某周的日期<BR>select min_date, to_char(min_date,'day') day from<BR>(select to_date(substr('2004-33',1,4)||'001'+rownum-1,'yyyyddd') min_date <BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;from all_tables<BR>&nbsp;&nbsp;where rownum &lt;= decode(mod(to_number(substr('2004-33',1,4)),4),0,366,365)&nbsp;&nbsp;<BR>&nbsp;&nbsp;union<BR><BR>&nbsp;&nbsp;select to_date(substr('2004-33',1,4)-1||<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;decode(mod(to_number(substr('2004-33',1,4))-1,4),0,359,358)+rownum,'yyyyddd') min_date <BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;from all_tables&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; <BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; where rownum &lt;= 7<BR>&nbsp;&nbsp;union<BR><BR>&nbsp;&nbsp;select to_date(substr('2004-33',1,4)+1||'001'+rownum-1,'yyyyddd') min_date <BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;from all_tables&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; <BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; where rownum &lt;= 7&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<BR>)<BR>where to_char(min_date,'yyyy-iw') ='2004-33'</TD></TR></TBODY></TABLE><img src ="http://www.blogjava.net/guanlong/aggbug/3701.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/guanlong/" target="_blank">Stephen</a> 2005-04-25 13:47 <a href="http://www.blogjava.net/guanlong/archive/2005/04/25/3701.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>