﻿<?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-&lt;DIV id="ddm_Title"&gt;&lt;H1&gt;大大毛&amp;nbsp;&lt;i class="little"&gt;的笔记&lt;/i&gt;&lt;/H1&gt;&lt;H2&gt;&amp;nbsp&amp;nbsp;DDM's Note&lt;/H2&gt;&lt;/DIV&gt;-文章分类-&lt;b value="10" class="ddm_Item"&gt;SQL&lt;/b&gt;</title><link>http://www.blogjava.net/tw-ddm/category/14127.html</link><description>&lt;div id="ddm_subTitle"&gt;
&lt;h2 class="catchline"&gt;
        哪怕没有办法一定有说法,&lt;br/&gt;
        就算没有鸽子一定有乌鸦,&lt;br/&gt;
        固执无罪&amp;nbsp;梦想有价,&lt;br/&gt;
        让他们惊讶.
&lt;/h2&gt;
&lt;/div&gt;</description><language>zh-cn</language><lastBuildDate>Sat, 08 Sep 2007 06:29:43 GMT</lastBuildDate><pubDate>Sat, 08 Sep 2007 06:29:43 GMT</pubDate><ttl>60</ttl><item><title>實例解析如何實現行間運算</title><link>http://www.blogjava.net/tw-ddm/articles/142572.html</link><dc:creator>大大毛</dc:creator><author>大大毛</author><pubDate>Tue, 04 Sep 2007 03:37:00 GMT</pubDate><guid>http://www.blogjava.net/tw-ddm/articles/142572.html</guid><wfw:comment>http://www.blogjava.net/tw-ddm/comments/142572.html</wfw:comment><comments>http://www.blogjava.net/tw-ddm/articles/142572.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/tw-ddm/comments/commentRss/142572.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/tw-ddm/services/trackbacks/142572.html</trackback:ping><description><![CDATA[
		<font face="Georgia">
				<br />
				<font size="2">
						<strong>問題</strong>
						<br />
						<br />      通常來說實現表級的關聯以及在列之間實現運算還算是比較容易，可是利用SQL來實現行間的關聯運算就顯得比較困難，這裏就一個實例來解析如何來思考及解決此類的問題。<br /><br /></font>
				<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
						<font size="2">
								<img id="Code_Closed_Image_094427" onclick="this.style.display='none'; Code_Closed_Text_094427.style.display='none'; Code_Open_Image_094427.style.display='inline'; Code_Open_Text_094427.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" />
								<img id="Code_Open_Image_094427" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_094427.style.display='none'; Code_Closed_Image_094427.style.display='inline'; Code_Closed_Text_094427.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" />
								<span id="Code_Closed_Text_094427" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">表結構</span>
						</font>
						<span id="Code_Open_Text_094427" style="DISPLAY: none">
								<br />
								<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
								<font size="2">
										<span style="COLOR: #0000ff">Create</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Table</span>
								</font>
								<font size="2">
										<span style="COLOR: #000000"> tb_ExampleA (<br />  kID     </span>
										<span style="FONT-WEIGHT: bold; COLOR: #000000">Char</span>
										<span style="COLOR: #000000">(</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">10</span>
										<span style="COLOR: #000000">),   </span>
										<span style="COLOR: #008080">--</span>
										<span style="COLOR: #008080">合約ID,主KEY</span>
								</font>
								<span style="COLOR: #008080">
										<br />
								</span>
								<font size="2">
										<span style="COLOR: #000000">  iYear   </span>
										<span style="FONT-WEIGHT: bold; COLOR: #000000">Int</span>
										<span style="COLOR: #000000">,        </span>
										<span style="COLOR: #008080">--</span>
										<span style="COLOR: #008080">第幾年度</span>
								</font>
								<span style="COLOR: #008080">
										<br />
								</span>
								<font size="2">
										<span style="COLOR: #000000">  iTime   </span>
										<span style="FONT-WEIGHT: bold; COLOR: #000000">Int</span>
										<span style="COLOR: #000000">,        </span>
										<span style="COLOR: #008080">--</span>
										<span style="COLOR: #008080">第幾期</span>
								</font>
								<span style="COLOR: #008080">
										<br />
								</span>
								<font size="2">
										<span style="COLOR: #000000">  D1      </span>
										<span style="FONT-WEIGHT: bold; COLOR: #000000">Int</span>
										<span style="COLOR: #000000">,        </span>
										<span style="COLOR: #008080">--</span>
										<span style="COLOR: #008080">開始日期</span>
								</font>
								<span style="COLOR: #008080">
										<br />
								</span>
								<font size="2">
										<span style="COLOR: #000000">  PayMode </span>
										<span style="FONT-WEIGHT: bold; COLOR: #000000">Char</span>
										<span style="COLOR: #000000">(</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
										<span style="COLOR: #000000">),    </span>
										<span style="COLOR: #008080">--</span>
										<span style="COLOR: #008080">付款方式</span>
								</font>
								<span style="COLOR: #008080">
										<br />
								</span>
								<font size="2">
										<span style="COLOR: #000000">  cOther  </span>
										<span style="FONT-WEIGHT: bold; COLOR: #000000">Char</span>
										<span style="COLOR: #000000">(</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">100</span>
										<span style="COLOR: #000000">),  </span>
										<span style="COLOR: #008080">--</span>
										<span style="COLOR: #008080">內容</span>
								</font>
								<span style="COLOR: #008080">
										<br />
								</span>
								<font size="2">
										<span style="COLOR: #000000">  </span>
										<span style="COLOR: #0000ff">Primary</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Key</span>
								</font>
								<span style="COLOR: #000000">
										<font size="2">(kID,iYear,iMonth)<br />);</font>
								</span>
						</span>
				</div>
				<br />
				<font size="2">      表結構說明<br />         kID               主關鍵字，例如合約號<br />         iYear            第幾年度( &gt;= 1)，連續遞增<br />         iTime           第幾期[1, 12]，連續遞增，根據付款方式有所不同<br />         D1                  開始日期，數字類型的民國日期<br />         PayMode   付款方式，取值 A--年繳，S--半年繳，Q--季繳，M--月繳<br />      要求<br />         找到那些 D1 有問題的合約。因為根據PayMode來說相同 kID 的 D1 變化是存在有規律的，例如如果是A--年繳則應該是一年一跳，M--月繳則應該是一個月一跳，因此要找到那些跳的間隔有錯的合約。<br />      數據現狀<br />         iYear / iTime 兩列上的數據就很亂，有跳期的現象存在，例如月繳1年1期--1年3期。<br />         D1 列上有誤，例如月繳方式下  950505 -- 950718 (正確應該是950505 -- 959604)<br /><br />      表結構與 </font>
				<a href="/tw-ddm/articles/134308.html" target="_blank">
						<font size="2">我的上一篇</font>
				</a> <font size="2">示例2中所用表完全相同，只不過上一篇是為了找出年度+月份不連續的問題件，而這裏則是要解決數據行Row之間的對比，不過這一次還是要用到上回講述到的那些解決方案，只不過要更加複雜。<br /><br /><br /><strong>思考</strong><br /><br />      1. 要達到間隔的計算，就要實現行間數據的對比，因此大體的思路應該是使用自連接的方式來完成。<br />      2. 根據數據現狀可以看出，上一篇中提到的將 iYear + iTime 兩列合併的方案已經不可用，因為根據現狀這兩列雖然保持唯一和增加，但是增加的 Step(步長) 已經不可信任。根據自連接的特點，我們必須找到一個絕對可以信賴的遞增列，然後再在該列上實現自連接，問題是這一列該如何選取呢？<br />      3. D1 列是數字值的民國日期，要用日期的間隔來判斷需要先將該列轉換成正常的西元日期型列才可以實現。<br /><br /><br /><strong>解決方法<br /></strong><br />      1. 找到那個 Step 可以信賴的遞增列。<br />         既然 iYear + iTime 的 Step 不可信賴可是兩列唯一和增加還是可用的，因為可以用 <a href="/tw-ddm/articles/93929.html" target="_blank">自行構建Identity的方法</a> 來據此生成一個可用的 Identity 列。<br />      2. D1的西元日期型轉換，可以用 <a href="/tw-ddm/articles/142418.html" target="_blank">幾個用於SQL的日期轉換函數(vb)</a> 中功能2所示的 parseDate(strSQL) 來實現。<br />      3. 問題日期的間隔這裏只簡單的按大於32天/月來判斷。<br /><br /><strong>SQL語句</strong><br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img id="Code_Closed_Image_112528" onclick="this.style.display='none'; Code_Closed_Text_112528.style.display='none'; Code_Open_Image_112528.style.display='inline'; Code_Open_Text_112528.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" /><img id="Code_Open_Image_112528" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_112528.style.display='none'; Code_Closed_Image_112528.style.display='inline'; Code_Closed_Text_112528.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" /><span id="Code_Closed_Text_112528" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">SQL語句</span><span id="Code_Open_Text_112528" style="DISPLAY: none"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: #008080"> 1</span> <span style="COLOR: #0000ff">Select</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">*</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">From</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 2</span> <span style="COLOR: #000000">  (</span><span style="COLOR: #0000ff">Select</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080"> 3</span> <span style="COLOR: #000000">        A0.</span><span style="COLOR: #808080">*</span><span style="COLOR: #000000">,<br /></span><span style="COLOR: #008080"> 4</span> <span style="COLOR: #000000">        (</span><span style="COLOR: #0000ff">Select</span><span style="COLOR: #000000"> <br /></span><span style="COLOR: #008080"> 5</span> <span style="COLOR: #000000">            </span><span style="COLOR: #ff00ff">Count</span><span style="COLOR: #000000">(</span><span style="COLOR: #808080">*</span><span style="COLOR: #000000">) <br /></span><span style="COLOR: #008080"> 6</span> <span style="COLOR: #000000">          </span><span style="COLOR: #0000ff">From</span><span style="COLOR: #000000"> <br /></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #000000">            LIB</span><span style="COLOR: #808080">/</span><span style="COLOR: #000000">tb_ExampleA subA<br /></span><span style="COLOR: #008080"> 8</span> <span style="COLOR: #000000">          </span><span style="COLOR: #0000ff">Where</span><span style="COLOR: #000000"> <br /></span><span style="COLOR: #008080"> 9</span> <span style="COLOR: #000000">            subA.kID </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> A0.kID<br /></span><span style="COLOR: #008080">10</span> <span style="COLOR: #000000">            </span><span style="COLOR: #808080">And</span><span style="COLOR: #000000"> subA.iYear</span><span style="COLOR: #808080">*</span><span style="FONT-WEIGHT: bold; COLOR: #800000">100</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">+</span><span style="COLOR: #000000"> subA.iTime </span><span style="COLOR: #808080">&lt;=</span><span style="COLOR: #000000"> A0.iYear</span><span style="COLOR: #808080">*</span><span style="FONT-WEIGHT: bold; COLOR: #800000">100</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">+</span><span style="COLOR: #000000"> A0.iTime<br /></span><span style="COLOR: #008080">11</span> <span style="COLOR: #000000">        ) Index1<br /></span><span style="COLOR: #008080">12</span> <span style="COLOR: #000000">      </span><span style="COLOR: #0000ff">From</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">13</span> <span style="COLOR: #000000">        LIB</span><span style="COLOR: #808080">/</span><span style="COLOR: #000000">tb_ExampleA A0<br /></span><span style="COLOR: #008080">14</span> <span style="COLOR: #000000">  ) AL<br /></span><span style="COLOR: #008080">15</span> <span style="COLOR: #000000">  </span><span style="COLOR: #0000ff">Inner</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Join</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">16</span> <span style="COLOR: #000000">  (</span><span style="COLOR: #0000ff">Select</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">17</span> <span style="COLOR: #000000">        A0.</span><span style="COLOR: #808080">*</span><span style="COLOR: #000000">,<br /></span><span style="COLOR: #008080">18</span> <span style="COLOR: #000000">        (</span><span style="COLOR: #0000ff">Select</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">19</span> <span style="COLOR: #000000">            </span><span style="COLOR: #ff00ff">Count</span><span style="COLOR: #000000">(</span><span style="COLOR: #808080">*</span><span style="COLOR: #000000">)<br /></span><span style="COLOR: #008080">20</span> <span style="COLOR: #000000">          </span><span style="COLOR: #0000ff">From</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">21</span> <span style="COLOR: #000000">            LIB</span><span style="COLOR: #808080">/</span><span style="COLOR: #000000">tb_ExampleA subA<br /></span><span style="COLOR: #008080">22</span> <span style="COLOR: #000000">          </span><span style="COLOR: #0000ff">Where</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">23</span> <span style="COLOR: #000000">            subA.kID </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> A0.kID<br /></span><span style="COLOR: #008080">24</span> <span style="COLOR: #000000">            </span><span style="COLOR: #808080">And</span><span style="COLOR: #000000"> subA.iYear</span><span style="COLOR: #808080">*</span><span style="FONT-WEIGHT: bold; COLOR: #800000">100</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">+</span><span style="COLOR: #000000"> subA.iTime </span><span style="COLOR: #808080">&lt;=</span><span style="COLOR: #000000"> A0.iYear</span><span style="COLOR: #808080">*</span><span style="FONT-WEIGHT: bold; COLOR: #800000">100</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">+</span><span style="COLOR: #000000"> A0.iTime<br /></span><span style="COLOR: #008080">25</span> <span style="COLOR: #000000">        ) Index1<br /></span><span style="COLOR: #008080">26</span> <span style="COLOR: #000000">      </span><span style="COLOR: #0000ff">From</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">27</span> <span style="COLOR: #000000">        LIB</span><span style="COLOR: #808080">/</span><span style="COLOR: #000000">tb_ExampleA A0<br /></span><span style="COLOR: #008080">28</span> <span style="COLOR: #000000">  ) AR<br /></span><span style="COLOR: #008080">29</span> <span style="COLOR: #000000">  </span><span style="COLOR: #0000ff">On</span><span style="COLOR: #000000"> AL.kID </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> AR.kID </span><span style="COLOR: #808080">And</span><span style="COLOR: #000000"> AL.Index1 </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> AR.Index1 </span><span style="COLOR: #808080">+</span><span style="COLOR: #000000"> </span><span style="FONT-WEIGHT: bold; COLOR: #800000">1</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">30</span> <span style="COLOR: #000000">  </span><span style="COLOR: #0000ff">Where</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">31</span> <span style="COLOR: #000000">    </span><span style="COLOR: #008000">@Date</span><span style="COLOR: #000000">(AR.D1) </span><span style="COLOR: #808080">+</span><span style="COLOR: #000000"> (<br /></span><span style="COLOR: #008080">32</span> <span style="COLOR: #000000">      </span><span style="COLOR: #ff00ff">CASE</span><span style="COLOR: #000000"> AR.PayMode<br /></span><span style="COLOR: #008080">33</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">WHEN</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">A</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">THEN</span><span style="COLOR: #000000"> </span><span style="FONT-WEIGHT: bold; COLOR: #800000">12</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">34</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">WHEN</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">S</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">THEN</span><span style="COLOR: #000000"> </span><span style="FONT-WEIGHT: bold; COLOR: #800000">6</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">35</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">WHEN</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">Q</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">THEN</span><span style="COLOR: #000000"> </span><span style="FONT-WEIGHT: bold; COLOR: #800000">3</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">36</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">ELSE</span><span style="COLOR: #000000"> </span><span style="FONT-WEIGHT: bold; COLOR: #800000">1</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">37</span> <span style="COLOR: #000000">      </span><span style="COLOR: #0000ff">END</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">*</span><span style="COLOR: #000000"> </span><span style="FONT-WEIGHT: bold; COLOR: #800000">32</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">38</span> <span style="COLOR: #000000">    ) Days </span><span style="COLOR: #808080">&lt;</span><span style="COLOR: #000000"> </span><span style="COLOR: #008000">@Date</span><span style="COLOR: #000000">(AL.D1)</span></span></div><br />         從該SQL語句可以看到為了實現自連接，構建了2張表 AL(2-14行) 和 AR(16-28行)，兩表的內容完全一致，中間使用了自定義 Identity 列 Index1 (4-11行) ，然後再將AL 與 AR 實現錯值連接(29行)。<br />         對連接後表記錄的日期間隔運算，放在Where子句中(30-38行)，這裏寫的是僞語句，需要用前面提到的 VB函數轉換才可以運行，不過這樣看起來就會明子許多。<br /><br /><br />         可以看到，只要在處理裏靈活運用各種技巧就可以解決更加複雜的邏輯。</font></font>
<img src ="http://www.blogjava.net/tw-ddm/aggbug/142572.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/tw-ddm/" target="_blank">大大毛</a> 2007-09-04 11:37 <a href="http://www.blogjava.net/tw-ddm/articles/142572.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>幾個用於SQL的日期轉換函數(vb)</title><link>http://www.blogjava.net/tw-ddm/articles/142418.html</link><dc:creator>大大毛</dc:creator><author>大大毛</author><pubDate>Mon, 03 Sep 2007 11:56:00 GMT</pubDate><guid>http://www.blogjava.net/tw-ddm/articles/142418.html</guid><wfw:comment>http://www.blogjava.net/tw-ddm/comments/142418.html</wfw:comment><comments>http://www.blogjava.net/tw-ddm/articles/142418.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/tw-ddm/comments/commentRss/142418.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/tw-ddm/services/trackbacks/142418.html</trackback:ping><description><![CDATA[
		<p>
				<font face="Georgia" size="2">
						<br />這幾個小函數很早就寫了，項目做完後沒想到有一天有公司的兄弟說要用，於是就想著放在這上面來。<br /><br />功能1<br />   現在公司接的活是臺灣的，數據庫中免不了要保存民國日期(公元年份 - 1911即為民國日期)，而運算時又要轉成西元日期，因此在運用時會要求進行轉換，通常用兩類。<br />   1. 在程序中轉換，通常用類型轉換函數將日期轉成長整型再減去19110000。<br />   2. 在抓資料時就完成轉換，為了方便我做了兩個小函數來完成。<br />   適用於MS - SQL</font>
		</p>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<img id="Code_Closed_Image_194627" onclick="this.style.display='none'; Code_Closed_Text_194627.style.display='none'; Code_Open_Image_194627.style.display='inline'; Code_Open_Text_194627.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" />
				<img id="Code_Open_Image_194627" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_194627.style.display='none'; Code_Closed_Image_194627.style.display='inline'; Code_Closed_Text_194627.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" />
				<span id="Code_Closed_Text_194627" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">民國日期列轉換為西元列</span>
				<span id="Code_Open_Text_194627" style="DISPLAY: none">
						<br />
						<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<span style="COLOR: #008000">'</span>
						<span style="COLOR: #008000">'****************************************************************************************************************</span>
						<span style="COLOR: #008000">
								<br />'</span>
						<span style="COLOR: #008000">*程式功能  :       將一個民國日期列轉換為西元列</span>
						<span style="COLOR: #008000">
								<br />'</span>
						<span style="COLOR: #008000">*開發人員  :　     ddm 2007/1/13</span>
						<span style="COLOR: #008000">
								<br />'</span>
						<span style="COLOR: #008000">*開發說明  :　     例如 AAABREP.ABACDT列 ,使用 "Select " &amp; MG2XYCol("AAABREP.ABACDT") &amp; " As ABACDT From AAABREP" 轉換</span>
						<span style="COLOR: #008000">
								<br />'</span>
						<span style="COLOR: #008000">*傳入值    :       strColName      --需要轉換的列名</span>
						<span style="COLOR: #008000">
								<br />'</span>
						<span style="COLOR: #008000">*傳入值    :       strDefaultValue --如果不是一個符合的日期時的值</span>
						<span style="COLOR: #008000">
								<br />'</span>
						<span style="COLOR: #008000">*回傳值    :       String          --轉換後的SQL語句</span>
						<span style="COLOR: #008000">
								<br />'</span>
						<span style="COLOR: #008000">'****************************************************************************************************************</span>
						<span style="COLOR: #008000">
								<br />
						</span>
						<span style="COLOR: #0000ff">Public</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">Function</span>
						<span style="COLOR: #000000"> MG2XYCol(strColName </span>
						<span style="COLOR: #0000ff">As</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">String</span>
						<span style="COLOR: #000000">, </span>
						<span style="COLOR: #0000ff">Optional</span>
						<span style="COLOR: #000000"> strDefaultValue </span>
						<span style="COLOR: #0000ff">As</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">String</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">''</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">) </span>
						<span style="COLOR: #0000ff">As</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">String</span>
						<span style="COLOR: #000000">
								<br />    </span>
						<span style="COLOR: #0000ff">Dim</span>
						<span style="COLOR: #000000"> strSql </span>
						<span style="COLOR: #0000ff">As</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">String</span>
						<span style="COLOR: #000000">
								<br />    strSql </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000"> CASE </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">
								<br />    strSql </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> strSql </span>
						<span style="COLOR: #000000">&amp;</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000"> WHEN LEN(LTRIM(RTRIM(ISNULL(@ColName,''))))&gt;=6 THEN</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">
								<br />    strSql </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> strSql </span>
						<span style="COLOR: #000000">&amp;</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000"> CAST((CAST(LEFT(LTRIM(@ColName), LEN(LTRIM(RTRIM(@ColName)))-4) As int) + 1911) As varchar(4))</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">
								<br />    strSql </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> strSql </span>
						<span style="COLOR: #000000">&amp;</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">+LEFT(RIGHT(LTRIM(RTRIM(@ColName)),4),2)</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">
								<br />    strSql </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> strSql </span>
						<span style="COLOR: #000000">&amp;</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">+RIGHT(LTRIM(RTRIM(@ColName)),2)</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">
								<br />    strSql </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> strSql </span>
						<span style="COLOR: #000000">&amp;</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000"> ELSE</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">
								<br />    strSql </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> strSql </span>
						<span style="COLOR: #000000">&amp;</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000"> @DefaultValue</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">
								<br />    strSql </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> strSql </span>
						<span style="COLOR: #000000">&amp;</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000"> END </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">
								<br />    strSql </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">Replace</span>
						<span style="COLOR: #000000">(strSql, </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">@ColName</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">, strColName)<br />    MG2XYCol </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">Replace</span>
						<span style="COLOR: #000000">(strSql, </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">@DefaultValue</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">, strDefaultValue)<br /></span>
						<span style="COLOR: #0000ff">End Function</span>
				</span>
		</div>
		<p> </p>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<img id="Code_Closed_Image_194811" onclick="this.style.display='none'; Code_Closed_Text_194811.style.display='none'; Code_Open_Image_194811.style.display='inline'; Code_Open_Text_194811.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" />
				<img id="Code_Open_Image_194811" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_194811.style.display='none'; Code_Closed_Image_194811.style.display='inline'; Code_Closed_Text_194811.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" />
				<span id="Code_Closed_Text_194811" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">西元列轉換為民國日期列</span>
				<span id="Code_Open_Text_194811" style="DISPLAY: none">
						<br />
						<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<span style="COLOR: #008000">'</span>
						<span style="COLOR: #008000">'****************************************************************************************************************</span>
						<span style="COLOR: #008000">
								<br />'</span>
						<span style="COLOR: #008000">*程式功能  :       將一個西元列轉換為民國日期列</span>
						<span style="COLOR: #008000">
								<br />'</span>
						<span style="COLOR: #008000">*開發人員  :　     ddm 2007/1/13</span>
						<span style="COLOR: #008000">
								<br />'</span>
						<span style="COLOR: #008000">*開發說明  :　     例如 AAABREP.ABACDT列 ,使用 "Select " &amp; XY2MGCol("CStartDate") &amp; " As CStartDate From tbContractRent" 轉換</span>
						<span style="COLOR: #008000">
								<br />'</span>
						<span style="COLOR: #008000">*傳入值    :       strColName      --需要轉換的列名</span>
						<span style="COLOR: #008000">
								<br />'</span>
						<span style="COLOR: #008000">*傳入值    :       strDefaultValue --如果不是一個符合的日期時的值</span>
						<span style="COLOR: #008000">
								<br />'</span>
						<span style="COLOR: #008000">*回傳值    :       String          --轉換後的SQL語句</span>
						<span style="COLOR: #008000">
								<br />'</span>
						<span style="COLOR: #008000">'****************************************************************************************************************</span>
						<span style="COLOR: #008000">
								<br />
						</span>
						<span style="COLOR: #0000ff">Public</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">Function</span>
						<span style="COLOR: #000000"> XY2MGCol(strColName </span>
						<span style="COLOR: #0000ff">As</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">String</span>
						<span style="COLOR: #000000">, </span>
						<span style="COLOR: #0000ff">Optional</span>
						<span style="COLOR: #000000"> strDefaultValue </span>
						<span style="COLOR: #0000ff">As</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">String</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">''</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">) </span>
						<span style="COLOR: #0000ff">As</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">String</span>
						<span style="COLOR: #000000">
								<br />    </span>
						<span style="COLOR: #0000ff">Dim</span>
						<span style="COLOR: #000000"> strSql </span>
						<span style="COLOR: #0000ff">As</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">String</span>
						<span style="COLOR: #000000">
								<br />    strSql </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000"> CASE </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">
								<br />    strSql </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> strSql </span>
						<span style="COLOR: #000000">&amp;</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000"> WHEN LEN(LTRIM(RTRIM(ISNULL(@ColName,''))))=8 THEN</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">
								<br />    strSql </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> strSql </span>
						<span style="COLOR: #000000">&amp;</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000"> CAST((CAST(LEFT(LTRIM(@ColName),4) As int) - 1911) As varchar(4))</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">
								<br />    strSql </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> strSql </span>
						<span style="COLOR: #000000">&amp;</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">+LEFT(RIGHT(LTRIM(RTRIM(@ColName)),4),2)</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">
								<br />    strSql </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> strSql </span>
						<span style="COLOR: #000000">&amp;</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">+RIGHT(LTRIM(RTRIM(@ColName)),2)</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">
								<br />    strSql </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> strSql </span>
						<span style="COLOR: #000000">&amp;</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">ELSE</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">
								<br />    strSql </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> strSql </span>
						<span style="COLOR: #000000">&amp;</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000"> @DefaultValue</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">
								<br />    strSql </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> strSql </span>
						<span style="COLOR: #000000">&amp;</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000"> END </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">
								<br />    strSql </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">Replace</span>
						<span style="COLOR: #000000">(strSql, </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">@ColName</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">, strColName)<br />    XY2MGCol </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">Replace</span>
						<span style="COLOR: #000000">(strSql, </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">@DefaultValue</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">, strDefaultValue)<br /></span>
						<span style="COLOR: #0000ff">End Function</span>
				</span>
		</div>
		<p>
				<br />功能2<br />   在用DB2時數據庫中以數值類型來存放民國日期，要做日期類型運算就要先轉換成日期類型</p>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<img id="Code_Closed_Image_195401" onclick="this.style.display='none'; Code_Closed_Text_195401.style.display='none'; Code_Open_Image_195401.style.display='inline'; Code_Open_Text_195401.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" />
				<img id="Code_Open_Image_195401" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_195401.style.display='none'; Code_Closed_Image_195401.style.display='inline'; Code_Closed_Text_195401.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" />
				<span id="Code_Closed_Text_195401" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">數值型民國日期列轉換為日期列</span>
				<span id="Code_Open_Text_195401" style="DISPLAY: none">
						<br />
						<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<span style="COLOR: #008000">'</span>
						<span style="COLOR: #008000">'****************************************************************************************************************</span>
						<span style="COLOR: #008000">
								<br />'</span>
						<span style="COLOR: #008000">*程式功能  :       將一個數值型民國日期列轉換為日期列</span>
						<span style="COLOR: #008000">
								<br />'</span>
						<span style="COLOR: #008000">*開發人員  :　     ddm 2007/8</span>
						<span style="COLOR: #008000">
								<br />'</span>
						<span style="COLOR: #008000">*開發說明  :　     例如 parseDate("Select * From tb Where @Date(d1) + 20 Days &lt; @Date(d2)")</span>
						<span style="COLOR: #008000">
								<br />'</span>
						<span style="COLOR: #008000">*傳入值    :       strSQL            --需要轉換的SQL語句</span>
						<span style="COLOR: #008000">
								<br />'</span>
						<span style="COLOR: #008000">*回傳值    :       String          --轉換後的SQL語句</span>
						<span style="COLOR: #008000">
								<br />'</span>
						<span style="COLOR: #008000">'****************************************************************************************************************</span>
						<span style="COLOR: #008000">
								<br />
						</span>
						<span style="COLOR: #0000ff">Public</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">Function</span>
						<span style="COLOR: #000000"> parseDate(strSQL)<br />    </span>
						<span style="COLOR: #0000ff">Dim</span>
						<span style="COLOR: #000000"> s </span>
						<span style="COLOR: #0000ff">As</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">String</span>
						<span style="COLOR: #000000">, fieldName </span>
						<span style="COLOR: #0000ff">As</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">String</span>
						<span style="COLOR: #000000">, i </span>
						<span style="COLOR: #0000ff">As</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">Long</span>
						<span style="COLOR: #000000">, j </span>
						<span style="COLOR: #0000ff">As</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">Long</span>
						<span style="COLOR: #000000">, result </span>
						<span style="COLOR: #0000ff">As</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">String</span>
						<span style="COLOR: #000000">
								<br />    result </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> strSQL<br />    s </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">""</span>
						<span style="COLOR: #000000">
								<br />    i </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">InStr</span>
						<span style="COLOR: #000000">(</span>
						<span style="COLOR: #000000">1</span>
						<span style="COLOR: #000000">, result, </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">@Date(</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">)<br />    </span>
						<span style="COLOR: #0000ff">Do</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">While</span>
						<span style="COLOR: #000000"> i </span>
						<span style="COLOR: #000000">&gt;</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">0</span>
						<span style="COLOR: #000000">
								<br />        j </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">InStr</span>
						<span style="COLOR: #000000">(i, result, </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">)</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">)<br />        </span>
						<span style="COLOR: #0000ff">If</span>
						<span style="COLOR: #000000"> i </span>
						<span style="COLOR: #000000">&gt;</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">0</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">And</span>
						<span style="COLOR: #000000"> j </span>
						<span style="COLOR: #000000">&gt;</span>
						<span style="COLOR: #000000"> i </span>
						<span style="COLOR: #0000ff">Then</span>
						<span style="COLOR: #000000">
								<br />            fieldName </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">Mid</span>
						<span style="COLOR: #000000">(result, i </span>
						<span style="COLOR: #000000">+</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">6</span>
						<span style="COLOR: #000000">, j </span>
						<span style="COLOR: #000000">-</span>
						<span style="COLOR: #000000"> i </span>
						<span style="COLOR: #000000">-</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">6</span>
						<span style="COLOR: #000000">)<br />            fieldName </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">Varchar(</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">&amp;</span>
						<span style="COLOR: #000000"> fieldName </span>
						<span style="COLOR: #000000">&amp;</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">+19110000)</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">
								<br />            s </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">Date(Substring(</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">&amp;</span>
						<span style="COLOR: #000000"> fieldName </span>
						<span style="COLOR: #000000">&amp;</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">,1,4)</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">
								<br />            s </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> s </span>
						<span style="COLOR: #000000">&amp;</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000"> || '-' || Substring(</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">&amp;</span>
						<span style="COLOR: #000000"> fieldName </span>
						<span style="COLOR: #000000">&amp;</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">,5,2)</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">
								<br />            s </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> s </span>
						<span style="COLOR: #000000">&amp;</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000"> || '-' || Substring(</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">&amp;</span>
						<span style="COLOR: #000000"> fieldName </span>
						<span style="COLOR: #000000">&amp;</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">,7,2))</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">
								<br />        </span>
						<span style="COLOR: #0000ff">End</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">If</span>
						<span style="COLOR: #000000">
								<br />        fieldName </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">Mid</span>
						<span style="COLOR: #000000">(result, i, j </span>
						<span style="COLOR: #000000">-</span>
						<span style="COLOR: #000000"> i </span>
						<span style="COLOR: #000000">+</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">1</span>
						<span style="COLOR: #000000">)<br />        result </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">Replace</span>
						<span style="COLOR: #000000">(result, fieldName, s)<br />        i </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">InStr</span>
						<span style="COLOR: #000000">(</span>
						<span style="COLOR: #000000">1</span>
						<span style="COLOR: #000000">, result, </span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">@Date(</span>
						<span style="COLOR: #000000">"</span>
						<span style="COLOR: #000000">)<br />    </span>
						<span style="COLOR: #0000ff">Loop</span>
						<span style="COLOR: #000000">
								<br />    parseDate </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> result<br /></span>
						<span style="COLOR: #0000ff">End Function</span>
				</span>
		</div>
		<p>
				<br />
				<br /> </p>
<img src ="http://www.blogjava.net/tw-ddm/aggbug/142418.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/tw-ddm/" target="_blank">大大毛</a> 2007-09-03 19:56 <a href="http://www.blogjava.net/tw-ddm/articles/142418.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>查找不連續數據的多種方法</title><link>http://www.blogjava.net/tw-ddm/articles/134308.html</link><dc:creator>大大毛</dc:creator><author>大大毛</author><pubDate>Mon, 03 Sep 2007 11:31:00 GMT</pubDate><guid>http://www.blogjava.net/tw-ddm/articles/134308.html</guid><wfw:comment>http://www.blogjava.net/tw-ddm/comments/134308.html</wfw:comment><comments>http://www.blogjava.net/tw-ddm/articles/134308.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/tw-ddm/comments/commentRss/134308.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/tw-ddm/services/trackbacks/134308.html</trackback:ping><description><![CDATA[
		<br />
		<font size="2">
				<strong>問題：</strong>
				<br />
				<br />      經常會遇到在業務邏輯上數據連續的而需要找到那些不連續數據的情況。<br />      解決此類問題的方法有很多，可以根據具體的數據結構來加以選擇運用，現就示例說明：<br /><br /></font>
		<strong>
				<font size="2">示例1：<br /></font>
		</strong>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font size="2">
						<img id="Code_Closed_Image_175221" onclick="this.style.display='none'; Code_Closed_Text_175221.style.display='none'; Code_Open_Image_175221.style.display='inline'; Code_Open_Text_175221.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" />
						<img id="Code_Open_Image_175221" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_175221.style.display='none'; Code_Closed_Image_175221.style.display='inline'; Code_Closed_Text_175221.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" />
						<span id="Code_Closed_Text_175221" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">表A結構</span>
				</font>
				<span id="Code_Open_Text_175221" style="DISPLAY: none">
						<br />
						<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<font size="2">
								<span style="COLOR: #0000ff">Create</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">Table</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000"> tb_ExampleA (<br />  kID     </span>
								<span style="FONT-WEIGHT: bold; COLOR: #000000">Char</span>
								<span style="COLOR: #000000">(</span>
								<span style="FONT-WEIGHT: bold; COLOR: #800000">3</span>
								<span style="COLOR: #000000">),          </span>
								<span style="COLOR: #008080">--</span>
								<span style="COLOR: #008080">主關鍵字</span>
						</font>
						<span style="COLOR: #008080">
								<br />
						</span>
						<font size="2">
								<span style="COLOR: #000000">  iNo     </span>
								<span style="FONT-WEIGHT: bold; COLOR: #000000">Int</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #808080">Not</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">Null</span>
								<span style="COLOR: #000000">,     </span>
								<span style="COLOR: #008080">--</span>
								<span style="COLOR: #008080">序號</span>
						</font>
						<span style="COLOR: #008080">
								<br />
						</span>
						<font size="2">
								<span style="COLOR: #000000">  cContent </span>
								<span style="FONT-WEIGHT: bold; COLOR: #000000">varchar</span>
								<span style="COLOR: #000000">(</span>
								<span style="FONT-WEIGHT: bold; COLOR: #800000">100</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000">),<br />  </span>
								<span style="COLOR: #0000ff">Primary</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">Key</span>
						</font>
						<span style="COLOR: #000000">
								<font size="2"> (kID,iNo)<br />);</font>
						</span>
				</span>
		</div>
		<br />
		<font size="2">   表結構說明<br />      kID      主關鍵字，例如合同號<br />      iNo      序號(&gt;=1)，從業務邏輯上來說對於同一筆合同號該列的值應該保持連續遞增<br />   要求<br />      找到那些有問題的 kID <br /><br />解決方法：<br />      表A屬於是一種比較單純、簡單的結構，對於這類只要直接在表上做一次處理即可。<br />      1. 方法A -- 自連接<br />         利用對自身表的 iNo 進行錯值(+1或-1)外連, 找到無法連接的數據。<br /></font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font size="2">
						<img id="Code_Closed_Image_181456" onclick="this.style.display='none'; Code_Closed_Text_181456.style.display='none'; Code_Open_Image_181456.style.display='inline'; Code_Open_Text_181456.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" />
						<img id="Code_Open_Image_181456" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_181456.style.display='none'; Code_Closed_Image_181456.style.display='inline'; Code_Closed_Text_181456.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" />
						<span id="Code_Closed_Text_181456" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">自連接</span>
				</font>
				<span id="Code_Open_Text_181456" style="DISPLAY: none">
						<br />
						<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<span style="COLOR: #0000ff">
								<font size="2">Select</font>
						</span>
						<span style="COLOR: #000000">
								<br />
								<font size="2">    </font>
						</span>
						<span style="COLOR: #0000ff">
								<font size="2">Distinct</font>
						</span>
						<font size="2">
								<span style="COLOR: #000000"> t1.kID<br />  </span>
								<span style="COLOR: #0000ff">From</span>
						</font>
						<span style="COLOR: #000000">
								<br />
								<font size="2">    tb_ExampleA t1<br />    </font>
						</span>
						<font size="2">
								<span style="COLOR: #ff00ff">Left</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">Join</span>
								<span style="COLOR: #000000"> tb_ExampleA t2 </span>
								<span style="COLOR: #0000ff">On</span>
								<span style="COLOR: #000000"> t1.kID </span>
								<span style="COLOR: #808080">=</span>
								<span style="COLOR: #000000"> t2.kID </span>
								<span style="COLOR: #808080">And</span>
								<span style="COLOR: #000000"> t1.iNo </span>
								<span style="COLOR: #808080">=</span>
								<span style="COLOR: #000000"> t2.iNo </span>
								<span style="COLOR: #808080">+</span>
								<span style="COLOR: #000000"> </span>
								<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
						</font>
						<span style="COLOR: #000000">
								<br />
								<font size="2">  </font>
						</span>
						<span style="COLOR: #0000ff">
								<font size="2">Where</font>
						</span>
						<span style="COLOR: #000000">
								<br />
								<font size="2">    t1.iNo </font>
						</span>
						<font size="2">
								<span style="COLOR: #808080">&gt;</span>
								<span style="COLOR: #000000"> </span>
								<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
						</font>
						<span style="COLOR: #000000">
								<br />
								<font size="2">    </font>
						</span>
						<font size="2">
								<span style="COLOR: #808080">And</span>
								<span style="COLOR: #000000"> t2.iNo </span>
								<span style="COLOR: #0000ff">Is</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">Null</span>
						</font>
				</span>
		</div>
		<br />
		<font size="2">      這樣就可以定位到那些有問題的 kID 上。<br />      這種解決方法是最通用的方法，不過在編寫上比較複雜。<br /><br />      2. 方法B -- 利用Count(*)<br /></font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font size="2">
						<img id="Code_Closed_Image_181947" onclick="this.style.display='none'; Code_Closed_Text_181947.style.display='none'; Code_Open_Image_181947.style.display='inline'; Code_Open_Text_181947.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" />
						<img id="Code_Open_Image_181947" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_181947.style.display='none'; Code_Closed_Image_181947.style.display='inline'; Code_Closed_Text_181947.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" />
						<span id="Code_Closed_Text_181947" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">利用Count(*)</span>
				</font>
				<span id="Code_Open_Text_181947" style="DISPLAY: none">
						<br />
						<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<span style="COLOR: #0000ff">
								<font size="2">Select</font>
						</span>
						<span style="COLOR: #000000">
								<br />
								<font size="2">    kID<br />  </font>
						</span>
						<span style="COLOR: #0000ff">
								<font size="2">From</font>
						</span>
						<span style="COLOR: #000000">
								<br />
								<font size="2">    tb_ExampleA<br />  </font>
						</span>
						<font size="2">
								<span style="COLOR: #0000ff">Group</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">By</span>
						</font>
						<span style="COLOR: #000000">
								<br />
								<font size="2">    kID<br />  </font>
						</span>
						<span style="COLOR: #0000ff">
								<font size="2">Having</font>
						</span>
						<span style="COLOR: #000000">
								<br />
								<font size="2">    </font>
						</span>
						<font size="2">
								<span style="COLOR: #ff00ff">Count</span>
								<span style="COLOR: #000000">(</span>
								<span style="COLOR: #808080">*</span>
								<span style="COLOR: #000000">) </span>
								<span style="COLOR: #808080">&lt;&gt;</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #ff00ff">Max</span>
								<span style="COLOR: #000000">(iNo)</span>
						</font>
				</span>
		</div>
		<br />
		<font size="2">      看看這個實現就可以發現，這裏比較巧妙的利用了 iNo 列上的邏輯規則 (int型的連續遞增)，如果 iNo 不從1開始計數則在Having 子句中加一點點運算即可。<br />      這個解決方法比較巧妙，執行效率較高。<br /><br />      3. 方法C -- 利用函數<br /></font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font size="2">
						<img id="Code_Closed_Image_183104" onclick="this.style.display='none'; Code_Closed_Text_183104.style.display='none'; Code_Open_Image_183104.style.display='inline'; Code_Open_Text_183104.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" />
						<img id="Code_Open_Image_183104" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_183104.style.display='none'; Code_Closed_Image_183104.style.display='inline'; Code_Closed_Text_183104.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" />
						<span id="Code_Closed_Text_183104" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">利用公式運算</span>
				</font>
				<span id="Code_Open_Text_183104" style="DISPLAY: none">
						<br />
						<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<span style="COLOR: #0000ff">
								<font size="2">Select</font>
						</span>
						<span style="COLOR: #000000">
								<br />
								<font size="2">    kID<br />  </font>
						</span>
						<span style="COLOR: #0000ff">
								<font size="2">From</font>
						</span>
						<span style="COLOR: #000000">
								<br />
								<font size="2">    tb_ExampleA<br />  </font>
						</span>
						<font size="2">
								<span style="COLOR: #0000ff">Group</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">By</span>
						</font>
						<span style="COLOR: #000000">
								<br />
								<font size="2">    kID<br />  </font>
						</span>
						<span style="COLOR: #0000ff">
								<font size="2">Having</font>
						</span>
						<span style="COLOR: #000000">
								<br />
								<font size="2">    </font>
						</span>
						<font size="2">
								<span style="COLOR: #ff00ff">Sum</span>
								<span style="COLOR: #000000">(iNo) </span>
								<span style="COLOR: #808080">&lt;&gt;</span>
								<span style="COLOR: #000000"> (</span>
								<span style="COLOR: #ff00ff">Max</span>
								<span style="COLOR: #000000">(iNo) </span>
								<span style="COLOR: #808080">*</span>
								<span style="COLOR: #000000"> (</span>
								<span style="COLOR: #ff00ff">Max</span>
								<span style="COLOR: #000000">(iNo) </span>
								<span style="COLOR: #808080">+</span>
								<span style="COLOR: #000000"> </span>
								<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
								<span style="COLOR: #000000">)) </span>
								<span style="COLOR: #808080">/</span>
								<span style="COLOR: #000000"> </span>
								<span style="FONT-WEIGHT: bold; COLOR: #800000">2</span>
						</font>
				</span>
		</div>
		<br />
		<font size="2">      方法C 與方法B 的思路實際上是一致的，就是利用 Group ，在 iNo 列上直接實施檢查，方法C 功能更加強大一些，因為如果 iNo 的規則有變更的話(如奇數遞增, :P )，改改這裏用的 n(n+1) / 2 的公式就好。<br /><br /><br /></font>
		<font size="2">
				<strong>示例2：<br /></strong>      這裏是另外一種情況，相比示例1 來說更加複雜一些，它的 iNo 列由多列組成，示例如下。<br /></font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font size="2">
						<img id="Code_Closed_Image_184726" onclick="this.style.display='none'; Code_Closed_Text_184726.style.display='none'; Code_Open_Image_184726.style.display='inline'; Code_Open_Text_184726.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" />
						<img id="Code_Open_Image_184726" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_184726.style.display='none'; Code_Closed_Image_184726.style.display='inline'; Code_Closed_Text_184726.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" />
						<span id="Code_Closed_Text_184726" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">表B結構</span>
				</font>
				<span id="Code_Open_Text_184726" style="DISPLAY: none">
						<br />
						<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<font size="2">
								<span style="COLOR: #0000ff">Create</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">Table</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000"> tb_ExampleB (<br />  kID       </span>
								<span style="FONT-WEIGHT: bold; COLOR: #000000">Char</span>
								<span style="COLOR: #000000">(</span>
								<span style="FONT-WEIGHT: bold; COLOR: #800000">3</span>
								<span style="COLOR: #000000">),        </span>
								<span style="COLOR: #008080">--</span>
								<span style="COLOR: #008080">主關鍵字</span>
						</font>
						<span style="COLOR: #008080">
								<br />
						</span>
						<font size="2">
								<span style="COLOR: #000000">  iYear     </span>
								<span style="FONT-WEIGHT: bold; COLOR: #000000">Int</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #808080">Not</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">Null</span>
								<span style="COLOR: #000000">,   </span>
								<span style="COLOR: #008080">--</span>
								<span style="COLOR: #008080">序1</span>
						</font>
						<span style="COLOR: #008080">
								<br />
						</span>
						<font size="2">
								<span style="COLOR: #000000">  iMonth    </span>
								<span style="FONT-WEIGHT: bold; COLOR: #000000">Int</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #808080">Not</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">Null</span>
								<span style="COLOR: #000000">,   </span>
								<span style="COLOR: #008080">--</span>
								<span style="COLOR: #008080">序2</span>
						</font>
						<span style="COLOR: #008080">
								<br />
						</span>
						<font size="2">
								<span style="COLOR: #000000">  cContent  </span>
								<span style="FONT-WEIGHT: bold; COLOR: #000000">Varchar</span>
								<span style="COLOR: #000000">(</span>
								<span style="FONT-WEIGHT: bold; COLOR: #800000">100</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000">),<br />  </span>
								<span style="COLOR: #0000ff">Primary</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">Key</span>
						</font>
						<span style="COLOR: #000000">
								<font size="2"> (kID,iYear,iMonth)<br />);</font>
						</span>
				</span>
		</div>
		<br />
		<font size="2">      表結構說明<br />         kID            主關鍵字，例如合同號<br />         iYear         第幾年度( &gt;= 1)，連續遞增<br />         iMonth     第幾月份[1, 12]，連續遞增<br />      要求<br />         找到那些年度+月份不連續的 kID<br /><br />解決方法：<br />      表B 的結構與表A 相比在需要檢查的列上變成多列的結構，而且多列的內部還有著一些隱含關係(如這裏示例中的年/月，顯然不可能第1年度未滿12月就跳到第2年度)，不過就實質來看與表A 的結構一致 ( 可以將iYear , iMonth 看成一列 )，因此同樣可以適用示例1中的方法，只要多加合併 iYear, iMonth的邏輯就好。<br />      這裏對iYear, iMonth列的合併，使用自定義列 + 表封裝 的方式來實現，下面就示例上面的Count方式。 <br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img id="Code_Closed_Image_192231" onclick="this.style.display='none'; Code_Closed_Text_192231.style.display='none'; Code_Open_Image_192231.style.display='inline'; Code_Open_Text_192231.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" /><img id="Code_Open_Image_192231" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_192231.style.display='none'; Code_Closed_Image_192231.style.display='inline'; Code_Closed_Text_192231.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" /><span id="Code_Closed_Text_192231" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">利用Count(*)</span><span id="Code_Open_Text_192231" style="DISPLAY: none"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: #0000ff">Select</span><span style="COLOR: #000000"><br />    kID<br />  </span><span style="COLOR: #0000ff">From</span><span style="COLOR: #000000"><br />    (</span><span style="COLOR: #0000ff">Select</span><span style="COLOR: #000000"> ((iYear</span><span style="COLOR: #808080">-</span><span style="FONT-WEIGHT: bold; COLOR: #800000">1</span><span style="COLOR: #000000">)</span><span style="COLOR: #808080">*</span><span style="FONT-WEIGHT: bold; COLOR: #800000">12</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">+</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff00ff">Month</span><span style="COLOR: #000000">) iNo,tb_ExampleB.</span><span style="COLOR: #808080">*</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">From</span><span style="COLOR: #000000"> tb_ExampleB) tb_ExampleB<br />  </span><span style="COLOR: #0000ff">Group</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">By</span><span style="COLOR: #000000"><br />    kID<br />  </span><span style="COLOR: #0000ff">Having</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #ff00ff">Count</span><span style="COLOR: #000000">(</span><span style="COLOR: #808080">*</span><span style="COLOR: #000000">) </span><span style="COLOR: #808080">&lt;&gt;</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff00ff">Max</span><span style="COLOR: #000000">(iNo)</span></span></div><br /></font>
<img src ="http://www.blogjava.net/tw-ddm/aggbug/134308.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/tw-ddm/" target="_blank">大大毛</a> 2007-09-03 19:31 <a href="http://www.blogjava.net/tw-ddm/articles/134308.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>根據時間間隔抽樣數據</title><link>http://www.blogjava.net/tw-ddm/articles/115308.html</link><dc:creator>大大毛</dc:creator><author>大大毛</author><pubDate>Fri, 04 May 2007 06:08:00 GMT</pubDate><guid>http://www.blogjava.net/tw-ddm/articles/115308.html</guid><wfw:comment>http://www.blogjava.net/tw-ddm/comments/115308.html</wfw:comment><comments>http://www.blogjava.net/tw-ddm/articles/115308.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/tw-ddm/comments/commentRss/115308.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/tw-ddm/services/trackbacks/115308.html</trackback:ping><description><![CDATA[
		<br />
		<font size="2">
				<font face="Georgia">
						<strong>問題：</strong>
						<br />
						<br />   按時間順序來存放的資料量很大，此時通常需要根據一個時間間隔來抽樣數據，例如說有一張表存放的是設備的運行狀態，現在需要根據輸入的時間間隔（比如5分鍾）來進行數據抽樣。<br /></font>
		</font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font size="2">
						<font face="Georgia">
								<img id="Code_Closed_Image_121542" onclick="this.style.display='none'; Code_Closed_Text_121542.style.display='none'; Code_Open_Image_121542.style.display='inline'; Code_Open_Text_121542.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" />
								<img id="Code_Open_Image_121542" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_121542.style.display='none'; Code_Closed_Image_121542.style.display='inline'; Code_Closed_Text_121542.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" />
								<span id="Code_Closed_Text_121542" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">表結構及測試數據</span>
						</font>
				</font>
				<span id="Code_Open_Text_121542" style="DISPLAY: none">
						<br />
						<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #0000ff">Create</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Table</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000"> tbRunStatus (<br />    rID </span>
										<span style="FONT-WEIGHT: bold; COLOR: #000000">int</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #ff00ff">identity</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">primary</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">key</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000">,<br />    snapshotTime </span>
										<span style="FONT-WEIGHT: bold; COLOR: #000000">datetime</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000">,<br />    thermometry </span>
										<span style="FONT-WEIGHT: bold; COLOR: #000000">int</span>
										<span style="COLOR: #000000">,    </span>
										<span style="COLOR: #008080">--</span>
										<span style="COLOR: #008080">溫度</span>
								</font>
						</font>
						<span style="COLOR: #008080">
								<br />
						</span>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000">    hygrometry </span>
										<span style="FONT-WEIGHT: bold; COLOR: #000000">int</span>
										<span style="COLOR: #000000">    </span>
										<span style="COLOR: #008080">--</span>
										<span style="COLOR: #008080">濕度</span>
								</font>
						</font>
						<span style="COLOR: #008080">
								<br />
						</span>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000">);<br /></span>
										<span style="COLOR: #0000ff">insert</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">into</span>
										<span style="COLOR: #000000"> tbrunstatus </span>
										<span style="COLOR: #0000ff">values</span>
										<span style="COLOR: #000000"> (</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #ff0000">2007-05-04 12:05:12.077</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">30</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">85</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000">);<br /></span>
										<span style="COLOR: #0000ff">insert</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">into</span>
										<span style="COLOR: #000000"> tbrunstatus </span>
										<span style="COLOR: #0000ff">values</span>
										<span style="COLOR: #000000"> (</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #ff0000">2007-05-04 12:06:38.340</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">30</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">85</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000">);<br /></span>
										<span style="COLOR: #0000ff">insert</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">into</span>
										<span style="COLOR: #000000"> tbrunstatus </span>
										<span style="COLOR: #0000ff">values</span>
										<span style="COLOR: #000000"> (</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #ff0000">2007-05-04 12:10:39.750</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">30</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">85</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000">);<br /></span>
										<span style="COLOR: #0000ff">insert</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">into</span>
										<span style="COLOR: #000000"> tbrunstatus </span>
										<span style="COLOR: #0000ff">values</span>
										<span style="COLOR: #000000"> (</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #ff0000">2007-05-05 01:05:38.340</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">31</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">87</span>
										<span style="COLOR: #000000">);</span>
								</font>
						</font>
				</span>
		</div>
		<font face="Georgia" size="2">   這個表只做了幾個數據，實際情況下數據條數會很多，而且時間上是連續的(比如說每秒都至少會有一條資料)。<br />   當然了，這類問題為了實現抽樣，就一定會有幾個關鍵數據需要提供：<br />   1. 開始抽樣的時間，因為所謂間隔必須具有一個開始點；<br />   2. 抽樣的時間段；<br />   3. 抽樣的間隔；<br /><br /><br /><strong>解決：</strong><br />   先來說一下思路，解決這個問題的關鍵在於：<br />   1. 先得到一個"時間上的間隔"；<br />   這個間隔是相對於上面提到的第1個關鍵點即開始時間(比如說開始時間為'2007-05-04 12:00:00' )，因為是時間上的計算，可以利用MS-SQL的 Datediff 時間函數來實現。<br />   2. 在第1點所得到的"間隔"上進行篩選。<br /><br />   現在就利用嵌套SQL來分步實現(按分鍾間隔)：<br />   <strong>1. 得到"間隔"計算列，並應用抽樣的時間範圍(提高效率)</strong><br /></font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<span style="COLOR: #0000ff">
						<font face="Georgia" size="2">Select</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">        </font>
				</span>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #808080">*</span>
								<span style="COLOR: #000000">, </span>
								<span style="COLOR: #ff00ff">datediff</span>
								<span style="COLOR: #000000">(mi,</span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #ff0000">2007-05-04 12:00:00</span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #000000">,snapshotTime) </span>
								<span style="COLOR: #0000ff">As</span>
						</font>
				</font>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #000000"> interval<br />    </span>
								<span style="COLOR: #0000ff">From</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">        tbRunStatus<br />    </font>
				</span>
				<span style="COLOR: #0000ff">
						<font face="Georgia" size="2">Where</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">        snapshotTime </font>
				</span>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #808080">Between</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #ff0000">2007-05-04 00:00:00</span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #808080">And</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #ff0000">2007-05-05 23:59:59</span>
								<span style="COLOR: #ff0000">'</span>
						</font>
				</font>
		</div>
		<br />
		<font face="Georgia" size="2">   <strong>2.0 在第一步的結果集上做篩選</strong><br /></font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<span style="COLOR: #0000ff">
						<font face="Georgia" size="2">Select</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">        </font>
				</span>
				<span style="COLOR: #808080">
						<font face="Georgia" size="2">*</font>
				</span>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #000000"> <br />    </span>
								<span style="COLOR: #0000ff">From</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">        (</font>
				</span>
				<span style="COLOR: #0000ff">
						<font face="Georgia" size="2">Select</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">                </font>
				</span>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #808080">*</span>
								<span style="COLOR: #000000">, </span>
								<span style="COLOR: #ff00ff">datediff</span>
								<span style="COLOR: #000000">(mi,</span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #ff0000">2007-05-04 12:00:00</span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #000000">,snapshotTime) </span>
								<span style="COLOR: #0000ff">As</span>
						</font>
				</font>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #000000"> interval<br />            </span>
								<span style="COLOR: #0000ff">From</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">                tbRunStatus<br />            </font>
				</span>
				<span style="COLOR: #0000ff">
						<font face="Georgia" size="2">Where</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">                snapshotTime </font>
				</span>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #808080">Between</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #ff0000">2007-05-04 00:00:00</span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #808080">And</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #ff0000">2007-05-05 23:59:59</span>
								<span style="COLOR: #ff0000">'</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">        ) t<br />    </font>
				</span>
				<span style="COLOR: #0000ff">
						<font face="Georgia" size="2">Where</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">        interval </font>
				</span>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #808080">%</span>
								<span style="COLOR: #000000"> </span>
								<span style="FONT-WEIGHT: bold; COLOR: #800000">5</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #808080">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="FONT-WEIGHT: bold; COLOR: #800000">0</span>
						</font>
				</font>
		</div>
		<br />
		<font face="Georgia" size="2">      需要注意的一點就是條件中所應用的 interval % 5 = 0 ，意圖是每隔5分鍾抽一條出來，但是如果是在一分鍾內會有多條而我們只需要最近的那一條數據該怎麽辦呢(如果上面的數據按天來做間隔的話，那麽前3條的間隔都會是0)？<br /><br />      <strong>2.1 這樣一來就需要用 Group By 分組來解決(按天做為間隔)</strong><br /></font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #0000ff">Select</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #808080">*</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">From</span>
								<span style="COLOR: #000000"> tbRunStatus </span>
								<span style="COLOR: #0000ff">Join</span>
						</font>
				</font>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #000000"> (<br /></span>
								<span style="COLOR: #0000ff">Select</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">        </font>
				</span>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #ff00ff">Min</span>
								<span style="COLOR: #000000">(rID) </span>
								<span style="COLOR: #0000ff">As</span>
								<span style="COLOR: #000000"> rID    </span>
								<span style="COLOR: #008080">--</span>
								<span style="COLOR: #008080">由於應用了分組,這裏只要取用關鍵列就好</span>
						</font>
				</font>
				<span style="COLOR: #008080">
						<br />
				</span>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #000000">    </span>
								<span style="COLOR: #0000ff">From</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">        (</font>
				</span>
				<span style="COLOR: #0000ff">
						<font face="Georgia" size="2">Select</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">                </font>
				</span>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #808080">*</span>
								<span style="COLOR: #000000">, </span>
								<span style="COLOR: #ff00ff">datediff</span>
								<span style="COLOR: #000000">(</span>
								<span style="COLOR: #ff00ff">day</span>
								<span style="COLOR: #000000">,</span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #ff0000">2007-05-04 12:00:00</span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #000000">,snapshotTime) </span>
								<span style="COLOR: #0000ff">As</span>
						</font>
				</font>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #000000"> interval<br />            </span>
								<span style="COLOR: #0000ff">From</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">                tbRunStatus<br />            </font>
				</span>
				<span style="COLOR: #0000ff">
						<font face="Georgia" size="2">Where</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">                snapshotTime </font>
				</span>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #808080">Between</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #ff0000">2007-05-04 00:00:00</span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #808080">And</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #ff0000">2007-05-05 23:59:59</span>
								<span style="COLOR: #ff0000">'</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">        ) t<br />    </font>
				</span>
				<span style="COLOR: #0000ff">
						<font face="Georgia" size="2">Where</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">        interval </font>
				</span>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #808080">%</span>
								<span style="COLOR: #000000"> </span>
								<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #808080">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="FONT-WEIGHT: bold; COLOR: #800000">0</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">    </font>
				</span>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #0000ff">Group</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">By</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">        interval<br />) tbRunStatus1 </font>
				</span>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #0000ff">On</span>
								<span style="COLOR: #000000"> tbRunStatus.rID </span>
								<span style="COLOR: #808080">=</span>
								<span style="COLOR: #000000"> tbRunStatus1.rID</span>
						</font>
				</font>
		</div>
		<font face="Georgia" size="2">      可以看到，實際上的處理也並不複雜，只是將結果集與表做了一次連接就成了，最後的抽樣結果會是兩條。<br /><br /><br />      更深入的思考一下，如果間隔比較特殊造成並不是卡的每個時刻都會有數據，例如間隔卡在4分鍾，那麽就會存在著近似的存在，看看下面SQL以及結果就明白了：<br /></font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<span style="COLOR: #0000ff">
						<font face="Georgia" size="2">Select</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">        </font>
				</span>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #808080">*</span>
								<span style="COLOR: #000000">, interval </span>
								<span style="COLOR: #808080">/</span>
								<span style="COLOR: #000000"> </span>
								<span style="FONT-WEIGHT: bold; COLOR: #800000">4.0</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">As</span>
						</font>
				</font>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #000000"> d<br />    </span>
								<span style="COLOR: #0000ff">From</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">        (</font>
				</span>
				<span style="COLOR: #0000ff">
						<font face="Georgia" size="2">Select</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">                </font>
				</span>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #808080">*</span>
								<span style="COLOR: #000000">, </span>
								<span style="COLOR: #ff00ff">datediff</span>
								<span style="COLOR: #000000">(mi,</span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #ff0000">2007-05-04 12:00:00</span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #000000">,snapshotTime) </span>
								<span style="COLOR: #0000ff">As</span>
						</font>
				</font>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #000000"> interval<br />            </span>
								<span style="COLOR: #0000ff">From</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">                tbRunStatus<br />            </font>
				</span>
				<span style="COLOR: #0000ff">
						<font face="Georgia" size="2">Where</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">                snapshotTime </font>
				</span>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #808080">Between</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #ff0000">2007-05-04 00:00:00</span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #808080">And</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #ff0000">2007-05-05 23:59:59</span>
								<span style="COLOR: #ff0000">'</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">        ) t</font>
				</span>
		</div>
		<font face="Georgia" size="2">      結果集會是這樣：<br /></font>
		<table width="500" border="1">
				<tbody>
						<tr>
								<th>
										<font face="Georgia" size="2">rID</font>
								</th>
								<th>
										<font face="Georgia" size="2">snapshotTime</font>
								</th>
								<th>
										<font face="Georgia" size="2">thermometry</font>
								</th>
								<th>
										<font face="Georgia" size="2">hygrometry</font>
								</th>
								<th>
										<font face="Georgia" size="2">interval</font>
								</th>
								<th>
										<font face="Georgia" size="2">d</font>
								</th>
						</tr>
						<tr>
								<td>
										<font face="Georgia" size="2">10</font>
								</td>
								<td>
										<font face="Georgia" size="2">2007-05-04 12:05:12.077</font>
								</td>
								<td>
										<font face="Georgia" size="2">30</font>
								</td>
								<td>
										<font face="Georgia" size="2">85</font>
								</td>
								<td>
										<font face="Georgia" size="2">5</font>
								</td>
								<td>
										<font face="Georgia" size="2">1.250000</font>
								</td>
						</tr>
						<tr>
								<td>
										<font face="Georgia" size="2">11</font>
								</td>
								<td>
										<font face="Georgia" size="2">2007-05-04 12:06:38.340</font>
								</td>
								<td>
										<font face="Georgia" size="2">30</font>
								</td>
								<td>
										<font face="Georgia" size="2">85</font>
								</td>
								<td>
										<font face="Georgia" size="2">6</font>
								</td>
								<td>
										<font face="Georgia" size="2">1.500000</font>
								</td>
						</tr>
						<tr>
								<td>
										<font face="Georgia" size="2">12</font>
								</td>
								<td>
										<font face="Georgia" size="2">2007-05-04 12:10:39.750</font>
								</td>
								<td>
										<font face="Georgia" size="2">30</font>
								</td>
								<td>
										<font face="Georgia" size="2">85</font>
								</td>
								<td>
										<font face="Georgia" size="2">10</font>
								</td>
								<td>
										<font face="Georgia" size="2">2.500000</font>
								</td>
						</tr>
						<tr>
								<td>
										<font face="Georgia" size="2">13</font>
								</td>
								<td>
										<font face="Georgia" size="2">2007-05-05 01:05:38.340</font>
								</td>
								<td>
										<font face="Georgia" size="2">31</font>
								</td>
								<td>
										<font face="Georgia" size="2">87</font>
								</td>
								<td>
										<font face="Georgia" size="2">785</font>
								</td>
								<td>
										<font face="Georgia" size="2">196.250000</font>
								</td>
						</tr>
				</tbody>
		</table>
		<font face="Georgia" size="2">      可以通過這裏的示範列"d"的數值看出，如果是按4分鍾做為間隔的話，那麽 [4-8) 分鍾內有2條資料,而[8-12)分鍾內有1條，而按上面的方法則是一條資料也拿不到的。現在需要在每4分鍾的間隔內取一條(如果有的話)，即應該取rID = (10,12,13)這3條才對。<br /><br />      <strong>2.2 對於這種近似匹配區間的情況，我們只需要改改就好</strong><br /></font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #0000ff">Select</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #808080">*</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">From</span>
								<span style="COLOR: #000000"> tbRunStatus </span>
								<span style="COLOR: #0000ff">Join</span>
						</font>
				</font>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #000000"> (<br /></span>
								<span style="COLOR: #0000ff">Select</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">        </font>
				</span>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #ff00ff">Min</span>
								<span style="COLOR: #000000">(rID) </span>
								<span style="COLOR: #0000ff">As</span>
						</font>
				</font>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #000000"> rID<br />    </span>
								<span style="COLOR: #0000ff">From</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">        (</font>
				</span>
				<span style="COLOR: #0000ff">
						<font face="Georgia" size="2">Select</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">                </font>
				</span>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #808080">*</span>
								<span style="COLOR: #000000">, </span>
								<span style="COLOR: #ff00ff">datediff</span>
								<span style="COLOR: #000000">(mi,</span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #ff0000">2007-05-04 12:00:00</span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #000000">,snapshotTime) </span>
								<strong>
										<span style="COLOR: #808080">/</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">4</span>
								</strong>
								<span style="COLOR: #000000">  </span>
								<span style="COLOR: #0000ff">As</span>
						</font>
				</font>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #000000"> interval<br />            </span>
								<span style="COLOR: #0000ff">From</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">                tbRunStatus<br />            </font>
				</span>
				<span style="COLOR: #0000ff">
						<font face="Georgia" size="2">Where</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">                snapshotTime </font>
				</span>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #808080">Between</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #ff0000">2007-05-04 00:00:00</span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #808080">And</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #ff0000">2007-05-05 23:59:59</span>
								<span style="COLOR: #ff0000">'</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">        ) t<br />    </font>
				</span>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #0000ff">Group</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">By</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">        interval<br />) tbRunStatus1 </font>
				</span>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #0000ff">On</span>
								<span style="COLOR: #000000"> tbRunStatus.rID </span>
								<span style="COLOR: #808080">=</span>
								<span style="COLOR: #000000"> tbRunStatus1.rID</span>
						</font>
				</font>
		</div>
		<font face="Georgia" size="2">      呵呵，改改那個計算列的計算邏輯就能達到目的。<br /></font>
<img src ="http://www.blogjava.net/tw-ddm/aggbug/115308.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/tw-ddm/" target="_blank">大大毛</a> 2007-05-04 14:08 <a href="http://www.blogjava.net/tw-ddm/articles/115308.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>分类小计的实现</title><link>http://www.blogjava.net/tw-ddm/articles/115292.html</link><dc:creator>大大毛</dc:creator><author>大大毛</author><pubDate>Fri, 04 May 2007 03:52:00 GMT</pubDate><guid>http://www.blogjava.net/tw-ddm/articles/115292.html</guid><wfw:comment>http://www.blogjava.net/tw-ddm/comments/115292.html</wfw:comment><comments>http://www.blogjava.net/tw-ddm/articles/115292.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/tw-ddm/comments/commentRss/115292.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/tw-ddm/services/trackbacks/115292.html</trackback:ping><description><![CDATA[
		<br />
		<font size="2">
				<font face="Georgia">
						<strong>問題：</strong>
						<br />
						<br />      分類小計的情況很多，通常情況下我們可能會在SQL的外部來實現分類小計，但是相比直接使用SQL得到滿意的結果集來說效率上就會差太多，現在就來解決它好了。先看表結構：<br />  </font>
		</font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font size="2">
						<font face="Georgia">
								<img id="Code_Closed_Image_100121" onclick="this.style.display='none'; Code_Closed_Text_100121.style.display='none'; Code_Open_Image_100121.style.display='inline'; Code_Open_Text_100121.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" />
								<img id="Code_Open_Image_100121" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_100121.style.display='none'; Code_Closed_Image_100121.style.display='inline'; Code_Closed_Text_100121.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" />
								<span id="Code_Closed_Text_100121" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">表結構及示例數據</span>
						</font>
				</font>
				<span id="Code_Open_Text_100121" style="DISPLAY: none">
						<br />
						<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #008080">--</span>
										<span style="COLOR: #008080">類別</span>
								</font>
						</font>
						<span style="COLOR: #008080">
								<br />
						</span>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #0000ff">Create</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Table</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000"> tbCategory (<br />    cID </span>
										<span style="FONT-WEIGHT: bold; COLOR: #000000">int</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">primary</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">key</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000">,<br />    cName </span>
										<span style="FONT-WEIGHT: bold; COLOR: #000000">varchar</span>
										<span style="COLOR: #000000">(</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">50</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000">)<br />);<br /></span>
										<span style="COLOR: #008080">--</span>
										<span style="COLOR: #008080">出版物的類型</span>
								</font>
						</font>
						<span style="COLOR: #008080">
								<br />
						</span>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #0000ff">Create</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Table</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000"> tbType (<br />    tID </span>
										<span style="FONT-WEIGHT: bold; COLOR: #000000">int</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">primary</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">key</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000">,<br />    tName </span>
										<span style="FONT-WEIGHT: bold; COLOR: #000000">varchar</span>
										<span style="COLOR: #000000">(</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">50</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000">)<br />);<br /></span>
										<span style="COLOR: #008080">--</span>
										<span style="COLOR: #008080">書</span>
								</font>
						</font>
						<span style="COLOR: #008080">
								<br />
						</span>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #0000ff">Create</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Table</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000"> tbBook (<br />    bID </span>
										<span style="FONT-WEIGHT: bold; COLOR: #000000">int</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">primary</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">key</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000">,<br />    cID </span>
										<span style="FONT-WEIGHT: bold; COLOR: #000000">int</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000">,<br />    tID </span>
										<span style="FONT-WEIGHT: bold; COLOR: #000000">int</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000">,<br />    bName </span>
										<span style="FONT-WEIGHT: bold; COLOR: #000000">varchar</span>
										<span style="COLOR: #000000">(</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">50</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000">),<br />    price </span>
										<span style="FONT-WEIGHT: bold; COLOR: #000000">money</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<br />
								<font face="Georgia" size="2">);<br /></font>
						</span>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #0000ff">Insert</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Into</span>
										<span style="COLOR: #000000"> tbCategory </span>
										<span style="COLOR: #0000ff">Values</span>
										<span style="COLOR: #000000"> (</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
										<span style="COLOR: #000000">,</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #ff0000">生物</span>
										<span style="COLOR: #ff0000">'</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000">);<br /></span>
										<span style="COLOR: #0000ff">Insert</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Into</span>
										<span style="COLOR: #000000"> tbCategory </span>
										<span style="COLOR: #0000ff">Values</span>
										<span style="COLOR: #000000"> (</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">2</span>
										<span style="COLOR: #000000">,</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #ff0000">地理</span>
										<span style="COLOR: #ff0000">'</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000">);<br /></span>
										<span style="COLOR: #0000ff">Insert</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Into</span>
										<span style="COLOR: #000000"> tbCategory </span>
										<span style="COLOR: #0000ff">Values</span>
										<span style="COLOR: #000000"> (</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">3</span>
										<span style="COLOR: #000000">,</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #ff0000">計算機</span>
										<span style="COLOR: #ff0000">'</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000">);<br /></span>
										<span style="COLOR: #0000ff">Insert</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Into</span>
										<span style="COLOR: #000000"> tbType </span>
										<span style="COLOR: #0000ff">Values</span>
										<span style="COLOR: #000000"> (</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
										<span style="COLOR: #000000">,</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #ff0000">印刷品</span>
										<span style="COLOR: #ff0000">'</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000">);<br /></span>
										<span style="COLOR: #0000ff">Insert</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Into</span>
										<span style="COLOR: #000000"> tbType </span>
										<span style="COLOR: #0000ff">Values</span>
										<span style="COLOR: #000000"> (</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">2</span>
										<span style="COLOR: #000000">,</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #ff0000">電子書</span>
										<span style="COLOR: #ff0000">'</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000">);<br /></span>
										<span style="COLOR: #0000ff">Insert</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Into</span>
										<span style="COLOR: #000000"> tbBook </span>
										<span style="COLOR: #0000ff">Values</span>
										<span style="COLOR: #000000"> (</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
										<span style="COLOR: #000000">,</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #ff0000">昆蟲學</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">23.5</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000">);<br /></span>
										<span style="COLOR: #0000ff">Insert</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Into</span>
										<span style="COLOR: #000000"> tbBook </span>
										<span style="COLOR: #0000ff">Values</span>
										<span style="COLOR: #000000"> (</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">2</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
										<span style="COLOR: #000000">,</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #ff0000">海洋生物探索</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">89.9</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000">);<br /></span>
										<span style="COLOR: #0000ff">Insert</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Into</span>
										<span style="COLOR: #000000"> tbBook </span>
										<span style="COLOR: #0000ff">Values</span>
										<span style="COLOR: #000000"> (</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">3</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">2</span>
										<span style="COLOR: #000000">,</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #ff0000">昆蟲學</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">5</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000">);<br /></span>
										<span style="COLOR: #0000ff">Insert</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Into</span>
										<span style="COLOR: #000000"> tbBook </span>
										<span style="COLOR: #0000ff">Values</span>
										<span style="COLOR: #000000"> (</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">4</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">2</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
										<span style="COLOR: #000000">,</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #ff0000">中國地理</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">51</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000">);<br /></span>
										<span style="COLOR: #0000ff">Insert</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Into</span>
										<span style="COLOR: #000000"> tbBook </span>
										<span style="COLOR: #0000ff">Values</span>
										<span style="COLOR: #000000"> (</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">5</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">2</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
										<span style="COLOR: #000000">,</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #ff0000">世界地理</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">85</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000">);<br /></span>
										<span style="COLOR: #0000ff">Insert</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Into</span>
										<span style="COLOR: #000000"> tbBook </span>
										<span style="COLOR: #0000ff">Values</span>
										<span style="COLOR: #000000"> (</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">6</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">2</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">2</span>
										<span style="COLOR: #000000">,</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #ff0000">中國地理</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">6.8</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000">);<br /></span>
										<span style="COLOR: #0000ff">Insert</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Into</span>
										<span style="COLOR: #000000"> tbBook </span>
										<span style="COLOR: #0000ff">Values</span>
										<span style="COLOR: #000000"> (</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">7</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">2</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">2</span>
										<span style="COLOR: #000000">,</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #ff0000">地理圖冊</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">15</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000">);<br /></span>
										<span style="COLOR: #0000ff">Insert</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Into</span>
										<span style="COLOR: #000000"> tbBook </span>
										<span style="COLOR: #0000ff">Values</span>
										<span style="COLOR: #000000"> (</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">8</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">3</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
										<span style="COLOR: #000000">,</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #ff0000">JAVA</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">105</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000">);<br /></span>
										<span style="COLOR: #0000ff">Insert</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Into</span>
										<span style="COLOR: #000000"> tbBook </span>
										<span style="COLOR: #0000ff">Values</span>
										<span style="COLOR: #000000"> (</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">9</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">3</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
										<span style="COLOR: #000000">,</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #ff0000">NET編輯</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">95</span>
								</font>
						</font>
						<font size="2">
								<font face="Georgia">
										<span style="COLOR: #000000">);<br /></span>
										<span style="COLOR: #0000ff">Insert</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Into</span>
										<span style="COLOR: #000000"> tbBook </span>
										<span style="COLOR: #0000ff">Values</span>
										<span style="COLOR: #000000"> (</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">10</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">3</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
										<span style="COLOR: #000000">,</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #ff0000">1小時精通JAVA</span>
										<span style="COLOR: #ff0000">'</span>
										<span style="COLOR: #000000">,</span>
										<span style="FONT-WEIGHT: bold; COLOR: #800000">35</span>
										<span style="COLOR: #000000">);</span>
								</font>
						</font>
				</span>
		</div>
		<font face="Georgia" size="2">      可以看到這3個層表現出的是一個3層的結構，tbCategory 是第一層代表大的類別 tbType 是第二層代表出版物的類型 tbBook 是最下一層，現在我們想要實現分類匯總，需要實現下面的效果：<br />效果表：<br /></font>
		<table width="400" border="1">
				<tbody>
						<tr>
								<th>
										<font face="Georgia" size="2">名稱</font>
								</th>
								<th>
										<font face="Georgia" size="2">金額</font>
								</th>
								<th>
										<font face="Georgia" size="2">數量</font>
								</th>
						</tr>
						<tr>
								<td>
										<font face="Georgia" size="2">生物</font>
								</td>
								<td>
										<font face="Georgia" size="2">118.4</font>
								</td>
								<td>
										<font face="Georgia" size="2">3</font>
								</td>
						</tr>
						<tr>
								<td>
										<font face="Georgia" size="2">  印刷品</font>
								</td>
								<td>
										<font face="Georgia" size="2">113.4</font>
								</td>
								<td>
										<font face="Georgia" size="2">2</font>
								</td>
						</tr>
						<tr>
								<td>
										<font face="Georgia" size="2">    昆蟲學</font>
								</td>
								<td>
										<font face="Georgia" size="2">23.5</font>
								</td>
								<td>
										<font face="Georgia" size="2">1</font>
								</td>
						</tr>
						<tr>
								<td>
										<font face="Georgia" size="2">    海洋生物探索</font>
								</td>
								<td>
										<font face="Georgia" size="2">89.9</font>
								</td>
								<td>
										<font face="Georgia" size="2">1</font>
								</td>
						</tr>
						<tr>
								<td>
										<font face="Georgia" size="2">  電子書</font>
								</td>
								<td>
										<font face="Georgia" size="2">5</font>
								</td>
								<td>
										<font face="Georgia" size="2">1</font>
								</td>
						</tr>
						<tr>
								<td>
										<font face="Georgia" size="2">    昆蟲學</font>
								</td>
								<td>
										<font face="Georgia" size="2">5</font>
								</td>
								<td>
										<font face="Georgia" size="2">1</font>
								</td>
						</tr>
				</tbody>
		</table>
		<font face="Georgia" size="2">      從這張效果表可以看到，我們需要對數據分類匯總才能實現如上表的效果。<br /><br /><br /><strong>解決：</strong><br /><br />      平時如果只是2層的關係，直接在SQL語句中實現 Group By 關鍵字即可以實現，但是現在的3層呢？答案是相同的，只是需要將2次匯總的結果集 Union 就好，下面將步驟分解來看：<br />      1. 我們先對類別實施匯總<br /></font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<span style="COLOR: #0000ff">
						<font face="Georgia" size="2">Select</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">        cID,</font>
				</span>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #ff00ff">Sum</span>
								<span style="COLOR: #000000">(price) </span>
								<span style="COLOR: #0000ff">as</span>
								<span style="COLOR: #000000"> price,</span>
								<span style="COLOR: #ff00ff">Count</span>
								<span style="COLOR: #000000">(</span>
								<span style="COLOR: #808080">*</span>
								<span style="COLOR: #000000">) </span>
								<span style="COLOR: #0000ff">as</span>
						</font>
				</font>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #000000"> bookCount<br />    </span>
								<span style="COLOR: #0000ff">From</span>
						</font>
				</font>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #000000"> <br />        tbBook<br />    </span>
								<span style="COLOR: #0000ff">Group</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">By</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">        cID</font>
				</span>
		</div>
		<font face="Georgia" size="2">      2. 我們再對出版物的類別匯總<br /></font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<span style="COLOR: #0000ff">
						<font face="Georgia" size="2">Select</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">        cID,tID,</font>
				</span>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #ff00ff">Sum</span>
								<span style="COLOR: #000000">(price) </span>
								<span style="COLOR: #0000ff">as</span>
								<span style="COLOR: #000000"> price,</span>
								<span style="COLOR: #ff00ff">Count</span>
								<span style="COLOR: #000000">(</span>
								<span style="COLOR: #808080">*</span>
								<span style="COLOR: #000000">) </span>
								<span style="COLOR: #0000ff">as</span>
						</font>
				</font>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #000000"> bookCount<br />    </span>
								<span style="COLOR: #0000ff">From</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">        tbBook<br />    </font>
				</span>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #0000ff">Group</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">By</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">        cID,tID</font>
				</span>
		</div>
		<font face="Georgia" size="2">      3. 將上面兩條 SQL Union<br /></font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<span style="COLOR: #0000ff">
						<font face="Georgia" size="2">Select</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">        cID,</font>
				</span>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #ff0000">''</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">as</span>
								<span style="COLOR: #000000"> tID,</span>
								<span style="COLOR: #ff00ff">Sum</span>
								<span style="COLOR: #000000">(price) </span>
								<span style="COLOR: #0000ff">as</span>
								<span style="COLOR: #000000"> price,</span>
								<span style="COLOR: #ff00ff">Count</span>
								<span style="COLOR: #000000">(</span>
								<span style="COLOR: #808080">*</span>
								<span style="COLOR: #000000">) </span>
								<span style="COLOR: #0000ff">as</span>
						</font>
				</font>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #000000"> bookCount<br />    </span>
								<span style="COLOR: #0000ff">From</span>
						</font>
				</font>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #000000"> <br />        tbBook<br />    </span>
								<span style="COLOR: #0000ff">Group</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">By</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">        cID<br /></font>
				</span>
				<span style="COLOR: #0000ff">
						<font face="Georgia" size="2">Union</font>
				</span>
				<span style="COLOR: #000000">
						<br />
				</span>
				<span style="COLOR: #0000ff">
						<font face="Georgia" size="2">Select</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">        cID,tID,</font>
				</span>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #ff00ff">Sum</span>
								<span style="COLOR: #000000">(price) </span>
								<span style="COLOR: #0000ff">as</span>
								<span style="COLOR: #000000"> price,</span>
								<span style="COLOR: #ff00ff">Count</span>
								<span style="COLOR: #000000">(</span>
								<span style="COLOR: #808080">*</span>
								<span style="COLOR: #000000">) </span>
								<span style="COLOR: #0000ff">as</span>
						</font>
				</font>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #000000"> bookCount<br />    </span>
								<span style="COLOR: #0000ff">From</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">        tbBook<br />    </font>
				</span>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #0000ff">Group</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">By</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">        cID,tID<br /></font>
				</span>
				<font size="2">
						<font face="Georgia">
								<span style="COLOR: #0000ff">Order</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">By</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font face="Georgia" size="2">    cID,tID</font>
				</span>
		</div>
		<font face="Georgia" size="2">      出來的結果是這樣：<br /></font>
		<table width="400" border="1">
				<tbody>
						<tr>
								<th>
										<font face="Georgia" size="2">cID</font>
								</th>
								<th>
										<font face="Georgia" size="2">tID</font>
								</th>
								<th>
										<font face="Georgia" size="2">price</font>
								</th>
								<th>
										<font face="Georgia" size="2">bookCount</font>
								</th>
						</tr>
						<tr>
								<td>
										<font face="Georgia" size="2">1</font>
								</td>
								<td>
										<font face="Georgia" size="2">0</font>
								</td>
								<td>
										<font face="Georgia" size="2">118.4</font>
								</td>
								<td>
										<font face="Georgia" size="2">3</font>
								</td>
						</tr>
						<tr>
								<td>
										<font face="Georgia" size="2">1</font>
								</td>
								<td>
										<font face="Georgia" size="2">1</font>
								</td>
								<td>
										<font face="Georgia" size="2">113.4</font>
								</td>
								<td>
										<font face="Georgia" size="2">2</font>
								</td>
						</tr>
						<tr>
								<td>
										<font face="Georgia" size="2">1</font>
								</td>
								<td>
										<font face="Georgia" size="2">2</font>
								</td>
								<td>
										<font face="Georgia" size="2">5</font>
								</td>
								<td>
										<font face="Georgia" size="2">1</font>
								</td>
						</tr>
						<tr>
								<td>
										<font face="Georgia" size="2">2</font>
								</td>
								<td>
										<font face="Georgia" size="2">0</font>
								</td>
								<td>
										<font face="Georgia" size="2">157.8</font>
								</td>
								<td>
										<font face="Georgia" size="2">4</font>
								</td>
						</tr>
						<tr>
								<td>
										<font face="Georgia" size="2">2</font>
								</td>
								<td>
										<font face="Georgia" size="2">1</font>
								</td>
								<td>
										<font face="Georgia" size="2">136</font>
								</td>
								<td>
										<font face="Georgia" size="2">2</font>
								</td>
						</tr>
						<tr>
								<td>
										<font face="Georgia" size="2">2</font>
								</td>
								<td>
										<font face="Georgia" size="2">2</font>
								</td>
								<td>
										<font face="Georgia" size="2">21.8</font>
								</td>
								<td>
										<font face="Georgia" size="2">2</font>
								</td>
						</tr>
						<tr>
								<td>
										<font face="Georgia" size="2">3</font>
								</td>
								<td>
										<font face="Georgia" size="2">0</font>
								</td>
								<td>
										<font face="Georgia" size="2">235.5</font>
								</td>
								<td>
										<font face="Georgia" size="2">3</font>
								</td>
						</tr>
						<tr>
								<td>
										<font face="Georgia" size="2">3</font>
								</td>
								<td>
										<font face="Georgia" size="2">1</font>
								</td>
								<td>
										<font face="Georgia" size="2">235.5</font>
								</td>
								<td>
										<font face="Georgia" size="2">3</font>
								</td>
						</tr>
				</tbody>
		</table>
		<font face="Georgia" size="2">      4. 代入類別名稱和出版物類型的名稱並 Union tbBook表的記錄，得到最終的結果集，为了能够达到排序的效果，我们会在这一步插入一个标识列，用它来控制显示的顺序：<br /></font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font face="Georgia">
						<font size="2">
								<span style="COLOR: #0000ff">Select</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #ff0000">0</span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">as</span>
								<span style="COLOR: #000000"> flag,tbBook.cID </span>
								<span style="COLOR: #0000ff">as</span>
								<span style="COLOR: #000000"> cID,</span>
								<span style="COLOR: #ff00ff">min</span>
								<span style="COLOR: #000000">(tbBook.tID) </span>
								<span style="COLOR: #0000ff">as</span>
								<span style="COLOR: #000000"> tID,</span>
								<span style="COLOR: #ff00ff">min</span>
								<span style="COLOR: #000000">(tbCategory.cName) </span>
								<span style="COLOR: #0000ff">as</span>
								<span style="COLOR: #000000"> cName,</span>
								<span style="COLOR: #ff00ff">Sum</span>
								<span style="COLOR: #000000">(price) </span>
								<span style="COLOR: #0000ff">as</span>
								<span style="COLOR: #000000"> price,</span>
								<span style="COLOR: #ff00ff">Count</span>
								<span style="COLOR: #000000">(</span>
								<span style="COLOR: #808080">*</span>
								<span style="COLOR: #000000">) </span>
								<span style="COLOR: #0000ff">as</span>
								<span style="COLOR: #000000"> bookCount </span>
								<span style="COLOR: #0000ff">From</span>
								<span style="COLOR: #000000"> tbBook </span>
								<span style="COLOR: #0000ff">Join</span>
								<span style="COLOR: #000000"> tbCategory </span>
								<span style="COLOR: #0000ff">On</span>
								<span style="COLOR: #000000"> tbBook.cID</span>
								<span style="COLOR: #808080">=</span>
								<span style="COLOR: #000000">tbCategory.cID </span>
								<span style="COLOR: #0000ff">Group</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">By</span>
						</font>
				</font>
				<font face="Georgia">
						<font size="2">
								<span style="COLOR: #000000"> tbBook.cID<br /></span>
								<span style="COLOR: #0000ff">union</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
				</span>
				<font face="Georgia">
						<font size="2">
								<span style="COLOR: #0000ff">Select</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #ff0000">1</span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #000000">,tbBook.cID,tbBook.tID,</span>
								<span style="COLOR: #ff00ff">min</span>
								<span style="COLOR: #000000">(tbType.tName),</span>
								<span style="COLOR: #ff00ff">Sum</span>
								<span style="COLOR: #000000">(price) </span>
								<span style="COLOR: #0000ff">as</span>
								<span style="COLOR: #000000"> price,</span>
								<span style="COLOR: #ff00ff">Count</span>
								<span style="COLOR: #000000">(</span>
								<span style="COLOR: #808080">*</span>
								<span style="COLOR: #000000">) </span>
								<span style="COLOR: #0000ff">From</span>
								<span style="COLOR: #000000"> tbBook </span>
								<span style="COLOR: #0000ff">Join</span>
								<span style="COLOR: #000000"> tbType </span>
								<span style="COLOR: #0000ff">On</span>
								<span style="COLOR: #000000"> tbBook.tID</span>
								<span style="COLOR: #808080">=</span>
								<span style="COLOR: #000000">tbType.tID  </span>
								<span style="COLOR: #0000ff">Group</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">By</span>
						</font>
				</font>
				<font face="Georgia">
						<font size="2">
								<span style="COLOR: #000000"> tbBook.cID,tbBook.tID<br /></span>
								<span style="COLOR: #0000ff">union</span>
						</font>
				</font>
				<span style="COLOR: #000000">
						<br />
				</span>
				<font face="Georgia">
						<font size="2">
								<span style="COLOR: #0000ff">Select</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #ff0000">2</span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #000000">,cID,tID,bName,price,</span>
								<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">From</span>
						</font>
				</font>
				<font face="Georgia">
						<font size="2">
								<span style="COLOR: #000000"> tbBook<br /></span>
								<span style="COLOR: #0000ff">Order</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">By</span>
								<span style="COLOR: #000000"> cID,tID,Flag</span>
						</font>
				</font>
		</div>
		<font face="Georgia" size="2">      這樣即可實現最終的效果了。</font>
<img src ="http://www.blogjava.net/tw-ddm/aggbug/115292.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/tw-ddm/" target="_blank">大大毛</a> 2007-05-04 11:52 <a href="http://www.blogjava.net/tw-ddm/articles/115292.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>複雜SQL語句的構建以及內部的封裝調試--續章1(視圖變更)</title><link>http://www.blogjava.net/tw-ddm/articles/98977.html</link><dc:creator>大大毛</dc:creator><author>大大毛</author><pubDate>Fri, 09 Feb 2007 04:52:00 GMT</pubDate><guid>http://www.blogjava.net/tw-ddm/articles/98977.html</guid><wfw:comment>http://www.blogjava.net/tw-ddm/comments/98977.html</wfw:comment><comments>http://www.blogjava.net/tw-ddm/articles/98977.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/tw-ddm/comments/commentRss/98977.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/tw-ddm/services/trackbacks/98977.html</trackback:ping><description><![CDATA[
		<font face="Georgia" size="2">
				<strong>前言</strong>：<br />      上回提到過的架構及一些細節上的分析，馬上(時隔2天)就要拉出來蹓蹓了。這次的起因是需求變動。對頁面的顯示效果進行變更。<br /><br /><strong>需求變動：</strong><br />      要求將相同職場的單位進行合併，以前產出的記錄集會是這樣：<br /><table cellspacing="0" cellpadding="0" width="200" border="1"><tbody><tr><th>職場單位</th><th>職場</th><th>其它信息</th></tr><tr><td>Unit1</td><td>AA001</td><td>....</td></tr><tr><td>Unit2</td><td>AA001</td><td>....</td></tr><tr><td>Unit3</td><td>AA002</td><td>....</td></tr></tbody></table>      現在頁面要顯示成這樣：<br /><table cellspacing="0" cellpadding="0" width="200" border="1"><tbody><tr><th>職場單位</th><th>職場</th><th>其它信息</th></tr><tr><td>Unit1,Unit2</td><td>AA001</td><td>....</td></tr><tr><td>Unit3</td><td>AA002</td><td>....</td></tr></tbody></table><br /><strong>思考及方案選擇：</strong><br />      這次的變動算是不大，對邏輯沒有影響，難度較低，可選方案有2個。<br />      1. 直接修改視圖，例如在ASP頁面中對產出記錄集的職場單位字段按職場實施合併;<br />      2. 在產出記錄集前實施修改，例如在VB的DLL代碼中增加處理。<br />      沒有什麼可以盤旋的余地，直接選擇方案2，原因是<br />      a. 模塊的視圖掛了兩種，一種是ASP頁面的“預覽”，另一種是Excel數據文件的產出，方案1的變更需要在多處進行。<br />      b. 留下適應再次變更的余地。<br /><br /><strong>實施細節：</strong><br />      這次的變更屬於視圖層的變更，不需要去變更實現邏輯，因此不會考慮去變更實施邏輯的SQL(而且SQL也不擅長解決此類問題)，采取體外手術的方式解決。<br />      實施的流程示例：<br />            SQL－＞原RS－＞處理(適應這次的需求變更)－＞新RS－＞產出<br />      這樣一來改動的影響會是極其特別以及肯定的非常小，在cls中增加功能函數就好。<br /><br /><strong>代碼：<br /></strong>      在原來的功能模塊fGetFreePlace中插入一行<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">If</span><span style="COLOR: #000000"> pretreatmentRsPlace(rsPlace) </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">False</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Then</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">GoTo</span><span style="COLOR: #000000"> ErrHandler<br /></span></div>      增加的處理FUNCTION，為能重復使用分割成2個了(cloneRsFrame是准備以後復用的)。<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008000">'</span><span style="COLOR: #008000">'****************************************************************************************************</span><span style="COLOR: #008000"><br />'</span><span style="COLOR: #008000">*程式功能  :       預處理"待退（閒置）租約資料報表"所需的職場記錄集</span><span style="COLOR: #008000"><br />'</span><span style="COLOR: #008000">*功能說明  :       現階段要求將相同"職場" &amp;&amp; "同狀態"的單位合併成一條記錄,單位檔合併成"單位1,單位2",以逗號分隔</span><span style="COLOR: #008000"><br />'</span><span style="COLOR: #008000">*開發人員  :　     ddm 2006/12/17</span><span style="COLOR: #008000"><br />'</span><span style="COLOR: #008000">'****************************************************************************************************<br /></span><span style="COLOR: #0000ff">Private</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Function</span><span style="COLOR: #000000"> pretreatmentRsPlace(</span><span style="COLOR: #0000ff">ByRef</span><span style="COLOR: #000000"> rsPlace </span><span style="COLOR: #0000ff">As</span><span style="COLOR: #000000"> Variant) </span><span style="COLOR: #0000ff">As</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Boolean</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">On</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Error</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">GoTo</span><span style="COLOR: #000000"> ErrHandler<br />    </span><span style="COLOR: #0000ff">Dim</span><span style="COLOR: #000000"> rsResult </span><span style="COLOR: #0000ff">As</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">New</span><span style="COLOR: #000000"> ADODB.Recordset<br />    </span><span style="COLOR: #0000ff">Dim</span><span style="COLOR: #000000"> tmpArr </span><span style="COLOR: #0000ff">As</span><span style="COLOR: #000000"> Variant<br />    </span><span style="COLOR: #0000ff">Dim</span><span style="COLOR: #000000"> strCPlaceCode </span><span style="COLOR: #0000ff">As</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">String</span><span style="COLOR: #000000">, strFlagState </span><span style="COLOR: #0000ff">As</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">String</span><span style="COLOR: #000000">, strCUnitName </span><span style="COLOR: #0000ff">As</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">String</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">Dim</span><span style="COLOR: #000000"> strCPlaceCodeFlag </span><span style="COLOR: #0000ff">As</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">String</span><span style="COLOR: #000000">, strFlagStateFlag </span><span style="COLOR: #0000ff">As</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">String</span><span style="COLOR: #000000">, strCUnitNameList </span><span style="COLOR: #0000ff">As</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">String</span><span style="COLOR: #000000">, index </span><span style="COLOR: #0000ff">As</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Long</span><span style="COLOR: #000000">, i </span><span style="COLOR: #0000ff">As</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Long</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #008000">'</span><span style="COLOR: #008000">如果源記錄集為空則返回</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">If</span><span style="COLOR: #000000"> rsPlace.EOF </span><span style="COLOR: #0000ff">Then</span><span style="COLOR: #000000"><br />        pretreatmentRsPlace </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">True</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #0000ff">Exit Function</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">End</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">If</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #008000">'</span><span style="COLOR: #008000">准備記錄集</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">Set</span><span style="COLOR: #000000"> rsResult </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> cloneRsFrame(rsPlace)<br />    </span><span style="COLOR: #0000ff">If</span><span style="COLOR: #000000"> rsResult </span><span style="COLOR: #0000ff">Is</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Nothing</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Then</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">GoTo</span><span style="COLOR: #000000"> ErrHandler<br />    <br />    </span><span style="COLOR: #008000">'</span><span style="COLOR: #008000">變量初始化</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">ReDim</span><span style="COLOR: #000000"> tmpArr(rsResult.Fields.Count </span><span style="COLOR: #000000">-</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)<br />    strCPlaceCodeFlag </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">i am ddm</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><br />    strCUnitNameList </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">""</span><span style="COLOR: #000000"><br />    index </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">Do</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">While</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Not</span><span style="COLOR: #000000"> rsPlace.EOF<br />        strCUnitName </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Trim</span><span style="COLOR: #000000">(rsPlace(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">CUnitName</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">))<br />        strCPlaceCode </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Trim</span><span style="COLOR: #000000">(rsPlace(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">CPlaceCode</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">))<br />        strFlagState </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Trim</span><span style="COLOR: #000000">(rsPlace(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">FlagState</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">))<br />        </span><span style="COLOR: #008000">'</span><span style="COLOR: #008000">比較是否一條新的職場記錄,由於同一個單位既可能屬於"閒置",同時該職場在考察期也可能會"退租",因此分開</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">If</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Not</span><span style="COLOR: #000000"> ((strCPlaceCodeFlag </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> strCPlaceCode) </span><span style="COLOR: #0000ff">And</span><span style="COLOR: #000000"> (strFlagStateFlag </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> strFlagState)) </span><span style="COLOR: #0000ff">Then</span><span style="COLOR: #000000"><br />            </span><span style="COLOR: #0000ff">If</span><span style="COLOR: #000000"> index </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Then</span><span style="COLOR: #000000"><br />                </span><span style="COLOR: #008000">'</span><span style="COLOR: #008000">第1條記錄,更新標識</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">            </span><span style="COLOR: #0000ff">Else</span><span style="COLOR: #000000"><br />                </span><span style="COLOR: #008000">'</span><span style="COLOR: #008000">更新記錄集</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">                rsResult.AddNew<br />                </span><span style="COLOR: #0000ff">For</span><span style="COLOR: #000000"> i </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">To</span><span style="COLOR: #000000"> rsResult.Fields.Count </span><span style="COLOR: #000000">-</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000"><br />                    rsResult(i) </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> tmpArr(i)<br />                </span><span style="COLOR: #0000ff">Next</span><span style="COLOR: #000000"><br />                </span><span style="COLOR: #008000">'</span><span style="COLOR: #008000">使用合併單位名稱列表的值</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">                rsResult(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">CUnitName</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">) </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> strCUnitNameList<br />                </span><span style="COLOR: #008000">'</span><span style="COLOR: #008000">清除單位列表，保存標識</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">                strCUnitNameList </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">""</span><span style="COLOR: #000000"><br />            </span><span style="COLOR: #0000ff">End</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">If</span><span style="COLOR: #000000"><br />            strCPlaceCodeFlag </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> strCPlaceCode<br />            strFlagStateFlag </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> strFlagState<br />        </span><span style="COLOR: #0000ff">Else</span><span style="COLOR: #000000"><br />            </span><span style="COLOR: #008000">'</span><span style="COLOR: #008000"> do nothing</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">End</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">If</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #008000">'</span><span style="COLOR: #008000">保存當前記錄</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">For</span><span style="COLOR: #000000"> i </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">To</span><span style="COLOR: #000000"> rsPlace.Fields.Count </span><span style="COLOR: #000000">-</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000"><br />            tmpArr(i) </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> rsPlace(i)<br />        </span><span style="COLOR: #0000ff">Next</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #008000">'</span><span style="COLOR: #008000">合併單位名稱</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">If</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Len</span><span style="COLOR: #000000">(strCUnitNameList) </span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Then</span><span style="COLOR: #000000"><br />            strCUnitNameList </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> strCUnitName<br />        </span><span style="COLOR: #0000ff">Else</span><span style="COLOR: #000000"><br />            strCUnitNameList </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> strCUnitNameList </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">, </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000"> strCUnitName<br />        </span><span style="COLOR: #0000ff">End</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">If</span><span style="COLOR: #000000"><br />        <br />        index </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> index </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000"><br />        rsPlace.MoveNext<br />    </span><span style="COLOR: #0000ff">Loop</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #008000">'</span><span style="COLOR: #008000">更新記錄集</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    rsResult.AddNew<br />    </span><span style="COLOR: #0000ff">For</span><span style="COLOR: #000000"> i </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">To</span><span style="COLOR: #000000"> rsResult.Fields.Count </span><span style="COLOR: #000000">-</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000"><br />        rsResult(i) </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> tmpArr(i)<br />    </span><span style="COLOR: #0000ff">Next</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #008000">'</span><span style="COLOR: #008000">使用合併單位名稱列表的值</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    rsResult(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">CUnitName</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">) </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> strCUnitNameList<br />    <br />    </span><span style="COLOR: #008000">'</span><span style="COLOR: #008000">處理完畢,關閉源記錄集,返回處理過的記錄集</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">If</span><span style="COLOR: #000000"> rsPlace.State </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Then</span><span style="COLOR: #000000"> rsPlace.Close<br />    </span><span style="COLOR: #0000ff">Set</span><span style="COLOR: #000000"> rsPlace </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> rsResult<br />    rsPlace.MoveFirst<br />    <br />    pretreatmentRsPlace </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">True</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">Exit Function</span><span style="COLOR: #000000"><br />ErrHandler:<br />    pretreatmentRsPlace </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">False</span><span style="COLOR: #000000"><br />    objCommon.WriteErrLog TheMdlName, </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">pretreatmentRsPlace</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">, </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">克隆記錄集結構異常</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">, Err.Number, Err.Description<br /></span><span style="COLOR: #0000ff">End Function</span><span style="COLOR: #000000"><br /><br /></span><span style="COLOR: #008000">'</span><span style="COLOR: #008000">****************************************************************************************************</span><span style="COLOR: #008000">*<br />'*程式功能  :       克隆記錄集結構</span><span style="COLOR: #008000"><br />'</span><span style="COLOR: #008000">*開發人員  :       ddm 2006/12/19</span><span style="COLOR: #008000"><br />'</span><span style="COLOR: #008000">*異動人員</span><span style="COLOR: #008000"><br />'</span><span style="COLOR: #008000">*傳入值    :       templetRs           --樣本記錄集</span><span style="COLOR: #008000"><br />'</span><span style="COLOR: #008000">*回傳值    :       Recordset           --克隆記錄集</span><span style="COLOR: #008000"><br />'</span><span style="COLOR: #008000">*****************************************************************************************************</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #0000ff">Public</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Function</span><span style="COLOR: #000000"> cloneRsFrame(templetRs </span><span style="COLOR: #0000ff">As</span><span style="COLOR: #000000"> Variant) </span><span style="COLOR: #0000ff">As</span><span style="COLOR: #000000"> ADODB.Recordset<br /></span><span style="COLOR: #0000ff">On</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Error</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">GoTo</span><span style="COLOR: #000000"> ErrHandler<br />    </span><span style="COLOR: #0000ff">Dim</span><span style="COLOR: #000000"> newRs </span><span style="COLOR: #0000ff">As</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">New</span><span style="COLOR: #000000"> ADODB.Recordset, f </span><span style="COLOR: #0000ff">As</span><span style="COLOR: #000000"> Field<br />    </span><span style="COLOR: #0000ff">If</span><span style="COLOR: #000000"> templetRs </span><span style="COLOR: #0000ff">Is</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Nothing</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Then</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #0000ff">Set</span><span style="COLOR: #000000"> cloneRsFrame </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> Null<br />    </span><span style="COLOR: #0000ff">Else</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #0000ff">Set</span><span style="COLOR: #000000"> newRs </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">New</span><span style="COLOR: #000000"> ADODB.Recordset<br />        </span><span style="COLOR: #008000">'</span><span style="COLOR: #008000">克隆字段結構</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">For</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Each</span><span style="COLOR: #000000"> f </span><span style="COLOR: #0000ff">In</span><span style="COLOR: #000000"> templetRs.Fields<br />           newRs.Fields.Append f.Name, f.Type, f.DefinedSize, adFldIsNullable<br />        </span><span style="COLOR: #0000ff">Next</span><span style="COLOR: #000000"><br />        newRs.Open<br />    </span><span style="COLOR: #0000ff">End</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">If</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">Set</span><span style="COLOR: #000000"> cloneRsFrame </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> newRs<br />    </span><span style="COLOR: #0000ff">Exit Function</span><span style="COLOR: #000000"><br />ErrHandler:<br />    </span><span style="COLOR: #0000ff">Set</span><span style="COLOR: #000000"> cloneRsFrame </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Nothing</span><span style="COLOR: #000000"><br />    objCommon.WriteErrLog TheMdlName, </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">cloneRsFrame</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">, </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">克隆記錄集結構異常</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">, Err.Number, Err.Description<br /></span><span style="COLOR: #0000ff">End Function</span></div><br /><strong>後記：</strong><br />      算是一個小插曲，不過也是從這次的實現中(cloneRsFrame)受益。<br />      後來USER增加了資料匯入的功能，當俺們完成後就發現不得不“適當”的修改一下表(僅僅是因為USER在Excel導入文件中某些列上寫了過多的說明而已)，修改表是最簡單的那種，放列寬。<br />      有兄弟的模塊中使用了這樣的代碼：<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">Dim</span><span style="COLOR: #000000"> rsPlace </span><span style="COLOR: #0000ff">as</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000"> ADODB.RecordSet<br />    <strong>rsPlace.Fields.Append </strong></span><strong><span style="COLOR: #000000">"</span><span style="COLOR: #000000">CAddress</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">, adChar, </span><span style="COLOR: #000000">200</span></strong><span style="COLOR: #000000"><br />    rsPlace.AddNew<br />    rsPlace(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">CAddress</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">) </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">Trim</span><span style="COLOR: #000000">(rsSource(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">CAddress</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">))<br />   </span><span style="COLOR: #008000"><img src="http://www.blogjava.net/images/dot.gif" />.</span><span style="COLOR: #008000"><br /></span></div><br />      當N列放寬後，終於...掛了。</font>
<img src ="http://www.blogjava.net/tw-ddm/aggbug/98977.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/tw-ddm/" target="_blank">大大毛</a> 2007-02-09 12:52 <a href="http://www.blogjava.net/tw-ddm/articles/98977.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>外聯陷阱</title><link>http://www.blogjava.net/tw-ddm/articles/94905.html</link><dc:creator>大大毛</dc:creator><author>大大毛</author><pubDate>Fri, 19 Jan 2007 04:47:00 GMT</pubDate><guid>http://www.blogjava.net/tw-ddm/articles/94905.html</guid><wfw:comment>http://www.blogjava.net/tw-ddm/comments/94905.html</wfw:comment><comments>http://www.blogjava.net/tw-ddm/articles/94905.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/tw-ddm/comments/commentRss/94905.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/tw-ddm/services/trackbacks/94905.html</trackback:ping><description><![CDATA[
		<font size="2">
				<strong>前言</strong>：<br />      這裡講的外聯指的是 <strong>Left/Right Join</strong>，這兩個是等效的，出於習慣用 Left Join。在遇到需要使用外聯的地方，大家通常會按照 Inner Join 的思路來進行改造，特別是在原有的內聯邏輯需要轉變到外聯邏輯的時候。雖然有某些時候內聯改外聯只要變關鍵字就好，但遺憾的是絕大部分情況都不會如此簡單，得要小心從事才行。下面就我遇到過的例舉一二：<br /><br /><strong>DDL</strong>：<br /></font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font size="2">
						<img id="Code_Closed_Image_121748" onclick="this.style.display='none'; Code_Closed_Text_121748.style.display='none'; Code_Open_Image_121748.style.display='inline'; Code_Open_Text_121748.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" />
						<img id="Code_Open_Image_121748" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_121748.style.display='none'; Code_Closed_Image_121748.style.display='inline'; Code_Closed_Text_121748.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" />
						<span id="Code_Closed_Text_121748" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">示例用DDL</span>
				</font>
				<span id="Code_Open_Text_121748" style="DISPLAY: none">
						<br />
						<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<font size="2">
								<span style="COLOR: #008080">--</span>
								<span style="COLOR: #008080">場地</span>
						</font>
						<span style="COLOR: #008080">
								<br />
						</span>
						<font size="2">
								<span style="COLOR: #0000ff">Create</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">Table</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000"> tbPlace (<br />    CPlaceCode  </span>
								<span style="FONT-WEIGHT: bold; COLOR: #000000">char</span>
								<span style="COLOR: #000000">(</span>
								<span style="FONT-WEIGHT: bold; COLOR: #800000">5</span>
								<span style="COLOR: #000000">) </span>
								<span style="COLOR: #0000ff">primary</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">key</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000">,<br />    CIsUse      </span>
								<span style="FONT-WEIGHT: bold; COLOR: #000000">char</span>
								<span style="COLOR: #000000">(</span>
								<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000">),<br />    CDelFlag    </span>
								<span style="FONT-WEIGHT: bold; COLOR: #000000">char</span>
								<span style="COLOR: #000000">(</span>
								<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000">)<br />)<br /></span>
								<span style="COLOR: #008080">--</span>
								<span style="COLOR: #008080">合約</span>
						</font>
						<span style="COLOR: #008080">
								<br />
						</span>
						<font size="2">
								<span style="COLOR: #0000ff">Create</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">Table</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000"> tbContractRent (<br />    CContractId </span>
								<span style="FONT-WEIGHT: bold; COLOR: #000000">char</span>
								<span style="COLOR: #000000">(</span>
								<span style="FONT-WEIGHT: bold; COLOR: #800000">5</span>
								<span style="COLOR: #000000">) </span>
								<span style="COLOR: #0000ff">primary</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">key</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000">,<br />    CPlaceCode  </span>
								<span style="FONT-WEIGHT: bold; COLOR: #000000">char</span>
								<span style="COLOR: #000000">(</span>
								<span style="FONT-WEIGHT: bold; COLOR: #800000">5</span>
								<span style="COLOR: #000000">) </span>
								<span style="COLOR: #808080">not</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">null</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000">,<br />    CEndDate    </span>
								<span style="FONT-WEIGHT: bold; COLOR: #000000">char</span>
								<span style="COLOR: #000000">(</span>
								<span style="FONT-WEIGHT: bold; COLOR: #800000">8</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000">),<br />    CDelFlag    </span>
								<span style="FONT-WEIGHT: bold; COLOR: #000000">char</span>
								<span style="COLOR: #000000">(</span>
								<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000">)<br />)<br /></span>
								<span style="COLOR: #008080">--</span>
								<span style="COLOR: #008080">合約房東</span>
						</font>
						<span style="COLOR: #008080">
								<br />
						</span>
						<font size="2">
								<span style="COLOR: #0000ff">Create</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">Table</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000"> tbContractOwner (<br />    CContractId </span>
								<span style="FONT-WEIGHT: bold; COLOR: #000000">char</span>
								<span style="COLOR: #000000">(</span>
								<span style="FONT-WEIGHT: bold; COLOR: #800000">5</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000">),<br />    COwnerId    </span>
								<span style="FONT-WEIGHT: bold; COLOR: #000000">char</span>
								<span style="COLOR: #000000">(</span>
								<span style="FONT-WEIGHT: bold; COLOR: #800000">5</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000">),<br />    CDelFlag    </span>
								<span style="FONT-WEIGHT: bold; COLOR: #000000">char</span>
								<span style="COLOR: #000000">(</span>
								<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000">),<br />    </span>
								<span style="COLOR: #0000ff">primary</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">key</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000">(CContractId,COwnerId)<br />)<br /></span>
								<span style="COLOR: #008080">--</span>
								<span style="COLOR: #008080">房東</span>
						</font>
						<span style="COLOR: #008080">
								<br />
						</span>
						<font size="2">
								<span style="COLOR: #0000ff">Create</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">Table</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000"> tbOwner (<br />    COwnerId    </span>
								<span style="FONT-WEIGHT: bold; COLOR: #000000">char</span>
								<span style="COLOR: #000000">(</span>
								<span style="FONT-WEIGHT: bold; COLOR: #800000">5</span>
								<span style="COLOR: #000000">) </span>
								<span style="COLOR: #0000ff">primary</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">key</span>
						</font>
						<font size="2">
								<span style="COLOR: #000000">,<br />    CDelFlag    </span>
								<span style="FONT-WEIGHT: bold; COLOR: #000000">char</span>
								<span style="COLOR: #000000">(</span>
								<span style="FONT-WEIGHT: bold; COLOR: #800000">1</span>
						</font>
						<span style="COLOR: #000000">
								<font size="2">)<br />)</font>
						</span>
				</span>
		</div>
		<font size="2">   表間關係是：<br />      合約－－場地;合約－(合約房東)－房東;<br />   說明：<br />      各表的 CDelFlag 字段是一個刪除標識，CDelFlag='N' 的記錄才是可用的。<br />      場地的 CIsUse 字段是個類型標識，下面的示例中會引用到。<br /><br /><br /><strong>示例1</strong>：<br />    邏輯：<br />         搜尋在指定時段內結束的合約房東。<br />    代入參數：<br />        1.PlaceCode:場地的PK，Like匹配;<br />        2.StartDate:開始日期;<br />        3.EndDate:結束日期。<br /><br />  <strong> DML</strong>：<br /></font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font size="2">
						<span style="COLOR: #000000">DML：<br />    </span>
						<span style="COLOR: #008080">--</span>
						<span style="COLOR: #008080">原邏輯：搜尋在指定時段內結束的(有效)合約房東資料</span>
				</font>
				<span style="COLOR: #008080">
						<br />
				</span>
				<font size="2">
						<span style="COLOR: #000000">    </span>
						<span style="COLOR: #0000ff">Select</span>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            </font>
				</span>
				<span style="COLOR: #808080">
						<font size="2">*</font>
				</span>
				<font size="2">
						<span style="COLOR: #000000"> <br />        </span>
						<span style="COLOR: #0000ff">From</span>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            tbContractRent </font>
				</span>
				<font size="2">
						<span style="COLOR: #0000ff">Join</span>
						<span style="COLOR: #000000"> tbContractOwner </span>
						<span style="COLOR: #0000ff">On</span>
						<span style="COLOR: #000000"> tbContractRent.CContractId</span>
						<span style="COLOR: #808080">=</span>
				</font>
				<font size="2">
						<span style="COLOR: #000000">tbContractOwner.CContractId<br />            </span>
						<span style="COLOR: #0000ff">Join</span>
						<span style="COLOR: #000000"> tbOwner </span>
						<span style="COLOR: #0000ff">On</span>
						<span style="COLOR: #000000"> tbContractOwner.COwnerId</span>
						<span style="COLOR: #808080">=</span>
				</font>
				<font size="2">
						<span style="COLOR: #000000">tbOwner.COwnerId<br />        </span>
						<span style="COLOR: #0000ff">Where</span>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            tbContractRent.CDelFlag</font>
				</span>
				<font size="2">
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #ff0000">'</span>
						<span style="COLOR: #ff0000">N</span>
						<span style="COLOR: #ff0000">'</span>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            </font>
				</span>
				<font size="2">
						<span style="COLOR: #808080">And</span>
						<span style="COLOR: #000000"> tbContractOwner.CDelFlag</span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #ff0000">'</span>
						<span style="COLOR: #ff0000">N</span>
						<span style="COLOR: #ff0000">'</span>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            </font>
				</span>
				<font size="2">
						<span style="COLOR: #808080">And</span>
						<span style="COLOR: #000000"> tbOwner.CDelFlag</span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #ff0000">'</span>
						<span style="COLOR: #ff0000">N</span>
						<span style="COLOR: #ff0000">'</span>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            </font>
				</span>
				<font size="2">
						<span style="COLOR: #808080">And</span>
						<span style="COLOR: #000000"> tbContractRent.CEndDate </span>
						<span style="COLOR: #808080">Between</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@StartDate</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">And</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@EndDate</span>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            </font>
				</span>
				<font size="2">
						<span style="COLOR: #808080">And</span>
						<span style="COLOR: #000000"> tbContractRent.CPlaceCode </span>
						<span style="COLOR: #808080">Like</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #ff0000">'</span>
						<span style="COLOR: #ff0000">%@PlaceCode%</span>
						<span style="COLOR: #ff0000">'</span>
				</font>
		</div>
		<br />
		<font size="2">       可以看到，這段DML代碼的邏輯是非常簡單的，構造也很容易，就是將 tbContractRent 與 tbOwner 通過中間表 tbContractOwner來連接，只要代入參數以及實現 CDelFlag = 'N' 就好。<br /><br />      <strong>邏輯變更</strong>:<br />         搜尋在指定時段內結束的所有(<strong>有效</strong>)合約且帶出房東資料(部分合約無匹配的房東資料)<br />         根據邏輯此時要做的是需要將上面的內聯改成外聯關係。<br /></font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font size="2">
						<span style="COLOR: #008080">--</span>
						<span style="COLOR: #008080">陷阱1：</span>
				</font>
				<span style="COLOR: #008080">
						<br />
				</span>
				<font size="2">
						<span style="COLOR: #000000">    </span>
						<span style="COLOR: #0000ff">Select</span>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            </font>
				</span>
				<span style="COLOR: #808080">
						<font size="2">*</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font size="2">        </font>
				</span>
				<span style="COLOR: #0000ff">
						<font size="2">From</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            tbContractRent </font>
				</span>
				<font size="2">
						<font color="#ffff00">
								<strong>
										<span style="COLOR: #ff00ff">Left</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">Join</span>
								</strong>
						</font>
						<span style="COLOR: #000000"> tbContractOwner </span>
						<span style="COLOR: #0000ff">On</span>
						<span style="COLOR: #000000"> tbContractRent.CContractId</span>
						<span style="COLOR: #808080">=</span>
				</font>
				<font size="2">
						<span style="COLOR: #000000">tbContractOwner.CContractId<br />            </span>
						<span style="COLOR: #0000ff">Join</span>
						<span style="COLOR: #000000"> tbOwner </span>
						<span style="COLOR: #0000ff">On</span>
						<span style="COLOR: #000000"> tbContractOwner.COwnerId</span>
						<span style="COLOR: #808080">=</span>
				</font>
				<font size="2">
						<span style="COLOR: #000000">tbOwner.COwnerId<br />        </span>
						<span style="COLOR: #0000ff">Where</span>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            tbContractRent.CDelFlag</font>
				</span>
				<font size="2">
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #ff0000">'</span>
						<span style="COLOR: #ff0000">N</span>
						<span style="COLOR: #ff0000">'</span>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            </font>
				</span>
				<font size="2">
						<span style="COLOR: #808080">And</span>
						<span style="COLOR: #000000"> tbContractOwner.CDelFlag</span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #ff0000">'</span>
						<span style="COLOR: #ff0000">N</span>
						<span style="COLOR: #ff0000">'</span>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            </font>
				</span>
				<font size="2">
						<span style="COLOR: #808080">And</span>
						<span style="COLOR: #000000"> tbOwner.CDelFlag</span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #ff0000">'</span>
						<span style="COLOR: #ff0000">N</span>
						<span style="COLOR: #ff0000">'</span>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            </font>
				</span>
				<font size="2">
						<span style="COLOR: #808080">And</span>
						<span style="COLOR: #000000"> tbContractRent.CEndDate </span>
						<span style="COLOR: #808080">Between</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@StartDate</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">And</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@EndDate</span>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            </font>
				</span>
				<font size="2">
						<span style="COLOR: #808080">And</span>
						<span style="COLOR: #000000"> tbContractRent.CPlaceCode </span>
						<span style="COLOR: #808080">Like</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #ff0000">'</span>
						<span style="COLOR: #ff0000">%@PlaceCode%</span>
						<span style="COLOR: #ff0000">'</span>
				</font>
				<span style="COLOR: #000000">
						<br />
				</span>
		</div>
		<font size="2">         上面SQL的意圖很明顯，是想將合約左聯中間表，再關聯到房東。但是改造的結果是失敗，失敗的原因很明顯，就是3表是逐步關聯起來的，只將合約與中間表實現了左聯，但<strong>與房東表的關聯卻沒有改動</strong>，現在重新改動一下。<br /></font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font size="2">
						<span style="COLOR: #008080">--</span>
						<span style="COLOR: #008080">陷阱2：</span>
				</font>
				<span style="COLOR: #008080">
						<br />
				</span>
				<font size="2">
						<span style="COLOR: #000000">    </span>
						<span style="COLOR: #0000ff">Select</span>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            </font>
				</span>
				<span style="COLOR: #808080">
						<font size="2">*</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font size="2">        </font>
				</span>
				<span style="COLOR: #0000ff">
						<font size="2">From</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            tbContractRent </font>
				</span>
				<font size="2">
						<span style="COLOR: #ff00ff">Left</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">Join</span>
						<span style="COLOR: #000000"> tbContractOwner </span>
						<span style="COLOR: #0000ff">On</span>
						<span style="COLOR: #000000"> tbContractRent.CContractId</span>
						<span style="COLOR: #808080">=</span>
				</font>
				<font size="2">
						<span style="COLOR: #000000">tbContractOwner.CContractId<br />            </span>
						<strong>
								<span style="COLOR: #ff00ff">Left</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">Join</span>
						</strong>
						<span style="COLOR: #000000"> tbOwner </span>
						<span style="COLOR: #0000ff">On</span>
						<span style="COLOR: #000000"> tbContractOwner.COwnerId</span>
						<span style="COLOR: #808080">=</span>
				</font>
				<font size="2">
						<span style="COLOR: #000000">tbOwner.COwnerId<br />        </span>
						<span style="COLOR: #0000ff">Where</span>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            tbContractRent.CDelFlag</font>
				</span>
				<font size="2">
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #ff0000">'</span>
						<span style="COLOR: #ff0000">N</span>
						<span style="COLOR: #ff0000">'</span>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            </font>
				</span>
				<font size="2">
						<span style="COLOR: #808080">And</span>
						<span style="COLOR: #000000"> tbContractOwner.CDelFlag</span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #ff0000">'</span>
						<span style="COLOR: #ff0000">N</span>
						<span style="COLOR: #ff0000">'</span>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            </font>
				</span>
				<font size="2">
						<span style="COLOR: #808080">And</span>
						<span style="COLOR: #000000"> tbOwner.CDelFlag</span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #ff0000">'</span>
						<span style="COLOR: #ff0000">N</span>
						<span style="COLOR: #ff0000">'</span>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            </font>
				</span>
				<font size="2">
						<span style="COLOR: #808080">And</span>
						<span style="COLOR: #000000"> tbContractRent.CEndDate </span>
						<span style="COLOR: #808080">Between</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@StartDate</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">And</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@EndDate</span>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            </font>
				</span>
				<font size="2">
						<span style="COLOR: #808080">And</span>
						<span style="COLOR: #000000"> tbContractRent.CPlaceCode </span>
						<span style="COLOR: #808080">Like</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #ff0000">'</span>
						<span style="COLOR: #ff0000">%@PlaceCode%</span>
						<span style="COLOR: #ff0000">'</span>
				</font>
				<span style="COLOR: #000000">
						<br />
				</span>
		</div>
		<font size="2">         這一次已經將3張表的關聯都改成了外聯，可是還是失敗，因為你無論怎麼查詢都會發現它與第1條SQL的內聯是等效的，為什麼會這樣呢？原因很簡單，在SQL語法中 <strong>Join 是連接關係，它會在 Where 子句之前完成</strong>，因此這條SQL的工作鎏程變成這樣：<br />         a.三表之間外聯 --&gt; b.Where<br />         步驟a形成的外聯結果集是包含全部的 tbContractRent 記錄的，未連接上的 tbContractOwner，tbOwner 表記錄的全部字段的值都會是 Null ，再從這個記錄上選取 tbOwner.CDelFalg='N'...，無疑就將這些 Null 值的記錄給排除掉了，<strong>產生了與 Inner Join 等效的結果</strong>。<br />         原因說清楚，就好解決。首先看 CDelFlag = 'N'，這個條件，它在邏輯中屬於“前置”條件，應該在連接表時進行。<br />    <strong>解決方案</strong>：<br /></font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font size="2">
						<span style="COLOR: #000000">    </span>
						<span style="COLOR: #0000ff">Select</span>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            </font>
				</span>
				<span style="COLOR: #808080">
						<font size="2">*</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font size="2">        </font>
				</span>
				<span style="COLOR: #0000ff">
						<font size="2">From</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            tbContractRent </font>
				</span>
				<font size="2">
						<span style="COLOR: #ff00ff">Left</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">Join</span>
						<span style="COLOR: #000000"> tbContractOwner </span>
						<span style="COLOR: #0000ff">On</span>
						<span style="COLOR: #000000"> tbContractRent.CContractId</span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #000000">tbContractOwner.CContractId </span>
						<strong>
								<span style="COLOR: #808080">And</span>
								<span style="COLOR: #000000"> tbContractOwner.CDelFlag</span>
								<span style="COLOR: #808080">=</span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #ff0000">N</span>
								<span style="COLOR: #ff0000">'</span>
						</strong>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            </font>
				</span>
				<font size="2">
						<span style="COLOR: #ff00ff">Left</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">Join</span>
						<span style="COLOR: #000000"> tbOwner </span>
						<span style="COLOR: #0000ff">On</span>
						<span style="COLOR: #000000"> tbContractOwner.COwnerId</span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #000000">tbOwner.COwnerId </span>
						<strong>
								<span style="COLOR: #808080">And</span>
								<span style="COLOR: #000000"> tbOwner.CDelFlag</span>
								<span style="COLOR: #808080">=</span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #ff0000">N</span>
								<span style="COLOR: #ff0000">'</span>
						</strong>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">        </font>
				</span>
				<span style="COLOR: #0000ff">
						<font size="2">Where</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            tbContractRent.CDelFlag</font>
				</span>
				<font size="2">
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #ff0000">'</span>
						<span style="COLOR: #ff0000">N</span>
						<span style="COLOR: #ff0000">'</span>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            </font>
				</span>
				<font size="2">
						<span style="COLOR: #808080">And</span>
						<span style="COLOR: #000000"> tbContractRent.CEndDate </span>
						<span style="COLOR: #808080">Between</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@StartDate</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">And</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@EndDate</span>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            </font>
				</span>
				<font size="2">
						<span style="COLOR: #808080">And</span>
						<span style="COLOR: #000000"> tbContractRent.CPlaceCode </span>
						<span style="COLOR: #808080">Like</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #ff0000">'</span>
						<span style="COLOR: #ff0000">%@PlaceCode%</span>
						<span style="COLOR: #ff0000">'</span>
				</font>
				<span style="COLOR: #000000">
						<br />
				</span>
		</div>
		<font size="2">      說明：由於合約表是關聯的入口表(即 Left 的左表)，因此它的 CDelFlag='N' 可以放在後部實現篩選。<br /><br /><br /><strong>示例2：</strong><br />      邏輯：<br />         搜尋異常數據，查找有合約卻沒有對應房東的記錄，前置條件是該合約所在場所的 CIsUse='N'<br />   <strong>DML：</strong><br /></font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font size="2">
						<span style="COLOR: #008080">--</span>
						<span style="COLOR: #008080">陷阱1：</span>
				</font>
				<span style="COLOR: #008080">
						<br />
				</span>
				<font size="2">
						<span style="COLOR: #000000">    </span>
						<span style="COLOR: #0000ff">Select</span>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            tbContractRent.</font>
				</span>
				<span style="COLOR: #808080">
						<font size="2">*</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font size="2">        </font>
				</span>
				<span style="COLOR: #0000ff">
						<font size="2">From</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            tbContractRent </font>
				</span>
				<font size="2">
						<span style="COLOR: #ff00ff">Left</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">Join</span>
						<span style="COLOR: #000000"> tbPlace </span>
						<span style="COLOR: #0000ff">on</span>
						<span style="COLOR: #000000"> tbContractRent.CPlaceCode</span>
						<span style="COLOR: #808080">=</span>
				</font>
				<font size="2">
						<span style="COLOR: #000000">tbPlace.CPlaceCode<br />            </span>
						<span style="COLOR: #ff00ff">Left</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">Join</span>
						<span style="COLOR: #000000"> tbContractOwner </span>
						<span style="COLOR: #0000ff">on</span>
						<span style="COLOR: #000000"> tbContractRent.CContractid</span>
						<span style="COLOR: #808080">=</span>
				</font>
				<font size="2">
						<span style="COLOR: #000000">tbContractOwner.CContractid<br />            </span>
						<span style="COLOR: #ff00ff">Left</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">Join</span>
						<span style="COLOR: #000000"> tbOwner </span>
						<span style="COLOR: #0000ff">on</span>
						<span style="COLOR: #000000"> tbContractOwner.COwnerid</span>
						<span style="COLOR: #808080">=</span>
				</font>
				<font size="2">
						<span style="COLOR: #000000">tbOwner.COwnerid<br />        </span>
						<span style="COLOR: #0000ff">Where</span>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            tbPlace.CIsUse</font>
				</span>
				<font size="2">
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #ff0000">'</span>
						<span style="COLOR: #ff0000">N</span>
						<span style="COLOR: #ff0000">'</span>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            </font>
				</span>
				<font size="2">
						<span style="COLOR: #808080">And</span>
						<span style="COLOR: #000000"> tbContractOwner.CContractid </span>
						<span style="COLOR: #0000ff">is</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">null</span>
				</font>
				<span style="COLOR: #000000">
						<br />
				</span>
		</div>
		<font size="2">         結果是失敗的，它並沒有按照設定的邏輯來正常工作。參照示例1中的解釋，問題應該是出在 tbPlace.CIsUse='N' 這個Where條件上，該條件將需要搜尋的數據全部給過濾出去了，那麼按照示例1的方法將該條件前置能夠解決問題嗎？<br /></font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font size="2">
						<span style="COLOR: #008080">--</span>
						<span style="COLOR: #008080">陷阱2：</span>
				</font>
				<span style="COLOR: #008080">
						<br />
				</span>
				<font size="2">
						<span style="COLOR: #000000">    </span>
						<span style="COLOR: #0000ff">Select</span>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            tbContractRent.</font>
				</span>
				<span style="COLOR: #808080">
						<font size="2">*</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font size="2">        </font>
				</span>
				<span style="COLOR: #0000ff">
						<font size="2">From</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            tbContractRent </font>
				</span>
				<font size="2">
						<span style="COLOR: #ff00ff">Left</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">Join</span>
						<span style="COLOR: #000000"> tbPlace </span>
						<span style="COLOR: #0000ff">on</span>
						<span style="COLOR: #000000"> tbContractRent.CPlaceCode</span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #000000">tbPlace.CPlaceCode </span>
						<strong>
								<span style="COLOR: #808080">And</span>
								<span style="COLOR: #000000"> tbPlace.CIsUse</span>
								<span style="COLOR: #808080">=</span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #ff0000">N</span>
								<span style="COLOR: #ff0000">'</span>
						</strong>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            </font>
				</span>
				<font size="2">
						<span style="COLOR: #ff00ff">Left</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">Join</span>
						<span style="COLOR: #000000"> tbContractOwner </span>
						<span style="COLOR: #0000ff">on</span>
						<span style="COLOR: #000000"> tbContractRent.CContractid</span>
						<span style="COLOR: #808080">=</span>
				</font>
				<font size="2">
						<span style="COLOR: #000000">tbContractOwner.CContractid<br />            </span>
						<span style="COLOR: #ff00ff">Left</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">Join</span>
						<span style="COLOR: #000000"> tbOwner </span>
						<span style="COLOR: #0000ff">on</span>
						<span style="COLOR: #000000"> tbContractOwner.COwnerid</span>
						<span style="COLOR: #808080">=</span>
				</font>
				<font size="2">
						<span style="COLOR: #000000">tbOwner.COwnerid<br />        </span>
						<span style="COLOR: #0000ff">Where</span>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            tbContractOwner.CContractid </font>
				</span>
				<font size="2">
						<span style="COLOR: #0000ff">is</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">null</span>
				</font>
				<span style="COLOR: #000000">
						<br />
				</span>
		</div>
		<font size="2">         條件前置，這是前例中的解決方案，但它不是萬能的，需要依據邏輯來判斷是否合適。在該示例中如果將 tbPlace.CIsUse='N' 條件前置，雖然可以先排除掉不符合該條件的 tbPlace 記錄，但是 Left Join 關鍵字卻會將它們重新給拉回來，只不過拉回來的結果是除了合約表的記錄外，其它字段全部為 Null 。正好完全符合下面的 tbContractOwner.CContractid is null 條件，很有欺騙性吧。<br />    <strong>解決方案</strong>：<br /></font>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font size="2">
						<span style="COLOR: #000000">    </span>
						<span style="COLOR: #0000ff">Select</span>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            tbContractRent.</font>
				</span>
				<span style="COLOR: #808080">
						<font size="2">*</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font size="2">        </font>
				</span>
				<span style="COLOR: #0000ff">
						<font size="2">From</font>
				</span>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            tbContractRent </font>
				</span>
				<font size="2">
						<span style="COLOR: #ff00ff">Left</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">Join</span>
						<span style="COLOR: #000000"> tbPlace </span>
						<span style="COLOR: #0000ff">on</span>
						<span style="COLOR: #000000"> tbContractRent.CPlaceCode</span>
						<span style="COLOR: #808080">=</span>
				</font>
				<font size="2">
						<span style="COLOR: #000000">tbPlace.CPlaceCode<br />            </span>
						<span style="COLOR: #ff00ff">Left</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">Join</span>
						<span style="COLOR: #000000"> tbContractOwner </span>
						<span style="COLOR: #0000ff">on</span>
						<span style="COLOR: #000000"> tbContractRent.CContractid</span>
						<span style="COLOR: #808080">=</span>
				</font>
				<font size="2">
						<span style="COLOR: #000000">tbContractOwner.CContractid<br />            </span>
						<span style="COLOR: #ff00ff">Left</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">Join</span>
						<span style="COLOR: #000000"> tbOwner </span>
						<span style="COLOR: #0000ff">on</span>
						<span style="COLOR: #000000"> tbContractOwner.COwnerid</span>
						<span style="COLOR: #808080">=</span>
				</font>
				<font size="2">
						<span style="COLOR: #000000">tbOwner.COwnerid<br />        </span>
						<span style="COLOR: #0000ff">Where</span>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            tbContractOwner.CContractid </font>
				</span>
				<font size="2">
						<span style="COLOR: #0000ff">is</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">null</span>
				</font>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            </font>
				</span>
				<strong>
						<font size="2">
								<span style="COLOR: #808080">And</span>
								<span style="COLOR: #000000"> (tbPlace.CIsUse</span>
								<span style="COLOR: #808080">=</span>
								<span style="COLOR: #ff0000">'</span>
								<span style="COLOR: #ff0000">N</span>
								<span style="COLOR: #ff0000">'</span>
						</font>
				</strong>
				<span style="COLOR: #000000">
						<br />
						<font size="2">                </font>
				</span>
				<strong>
						<font size="2">
								<span style="COLOR: #808080">OR</span>
								<span style="COLOR: #000000"> tbPlace.CPlaceCode </span>
								<span style="COLOR: #0000ff">is</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">null</span>
						</font>
				</strong>
				<span style="COLOR: #000000">
						<br />
						<font size="2">            <strong>)</strong><br /></font>
				</span>
		</div>
		<font size="2">         這個有一點點復雜：根據邏輯 tbPlace.CIsUse='N' 這個條件是後置條件，應該放在後部，但是僅僅這樣處理就會產生陷阱1的效果，對應於合約的 Left Join 關聯，需要加入 OR tbPlace.CPlaceCode is null 來維護外聯的結果集。<br /></font>
<img src ="http://www.blogjava.net/tw-ddm/aggbug/94905.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/tw-ddm/" target="_blank">大大毛</a> 2007-01-19 12:47 <a href="http://www.blogjava.net/tw-ddm/articles/94905.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>自行構建 Identity 列的方法</title><link>http://www.blogjava.net/tw-ddm/articles/93929.html</link><dc:creator>大大毛</dc:creator><author>大大毛</author><pubDate>Mon, 15 Jan 2007 04:51:00 GMT</pubDate><guid>http://www.blogjava.net/tw-ddm/articles/93929.html</guid><wfw:comment>http://www.blogjava.net/tw-ddm/comments/93929.html</wfw:comment><comments>http://www.blogjava.net/tw-ddm/articles/93929.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/tw-ddm/comments/commentRss/93929.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/tw-ddm/services/trackbacks/93929.html</trackback:ping><description><![CDATA[
		<p>
				<font size="2">   <strong>問題</strong>：<br />      SQL查詢中經常需要借助於表的 primary key 來實現記錄的唯一性標識，但是有的場合中卻不方便對主鍵進行直接處理，例如我遇到的這種情況：</font>
		</p>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<span style="COLOR: #0000ff">create</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">table</span>
				<span style="COLOR: #000000"> A(</span>
				<span style="FONT-WEIGHT: bold; COLOR: #000000">char</span>
				<span style="COLOR: #000000">(</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">500</span>
				<span style="COLOR: #000000">) pkCol,col1,col2);</span>
		</div>
		<p>      表A具有PK列，我現在需要在頁面上Show出以 CheckBox 表示的記錄條，類似於郵件列表的選取。<br />      通常的做法會是利用表A的PK列來形成 頁面元素 的ID，這樣在FORM的Submit中就可以拿到選取的記錄PK值，可以很方便的組織SQL來完成整個篩選邏輯。<br />       現在的<strong>問題是出在這個表A的PK列上</strong>，通常表的PK列會利用 Int 類型來建立，這樣建立唯一索引的代價會比較小，用Char來建立PK時的情況也是很多，但是也會保證該列的規則性。可是表A的PK列是建立在一個具有實際含義的Char列上，該列的內容會是用戶手工輸入的結果，可想而知如果用它來構造頁面元素的ID會發生什麼情況：<br />        1.如果該列寬很大的話，當然就會出錯;<br />        2.由於該列內容不會是非常“正規”的內容，很有可能包含一些特殊字符，這樣一定會使瀏覽器在解析頁面時產生歧義。<br /><br />   <strong>解決方法</strong>：<br />       問題2的情況可以通過對表的PK列進行字符替換等手段來排除掉特殊字符，但是問題1的長度過長依舊不能夠解決。<br />       我想到的解決該問題的方法是：<br />      1.對PK列進行字符替換處理這樣可以被保存在 textarea 這樣的元素裡;<br />      2.而需要提交的元素ID則由我以SQL構造的 Identity 列來構造;<br />      3.生成頁面的時候只需要建立起提交元素與 textarea 的關聯即可，最終提交的還是PK的值。<br />      可能有人會對解決方法3提出疑問，既然可以構造 Identity 列，為啥不直接用它來實現提交呢？<br />      看過實現就知道了：</p>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<span style="COLOR: #000000">    </span>
				<span style="COLOR: #0000ff">Select</span>
				<span style="COLOR: #000000">
						<br />            MyIDCol</span>
				<span style="COLOR: #808080">=</span>
				<span style="COLOR: #000000">(<br />                </span>
				<span style="COLOR: #0000ff">Select</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #ff00ff">Count</span>
				<span style="COLOR: #000000">(</span>
				<span style="COLOR: #808080">*</span>
				<span style="COLOR: #000000">) </span>
				<span style="COLOR: #0000ff">From</span>
				<span style="COLOR: #000000"> A subA </span>
				<span style="COLOR: #0000ff">Where</span>
				<span style="COLOR: #000000"> subA.pkCol</span>
				<span style="COLOR: #808080">=</span>
				<span style="COLOR: #000000">A.pkCol </span>
				<span style="COLOR: #808080">And</span>
				<span style="COLOR: #000000"> A.pkCol </span>
				<span style="COLOR: #808080">&lt;</span>
				<span style="COLOR: #000000"> subA.pkCol<br />            ),<br />            pkCol,<br />            col1,<br />            col2<br />        </span>
				<span style="COLOR: #0000ff">From</span>
				<span style="COLOR: #000000">
						<br />            A<br /></span>
		</div>
		<p>      這裡可以看 Identity 列(MyIDCol) 是構建在唯一查詢的排序結果集上的，因此它在工作時<strong>總能夠保證唯一的區分</strong>每一行記錄。<br />      但是需要注意的一點就是這條語句形成的 <strong>Identity 是關聯到某一時刻</strong>的，換句話說，如果在用戶對記錄進行過 Insert/Delete 操作之後，這個Identity的值與做DML之前的記錄是關聯不上的，因此需要在Select時帶出PK列的值，在提交時使用PK值才能保證數據的正確性。<br /><br />      當然，改變DB的設計也是可行的，只不過前提是必須在DB的設計階段。</p>
<img src ="http://www.blogjava.net/tw-ddm/aggbug/93929.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/tw-ddm/" target="_blank">大大毛</a> 2007-01-15 12:51 <a href="http://www.blogjava.net/tw-ddm/articles/93929.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>複雜SQL語句的構建以及內部的封裝調試</title><link>http://www.blogjava.net/tw-ddm/articles/91341.html</link><dc:creator>大大毛</dc:creator><author>大大毛</author><pubDate>Tue, 02 Jan 2007 00:54:00 GMT</pubDate><guid>http://www.blogjava.net/tw-ddm/articles/91341.html</guid><wfw:comment>http://www.blogjava.net/tw-ddm/comments/91341.html</wfw:comment><comments>http://www.blogjava.net/tw-ddm/articles/91341.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/tw-ddm/comments/commentRss/91341.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/tw-ddm/services/trackbacks/91341.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 前言：      這段時間還一直在努力之中，這一次在SQL的構建上遇到一個挑戰，不過這一次的挑戰是自找的。      開發環境是VB6的DLL＋ASP，分到的活是一個報表模塊。      需求         我這一塊的報表，現階段是要實現兩個功能(就我看應該只是一個，需求理解錯誤可不是我的事兒)，為了與其它的模塊保持一致，工作流程如下：         1.輸入查詢的年 / 月;        ...&nbsp;&nbsp;<a href='http://www.blogjava.net/tw-ddm/articles/91341.html'>阅读全文</a><img src ="http://www.blogjava.net/tw-ddm/aggbug/91341.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/tw-ddm/" target="_blank">大大毛</a> 2007-01-02 08:54 <a href="http://www.blogjava.net/tw-ddm/articles/91341.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SQLServer數據庫結構的獲取</title><link>http://www.blogjava.net/tw-ddm/articles/82438.html</link><dc:creator>大大毛</dc:creator><author>大大毛</author><pubDate>Tue, 21 Nov 2006 14:54:00 GMT</pubDate><guid>http://www.blogjava.net/tw-ddm/articles/82438.html</guid><wfw:comment>http://www.blogjava.net/tw-ddm/comments/82438.html</wfw:comment><comments>http://www.blogjava.net/tw-ddm/articles/82438.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/tw-ddm/comments/commentRss/82438.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/tw-ddm/services/trackbacks/82438.html</trackback:ping><description><![CDATA[
		<font size="2">   這段時間好不容易找著一份臨時工，工作難找，只有苦干了。由於現在的項目需要通過VPN來連接遠程的網絡，工作中經常需要通過遠程桌面的方式來查看遠程主機上的SQLServer系統的庫/表及數據集的特征和結構，但是同時連接的數量卻是有限，沒有辦法只能自行使用Select语句以獲取自己所需的資料了。<br /><br />      日常的需求是：<br />      1.知道該DBMS上存在有哪些數據庫<br />      2.知道具體的庫上存在有哪些用戶表<br />      3.知道表的字段結構，類型，寬度以及Select順序<br />      4.獲取表中的特征數據<br /><br />      而要從SQLServer的DBMS中獲取以上資料非常容易，只要具有權限，通過簡單的SQL語句即可實現。<br /><br />      <strong>1.獲取DBMS上存在有哪些數據庫<br /></strong><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"><br />        name,<br />        dbid<br />    </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"><br />        master.dbo.sysdatabases<br />    </span><span style="COLOR: #0000ff">WHERE</span><span style="COLOR: #000000"><br />        name </span><span style="COLOR: #808080">not</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">in</span><span style="COLOR: #000000"> (</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">master</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000">,</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">tempdb</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000">,</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">model</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000">,</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">msdb</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000">)</span></div><br />      <strong>2.獲取具體的庫上存在有哪些用戶表<br /></strong><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"><br />        name,<br />        id<br />    </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #ff0000">[</span><span style="COLOR: #ff0000">ddmDataBaseName</span><span style="COLOR: #ff0000">]</span><span style="COLOR: #000000">.dbo.sysobjects<br />    </span><span style="COLOR: #0000ff">where</span><span style="COLOR: #000000"><br />        xtype </span><span style="COLOR: #808080">=</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">U</span><span style="COLOR: #ff0000">'</span></div>         這條SQL語句需要傳遞一個參數(數據庫的名稱)以替換 [ddmDataBaseName] 參數。<br /><br />      <strong>3.獲取表的字段結構，類型，寬度以及Select順序<br /></strong><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"><br />        col.name colName,<br />        type_Name(col.xtype) colType,<br />        col.length colWidth,<br />        </span><span style="COLOR: #ff00ff">case</span><span style="COLOR: #000000"> isnullable </span><span style="COLOR: #0000ff">when</span><span style="COLOR: #000000"> </span><span style="FONT-WEIGHT: bold; COLOR: #800000">1</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">then</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">允許</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">禁止</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">end</span><span style="COLOR: #000000">  nullable<br />    </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #ff0000">[</span><span style="COLOR: #ff0000">ddmDataBaseName</span><span style="COLOR: #ff0000">]</span><span style="COLOR: #000000">.dbo.syscolumns col </span><span style="COLOR: #0000ff">join</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff0000">[</span><span style="COLOR: #ff0000">ddmDataBaseName</span><span style="COLOR: #ff0000">]</span><span style="COLOR: #000000">.dbo.sysobjects tab </span><span style="COLOR: #0000ff">on</span><span style="COLOR: #000000"> col.id </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> tab.id<br />    </span><span style="COLOR: #0000ff">where</span><span style="COLOR: #000000"><br />        tab.xtype </span><span style="COLOR: #808080">=</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">U</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #808080">and</span><span style="COLOR: #000000"> tab.name</span><span style="COLOR: #808080">=</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">[ddmTableName]</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">order</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">by</span><span style="COLOR: #000000"><br />        tab.id,<br />        col.colorder</span></div>         同樣的，這條SQL語句需要傳遞兩個參數：<br />         [ddmDataBaseName]:數據庫名稱<br />         [ddmTableName]:表名稱<br /><br />      <strong>4.獲取表的特征數據集<br /></strong><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #0000ff">top</span><span style="COLOR: #000000"> </span><span style="FONT-WEIGHT: bold; COLOR: #800000">200</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">*</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #ff0000">[</span><span style="COLOR: #ff0000">ddmDataBaseName</span><span style="COLOR: #ff0000">]</span><span style="COLOR: #000000">.dbo.</span><span style="COLOR: #ff0000">[</span><span style="COLOR: #ff0000">ddmTableName</span><span style="COLOR: #ff0000">]</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">Order</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">by</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #ff00ff">NewID</span><span style="COLOR: #000000">()</span></div>         由于表的记录条数可能会很大，通常并不需要全部Select出来，因此使用了 Top 200 的关键字，而为了能够随机抽取数据，这里使用了 Order by NewID() 。<br />         通过使用 3 + 4 的组合可以很容易地拼装出一张表格出来，以便在日常工作中使用。<br /><br />      由于现在工作的系统是 繁体系统，上班时又封网，搞到文章中显得简繁一体了，呵呵。<br /></font>
<img src ="http://www.blogjava.net/tw-ddm/aggbug/82438.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/tw-ddm/" target="_blank">大大毛</a> 2006-11-21 22:54 <a href="http://www.blogjava.net/tw-ddm/articles/82438.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>树型结构的处理</title><link>http://www.blogjava.net/tw-ddm/articles/75209.html</link><dc:creator>大大毛</dc:creator><author>大大毛</author><pubDate>Sat, 14 Oct 2006 19:16:00 GMT</pubDate><guid>http://www.blogjava.net/tw-ddm/articles/75209.html</guid><wfw:comment>http://www.blogjava.net/tw-ddm/comments/75209.html</wfw:comment><comments>http://www.blogjava.net/tw-ddm/articles/75209.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/tw-ddm/comments/commentRss/75209.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/tw-ddm/services/trackbacks/75209.html</trackback:ping><description><![CDATA[
		<font size="2">   树型结构在应用中被广泛的运用，在数据操作上需要一定的技巧来进行处理：<br /><br />   在表结构的定义上，有多种方式可以采用。<br />  <strong>.数据表：</strong><br />      a.单向链表式的结构 ( 这是藕给它起的名称，因为它与链表的结构相似，除了必要的自身描述外，就只包含父节点的位置)<br />         DDL示例：<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080">--</span><span style="COLOR: #008080">相当于单向链表</span><span style="COLOR: #008080"><br /></span><span style="COLOR: #0000ff">create</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">table</span><span style="COLOR: #000000"> linkTree (<br />    id </span><span style="FONT-WEIGHT: bold; COLOR: #000000">varchar</span><span style="COLOR: #000000">(</span><span style="FONT-WEIGHT: bold; COLOR: #800000">10</span><span style="COLOR: #000000">) </span><span style="COLOR: #0000ff">primary</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">key</span><span style="COLOR: #000000">,         </span><span style="COLOR: #008080">--</span><span style="COLOR: #008080">节点自身ID</span><span style="COLOR: #008080"><br /></span><span style="COLOR: #000000">    nodeContent </span><span style="FONT-WEIGHT: bold; COLOR: #000000">varchar</span><span style="COLOR: #000000">(</span><span style="FONT-WEIGHT: bold; COLOR: #800000">50</span><span style="COLOR: #000000">),          </span><span style="COLOR: #008080">--</span><span style="COLOR: #008080">节点内容</span><span style="COLOR: #008080"><br /></span><span style="COLOR: #000000">    parentId </span><span style="FONT-WEIGHT: bold; COLOR: #000000">varchar</span><span style="COLOR: #000000">(</span><span style="FONT-WEIGHT: bold; COLOR: #800000">10</span><span style="COLOR: #000000">) </span><span style="COLOR: #0000ff">default</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff0000">''</span><span style="COLOR: #000000">    </span><span style="COLOR: #008080">--</span><span style="COLOR: #008080">父节点ID</span><span style="COLOR: #008080"><br /></span><span style="COLOR: #000000">)</span></div><br />         <strong>优点</strong>：<br />            .列宽固定，表结构易维护；<br />            .查找指定节点的上/下游节点效率高；<br />            .维护树型结构方便(增/删层)；<br />         <strong>缺点</strong>，如在下列情况中，都需要多次Select完成：<br />            .从指定节点开始列出全部子树 ( 除 Oracle 提供支持外 )；<br />            .返回从指定节点A到其子节点B的路径 ( B 与 A 的层数相差会大于 1 )；<br /><br />         DML示例：<br />            向下查找及统计子节点(仅一层)<br />            (1)通过父节点的ID<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img id="Code_Closed_Image_022624" onclick="this.style.display='none'; Code_Closed_Text_022624.style.display='none'; Code_Open_Image_022624.style.display='inline'; Code_Open_Text_022624.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" /><img id="Code_Open_Image_022624" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_022624.style.display='none'; Code_Closed_Image_022624.style.display='inline'; Code_Closed_Text_022624.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" /><span id="Code_Closed_Text_022624" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">通过父节点的ID</span><span id="Code_Open_Text_022624" style="DISPLAY: none"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: #008080">--</span><span style="COLOR: #008080">查找子节点</span><span style="COLOR: #008080"><br /></span><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #808080">*</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"><br />        linkTree<br />    </span><span style="COLOR: #0000ff">where</span><span style="COLOR: #000000"><br />        parentId </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">01</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #008080">--</span><span style="COLOR: #008080">统计子节点数量</span><span style="COLOR: #008080"><br /></span><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"><br />        parentId,<br />        </span><span style="COLOR: #ff00ff">count</span><span style="COLOR: #000000">(</span><span style="COLOR: #808080">*</span><span style="COLOR: #000000">) </span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">count</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"><br />        linkTree<br />    </span><span style="COLOR: #0000ff">where</span><span style="COLOR: #000000"><br />        parentId </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">01</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">group</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">by</span><span style="COLOR: #000000"><br />        parentId</span></span></div><br />            (2)通过父节点的内容<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img id="Code_Closed_Image_023031" onclick="this.style.display='none'; Code_Closed_Text_023031.style.display='none'; Code_Open_Image_023031.style.display='inline'; Code_Open_Text_023031.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" /><img id="Code_Open_Image_023031" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_023031.style.display='none'; Code_Closed_Image_023031.style.display='inline'; Code_Closed_Text_023031.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" /><span id="Code_Closed_Text_023031" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">条件列nodeContent上存在唯一性约束</span><span id="Code_Open_Text_023031" style="DISPLAY: none"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: #008080">--</span><span style="COLOR: #008080">查找子节点</span><span style="COLOR: #008080"><br /></span><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #808080">*</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"><br />        linkTree<br />    </span><span style="COLOR: #0000ff">where</span><span style="COLOR: #000000"><br />        parentId </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> (</span><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"> id </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"> linkTree </span><span style="COLOR: #0000ff">where</span><span style="COLOR: #000000"> nodeContent </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">Root</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000">)<br /></span><span style="COLOR: #008080">--</span><span style="COLOR: #008080">统计子节点数量</span><span style="COLOR: #008080"><br /></span><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"><br />        parentId,<br />        </span><span style="COLOR: #ff00ff">count</span><span style="COLOR: #000000">(</span><span style="COLOR: #808080">*</span><span style="COLOR: #000000">) </span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">count</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"><br />        linkTree<br />    </span><span style="COLOR: #0000ff">where</span><span style="COLOR: #000000"><br />        parentId </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> (</span><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"> id </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"> linkTree </span><span style="COLOR: #0000ff">where</span><span style="COLOR: #000000"> nodeContent </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">Root</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000">)<br />    </span><span style="COLOR: #0000ff">group</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">by</span><span style="COLOR: #000000"><br />        parentId</span></span></div><br />               由于条件列无唯一性约束，所以在查找时有可能会返回一个记录集，需要做一些处理： 
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img id="Code_Closed_Image_024424" onclick="this.style.display='none'; Code_Closed_Text_024424.style.display='none'; Code_Open_Image_024424.style.display='inline'; Code_Open_Text_024424.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" /><img id="Code_Open_Image_024424" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_024424.style.display='none'; Code_Closed_Image_024424.style.display='inline'; Code_Closed_Text_024424.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" /><span id="Code_Closed_Text_024424" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">条件列nodeContent上无唯一性约束</span><span id="Code_Open_Text_024424" style="DISPLAY: none"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: #008080">--</span><span style="COLOR: #008080">查找子节点,此处仅返回具有子节点的父节点行,如果需要返回全部则用right join</span><span style="COLOR: #008080"><br /></span><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"><br />        sub.id parentId,<br />        main.id,<br />        main.nodeContent<br />    </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"><br />        linkTree main<br />        </span><span style="COLOR: #0000ff">join</span><span style="COLOR: #000000"> (</span><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"> id </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"> linkTree </span><span style="COLOR: #0000ff">where</span><span style="COLOR: #000000"> nodeContent </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">depart1</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000">) sub </span><span style="COLOR: #0000ff">on</span><span style="COLOR: #000000"> main.parentId </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> sub.id<br />    </span><span style="COLOR: #0000ff">order</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">by</span><span style="COLOR: #000000"><br />        parentId<br /></span><span style="COLOR: #008080">--</span><span style="COLOR: #008080">统计子节点数量,使用right join返回全部统计数</span><span style="COLOR: #008080"><br /></span><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"><br />        parentId,<br />        </span><span style="COLOR: #ff00ff">count</span><span style="COLOR: #000000">(id) </span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">count</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"> (<br />            </span><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"><br />                    sub.id parentId,<br />                    main.id <br />                </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"><br />                    linkTree main<br />                    </span><span style="COLOR: #ff00ff">right</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">join</span><span style="COLOR: #000000"> (</span><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"> id </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"> linkTree </span><span style="COLOR: #0000ff">where</span><span style="COLOR: #000000"> nodeContent </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">depart1</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000">) sub </span><span style="COLOR: #0000ff">on</span><span style="COLOR: #000000"> main.parentId </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> sub.id<br />        ) tree<br />    </span><span style="COLOR: #0000ff">group</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">by</span><span style="COLOR: #000000"><br />        parentId</span></span></div><br />            (3)查找兄弟节点<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img id="Code_Closed_Image_031427" onclick="this.style.display='none'; Code_Closed_Text_031427.style.display='none'; Code_Open_Image_031427.style.display='inline'; Code_Open_Text_031427.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" /><img id="Code_Open_Image_031427" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_031427.style.display='none'; Code_Closed_Image_031427.style.display='inline'; Code_Closed_Text_031427.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" /><span id="Code_Closed_Text_031427" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">查找兄弟节点</span><span id="Code_Open_Text_031427" style="DISPLAY: none"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: #008080">--</span><span style="COLOR: #008080">使用唯一列</span><span style="COLOR: #008080"><br /></span><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"><br />        parentId,<br />        id brotherId,<br />        nodeContent<br />    </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"><br />        linkTree<br />    </span><span style="COLOR: #0000ff">where</span><span style="COLOR: #000000"><br />        parentId </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> (</span><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"> parentId </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"> linkTree </span><span style="COLOR: #0000ff">where</span><span style="COLOR: #000000"> id</span><span style="COLOR: #808080">=</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">01</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000">)<br /></span><span style="COLOR: #008080">--</span><span style="COLOR: #008080">使用非唯一列,由于一父多子的对应关系,因此需要注意使用distinct关键字</span><span style="COLOR: #008080"><br /></span><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"><br />        sub.parentId,<br />        main.id brotherId,<br />        main.nodeContent<br />    </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"><br />        linkTree main<br />        </span><span style="COLOR: #0000ff">join</span><span style="COLOR: #000000"> (</span><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">distinct</span><span style="COLOR: #000000"> parentId </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"> linkTree </span><span style="COLOR: #0000ff">where</span><span style="COLOR: #000000"> nodeContent </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">depart1</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000">) sub </span><span style="COLOR: #0000ff">on</span><span style="COLOR: #000000"> main.parentId </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> sub.parentId<br />    </span><span style="COLOR: #0000ff">order</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">by</span><span style="COLOR: #000000"><br />        parentId</span></span></div><br />            (4)遍历树<br />            由于链表式表结构的特点，决定了无法用通用的单次 Select 完成操作，对于不同的 DBMS 可以采取不同的策略：<br />            如Oracle<br />               select * from linkTree start with id='00' connect by PRIOR  id = parentId;<br />            如Ms-SQL2000<br />               表的结构决定了数据只能逐层查找，网上常见的有使用存储过程进行递归返回表变量来实现。<br />               这里我用循环的方式来实现，效率比递归+表变量要高。<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img id="Code_Closed_Image_212702" onclick="this.style.display='none'; Code_Closed_Text_212702.style.display='none'; Code_Open_Image_212702.style.display='inline'; Code_Open_Text_212702.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" /><img id="Code_Open_Image_212702" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_212702.style.display='none'; Code_Closed_Image_212702.style.display='inline'; Code_Closed_Text_212702.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" /><span id="Code_Closed_Text_212702" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">Ms-SQL2000遍历树</span><span id="Code_Open_Text_212702" style="DISPLAY: none"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #008080">--</span><span style="COLOR: #008080">这里定义的两个参数</span><span style="COLOR: #008080"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #0000ff">declare</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #008000">@level</span><span style="COLOR: #000000"> </span><span style="FONT-WEIGHT: bold; COLOR: #000000">int</span><span style="COLOR: #000000">,            </span><span style="COLOR: #008080">--</span><span style="COLOR: #008080">查找层次</span><span style="COLOR: #008080"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #000000">    </span><span style="COLOR: #008000">@start</span><span style="COLOR: #000000"> </span><span style="FONT-WEIGHT: bold; COLOR: #000000">varchar</span><span style="COLOR: #000000">(</span><span style="FONT-WEIGHT: bold; COLOR: #800000">2</span><span style="COLOR: #000000">);    </span><span style="COLOR: #008080">--</span><span style="COLOR: #008080">开始节点ID</span><span style="COLOR: #008080"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"> </span><span style="COLOR: #008000">@level</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> </span><span style="FONT-WEIGHT: bold; COLOR: #800000">3</span><span style="COLOR: #000000">,</span><span style="COLOR: #008000">@start</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">00</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000">;    </span><span style="COLOR: #008080">--</span><span style="COLOR: #008080">示例赋参</span><span style="COLOR: #008080"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />--</span><span style="COLOR: #008080">临时变量</span><span style="COLOR: #008080"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #0000ff">declare</span><span style="COLOR: #000000">    </span><span style="COLOR: #008000">@flag</span><span style="COLOR: #000000"> </span><span style="FONT-WEIGHT: bold; COLOR: #000000">int</span><span style="COLOR: #000000">;        </span><span style="COLOR: #008080">--</span><span style="COLOR: #008080">当前处理节点的层次</span><span style="COLOR: #008080"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #0000ff">declare</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #008000">@t</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">table</span><span style="COLOR: #000000"> (<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        id </span><span style="FONT-WEIGHT: bold; COLOR: #000000">varchar</span><span style="COLOR: #000000">(</span><span style="FONT-WEIGHT: bold; COLOR: #800000">2</span><span style="COLOR: #000000">),<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        nodeContent </span><span style="FONT-WEIGHT: bold; COLOR: #000000">varchar</span><span style="COLOR: #000000">(</span><span style="FONT-WEIGHT: bold; COLOR: #800000">20</span><span style="COLOR: #000000">),<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        parentId </span><span style="FONT-WEIGHT: bold; COLOR: #000000">varchar</span><span style="COLOR: #000000">(</span><span style="FONT-WEIGHT: bold; COLOR: #800000">2</span><span style="COLOR: #000000">),<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        site </span><span style="FONT-WEIGHT: bold; COLOR: #000000">varchar</span><span style="COLOR: #000000">(</span><span style="FONT-WEIGHT: bold; COLOR: #800000">200</span><span style="COLOR: #000000">),<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        flag </span><span style="FONT-WEIGHT: bold; COLOR: #000000">int</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    );<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #008080">--</span><span style="COLOR: #008080">初始化</span><span style="COLOR: #008080"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #0000ff">set</span><span style="COLOR: #000000"> </span><span style="COLOR: #008000">@flag</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> </span><span style="FONT-WEIGHT: bold; COLOR: #800000">1</span><span style="COLOR: #000000">;<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #0000ff">insert</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">into</span><span style="COLOR: #000000"> </span><span style="COLOR: #008000">@t</span><span style="COLOR: #000000"> <br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />                </span><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />                        linkTree.</span><span style="COLOR: #808080">*</span><span style="COLOR: #000000">,<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />                        linkTree.id,<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />                        </span><span style="COLOR: #008000">@flag</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />                    </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />                        linkTree<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />                    </span><span style="COLOR: #0000ff">where</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />                        id </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #008000">@start</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #008080">--</span><span style="COLOR: #008080">循环查找,结束条件(1.达到指定查找层次;2.没有任何匹配记录;)</span><span style="COLOR: #008080"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000"> </span><span style="FONT-WEIGHT: bold; COLOR: #008000">@@rowcount</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">&gt;</span><span style="COLOR: #000000"> </span><span style="FONT-WEIGHT: bold; COLOR: #800000">0</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">and</span><span style="COLOR: #000000"> </span><span style="COLOR: #008000">@flag</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">&lt;</span><span style="COLOR: #000000"> </span><span style="COLOR: #008000">@level</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #0000ff">begin</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">set</span><span style="COLOR: #000000"> </span><span style="COLOR: #008000">@flag</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #008000">@flag</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">+</span><span style="COLOR: #000000"> </span><span style="FONT-WEIGHT: bold; COLOR: #800000">1</span><span style="COLOR: #000000">;    </span><span style="COLOR: #008080">--</span><span style="COLOR: #008080">查找层次递增</span><span style="COLOR: #008080"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #000000">    </span><span style="COLOR: #008080">--</span><span style="COLOR: #008080">向下查找匹配子节点</span><span style="COLOR: #008080"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">insert</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">into</span><span style="COLOR: #000000"> </span><span style="COLOR: #008000">@t</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />    </span><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />            linkTree.id,<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />            linkTree.nodeContent,<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />            linkTree.parentId,<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />            t.site </span><span style="COLOR: #808080">+</span><span style="COLOR: #000000"> linkTree.id,<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />            </span><span style="COLOR: #008000">@flag</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />             </span><span style="COLOR: #008000">@t</span><span style="COLOR: #000000"> t </span><span style="COLOR: #0000ff">join</span><span style="COLOR: #000000"> linkTree </span><span style="COLOR: #0000ff">on</span><span style="COLOR: #000000"> linkTree.parentId </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> t.id<br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />        </span><span style="COLOR: #0000ff">where</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" />            t.flag </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #008000">@flag</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">-</span><span style="COLOR: #000000"> </span><span style="FONT-WEIGHT: bold; COLOR: #800000">1</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #0000ff">end</span><span style="COLOR: #000000"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #008080">--</span><span style="COLOR: #008080">结果输出</span><span style="COLOR: #008080"><br /><img src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">*</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"> </span><span style="COLOR: #008000">@t</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">order</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">by</span><span style="COLOR: #000000"> site</span></span></div>      注：这里引入一个site列，是为了能够在输出时更加好的进行排序，因为在最终输出至网页时很有可能是顺序向下输出的，这里正好体现出这种结构。<br /><br />      b.位置描述式的结构 ( 这是藕给它起的名称，它有一个列，该列描述了当前节点在树中的位置)<br />         DDL示例：<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #0000ff">create</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">table</span><span style="COLOR: #000000"> siteTree (<br />    id </span><span style="FONT-WEIGHT: bold; COLOR: #000000">varchar</span><span style="COLOR: #000000">(</span><span style="FONT-WEIGHT: bold; COLOR: #800000">2</span><span style="COLOR: #000000">) </span><span style="COLOR: #0000ff">primary</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">key</span><span style="COLOR: #000000">,<br />    nodeContent </span><span style="FONT-WEIGHT: bold; COLOR: #000000">varchar</span><span style="COLOR: #000000">(</span><span style="FONT-WEIGHT: bold; COLOR: #800000">20</span><span style="COLOR: #000000">),<br />    site </span><span style="FONT-WEIGHT: bold; COLOR: #000000">varchar</span><span style="COLOR: #000000">(</span><span style="FONT-WEIGHT: bold; COLOR: #800000">200</span><span style="COLOR: #000000">)<br />)</span></div><br />         <strong>例子</strong>：<br />            01      Root      01<br />            02      dep1      0102<br />            03      dep2      0103<br />            04      dep11    010201<br /><br />         <strong>优点</strong>：<br />            由于该表结构中带一个位置描述列site，因此对于关于树的遍历等问题的解决将变得异常的轻松。位置列按从根结点到当前结点的ID连接而成。<br /><br />         <strong>缺点</strong>：<br />            有利即有弊，位置描述列也同时突出该种结构的缺点，它最大的缺点就是树的层次不可能是无穷的，由于它由整个路径的ID连接而已，因此该列的宽度并不是固定的，如根节点的长度将是ID列的宽度，而当前节点位置列所需要空间 = 当前节点层次 * ID列宽度。<br />            针对链表式结构的优点，这里就成了缺点，层次间的增删将变得较为困难。<br /><br />         遍历树：<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008080">--</span><span style="COLOR: #008080">输出指定节点的全部子节点</span><span style="COLOR: #008080"><br /></span><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">*</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"> siteTree </span><span style="COLOR: #0000ff">where</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff00ff">CHARINDEX</span><span style="COLOR: #000000">(</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">00</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000">,site)</span><span style="COLOR: #808080">=</span><span style="FONT-WEIGHT: bold; COLOR: #800000">1</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">order</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">by</span><span style="COLOR: #000000"> site</span></div><br /><br /></font>
<img src ="http://www.blogjava.net/tw-ddm/aggbug/75209.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/tw-ddm/" target="_blank">大大毛</a> 2006-10-15 03:16 <a href="http://www.blogjava.net/tw-ddm/articles/75209.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>查询投票的例子(向表中临时插入特征列)</title><link>http://www.blogjava.net/tw-ddm/articles/63748.html</link><dc:creator>大大毛</dc:creator><author>大大毛</author><pubDate>Tue, 15 Aug 2006 10:23:00 GMT</pubDate><guid>http://www.blogjava.net/tw-ddm/articles/63748.html</guid><wfw:comment>http://www.blogjava.net/tw-ddm/comments/63748.html</wfw:comment><comments>http://www.blogjava.net/tw-ddm/articles/63748.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/tw-ddm/comments/commentRss/63748.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/tw-ddm/services/trackbacks/63748.html</trackback:ping><description><![CDATA[
		<font face="Verdana" size="2">
				<br />   <strong>问题(示例)：<br /></strong>      投票表vote，根据条件查询 有效/失效/全部 的投票主题，是否有效的条件是将当前日期与投票的有效期进行比较<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img id="Code_Closed_Image_173632" onclick="this.style.display='none'; Code_Closed_Text_173632.style.display='none'; Code_Open_Image_173632.style.display='inline'; Code_Open_Text_173632.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" /><img id="Code_Open_Image_173632" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_173632.style.display='none'; Code_Closed_Image_173632.style.display='inline'; Code_Closed_Text_173632.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" /><span id="Code_Closed_Text_173632" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">vote表结构</span><span id="Code_Open_Text_173632" style="DISPLAY: none"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: #0000ff">create</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">table</span><span style="COLOR: #000000"> vote (<br />  </span><span style="COLOR: #008080">--</span><span style="COLOR: #008080">投票ID,主键</span><span style="COLOR: #008080"><br /></span><span style="COLOR: #000000">  vID </span><span style="FONT-WEIGHT: bold; COLOR: #000000">int</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff00ff">identity</span><span style="COLOR: #000000">(</span><span style="FONT-WEIGHT: bold; COLOR: #800000">1</span><span style="COLOR: #000000">,</span><span style="FONT-WEIGHT: bold; COLOR: #800000">1</span><span style="COLOR: #000000">) </span><span style="COLOR: #0000ff">primary</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">key</span><span style="COLOR: #000000">,<br />  </span><span style="COLOR: #008080">--</span><span style="COLOR: #008080">投票主题</span><span style="COLOR: #008080"><br /></span><span style="COLOR: #000000">  topic </span><span style="FONT-WEIGHT: bold; COLOR: #000000">varchar</span><span style="COLOR: #000000">(</span><span style="FONT-WEIGHT: bold; COLOR: #800000">50</span><span style="COLOR: #000000">) </span><span style="COLOR: #808080">not</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000">,<br />  </span><span style="COLOR: #008080">--</span><span style="COLOR: #008080">生效日期</span><span style="COLOR: #008080"><br /></span><span style="COLOR: #000000">  beginDate </span><span style="FONT-WEIGHT: bold; COLOR: #000000">datetime</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">default</span><span style="COLOR: #000000">(</span><span style="COLOR: #ff00ff">getdate</span><span style="COLOR: #000000">()),<br />  </span><span style="COLOR: #008080">--</span><span style="COLOR: #008080">失效日期</span><span style="COLOR: #008080"><br /></span><span style="COLOR: #000000">  endDate </span><span style="FONT-WEIGHT: bold; COLOR: #000000">datetime</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">default</span><span style="COLOR: #000000">(</span><span style="COLOR: #ff00ff">getdate</span><span style="COLOR: #000000">())<br />)</span></span></div>      示例数据:<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #0000ff">insert</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">into</span><span style="COLOR: #000000"> vote </span><span style="COLOR: #0000ff">values</span><span style="COLOR: #000000"> ( </span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">topic01</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000">,</span><span style="COLOR: #ff00ff">dateadd</span><span style="COLOR: #000000">(</span><span style="COLOR: #ff00ff">day</span><span style="COLOR: #000000">,</span><span style="COLOR: #808080">-</span><span style="FONT-WEIGHT: bold; COLOR: #800000">2</span><span style="COLOR: #000000">,</span><span style="COLOR: #ff00ff">getdate</span><span style="COLOR: #000000">()),</span><span style="COLOR: #ff00ff">dateadd</span><span style="COLOR: #000000">(</span><span style="COLOR: #ff00ff">day</span><span style="COLOR: #000000">,</span><span style="FONT-WEIGHT: bold; COLOR: #800000">2</span><span style="COLOR: #000000">,</span><span style="COLOR: #ff00ff">getdate</span><span style="COLOR: #000000">()) )<br /></span><span style="COLOR: #0000ff">insert</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">into</span><span style="COLOR: #000000"> vote </span><span style="COLOR: #0000ff">values</span><span style="COLOR: #000000"> ( </span><span style="COLOR: #ff0000">'</span><span style="COLOR: #ff0000">topic02</span><span style="COLOR: #ff0000">'</span><span style="COLOR: #000000">,</span><span style="COLOR: #ff00ff">dateadd</span><span style="COLOR: #000000">(</span><span style="COLOR: #ff00ff">day</span><span style="COLOR: #000000">,</span><span style="COLOR: #808080">-</span><span style="FONT-WEIGHT: bold; COLOR: #800000">20</span><span style="COLOR: #000000">,</span><span style="COLOR: #ff00ff">getdate</span><span style="COLOR: #000000">()),</span><span style="COLOR: #ff00ff">dateadd</span><span style="COLOR: #000000">(</span><span style="COLOR: #ff00ff">day</span><span style="COLOR: #000000">,</span><span style="COLOR: #808080">-</span><span style="FONT-WEIGHT: bold; COLOR: #800000">10</span><span style="COLOR: #000000">,</span><span style="COLOR: #ff00ff">getdate</span><span style="COLOR: #000000">()) )</span></div>      实现效果<br />         有效投票为 topic01 ，无效投票为 topic02<br /><br />   <strong>解决方法：</strong><br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img id="Code_Closed_Image_172934" onclick="this.style.display='none'; Code_Closed_Text_172934.style.display='none'; Code_Open_Image_172934.style.display='inline'; Code_Open_Text_172934.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" /><img id="Code_Open_Image_172934" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_172934.style.display='none'; Code_Closed_Image_172934.style.display='inline'; Code_Closed_Text_172934.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" /><font style="BACKGROUND-COLOR: #ffffff"><strong>自连接查询1<span id="Code_Closed_Text_172934" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"></span></strong></font><span id="Code_Open_Text_172934" style="DISPLAY: none"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: #008080">--</span><span style="COLOR: #008080">获取全部投票</span><span style="COLOR: #008080"><br />--</span><span style="COLOR: #008080">auther:ddm</span><span style="COLOR: #008080"><br />--</span><span style="COLOR: #008080">last:2006-8-15</span><span style="COLOR: #008080"><br />--</span><span style="COLOR: #008080">参数</span><span style="COLOR: #008080"><br />--</span><span style="COLOR: #008080">  isValid(int):缺省值为全部投票</span><span style="COLOR: #008080"><br />--</span><span style="COLOR: #008080">    0:无效投票</span><span style="COLOR: #008080"><br />--</span><span style="COLOR: #008080">    1:有效投票</span><span style="COLOR: #008080"><br />--</span><span style="COLOR: #008080">    other:全部投票</span><span style="COLOR: #008080"><br /></span><span style="COLOR: #0000ff">create</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">procedure</span><span style="COLOR: #000000"> vote_GetAllVote<br />  </span><span style="COLOR: #008080">--</span><span style="COLOR: #008080">包含的投票类型:0,无效投票;1,有效投票;other,全部投票</span><span style="COLOR: #008080"><br /></span><span style="COLOR: #000000">  </span><span style="COLOR: #008000">@isValid</span><span style="COLOR: #000000"> </span><span style="FONT-WEIGHT: bold; COLOR: #000000">int</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">as</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">begin</span><span style="COLOR: #000000"><br />  </span><span style="COLOR: #008080">--</span><span style="COLOR: #008080">获取服务器当前日期</span><span style="COLOR: #008080"><br /></span><span style="COLOR: #000000">  </span><span style="COLOR: #0000ff">declare</span><span style="COLOR: #000000"> </span><span style="COLOR: #008000">@now</span><span style="COLOR: #000000"> </span><span style="FONT-WEIGHT: bold; COLOR: #000000">datetime</span><span style="COLOR: #000000"><br />  </span><span style="COLOR: #0000ff">set</span><span style="COLOR: #000000"> </span><span style="COLOR: #008000">@now</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff00ff">getdate</span><span style="COLOR: #000000">()<br />  </span><span style="COLOR: #008080">--<br /></span><span style="COLOR: #000000">  </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> </span><span style="COLOR: #008000">@isValid</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">!=</span><span style="FONT-WEIGHT: bold; COLOR: #800000">0</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">and</span><span style="COLOR: #000000"> </span><span style="COLOR: #008000">@isvalid</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">!=</span><span style="FONT-WEIGHT: bold; COLOR: #800000">1</span><span style="COLOR: #000000"><br />  </span><span style="COLOR: #0000ff">begin</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">set</span><span style="COLOR: #000000"> </span><span style="COLOR: #008000">@isValid</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">null</span><span style="COLOR: #000000"><br />  </span><span style="COLOR: #0000ff">end</span><span style="COLOR: #000000"><br /><br />  </span><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"><br />      vote.</span><span style="COLOR: #808080">*</span><span style="COLOR: #000000"><br />    </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"><br />      (<br />        </span><span style="COLOR: #008080">--</span><span style="COLOR: #008080">筛选当前有效的投票</span><span style="COLOR: #008080"><br /></span><span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"><br />          vID,<br />          isValid </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> </span><span style="FONT-WEIGHT: bold; COLOR: #800000">1</span><span style="COLOR: #000000"><br />        </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"><br />          vote<br />        </span><span style="COLOR: #0000ff">where</span><span style="COLOR: #000000"><br />          </span><span style="COLOR: #008000">@now</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">between</span><span style="COLOR: #000000"> beginDate </span><span style="COLOR: #808080">and</span><span style="COLOR: #000000"> endDate<br />      ) validVote<br />      </span><span style="COLOR: #ff00ff">right</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">join</span><span style="COLOR: #000000"> vote </span><span style="COLOR: #0000ff">on</span><span style="COLOR: #000000"> validVote.vID </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> vote.vID<br />    </span><span style="COLOR: #0000ff">where</span><span style="COLOR: #000000"><br />      </span><span style="COLOR: #ff00ff">isNull</span><span style="COLOR: #000000">(isValid,</span><span style="FONT-WEIGHT: bold; COLOR: #800000">0</span><span style="COLOR: #000000">) </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff00ff">isNull</span><span style="COLOR: #000000">(</span><span style="COLOR: #008000">@isValid</span><span style="COLOR: #000000">,</span><span style="COLOR: #ff00ff">isNull</span><span style="COLOR: #000000">(isValid,</span><span style="FONT-WEIGHT: bold; COLOR: #800000">0</span><span style="COLOR: #000000">))<br /></span><span style="COLOR: #0000ff">end</span></span></div>   在使用自连接查询时，使用判断条件生成一个临时的子表进行自连接。利用左/右连接时子表对应列为 null 的转换从而达到在 vote 表中插入一个特征列的目的，忽略掉最后的 where 子句，手工在 select vote.* 处加入  ,isValid即可以看到效果。<br /><br />   可以将SQL语句改写成下面这样，可以看得清楚一些。<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img id="Code_Closed_Image_175125" onclick="this.style.display='none'; Code_Closed_Text_175125.style.display='none'; Code_Open_Image_175125.style.display='inline'; Code_Open_Text_175125.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" /><img id="Code_Open_Image_175125" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_175125.style.display='none'; Code_Closed_Image_175125.style.display='inline'; Code_Closed_Text_175125.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" /><span id="Code_Closed_Text_175125" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><strong>自连接查询2</strong></span><span id="Code_Open_Text_175125" style="DISPLAY: none"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"><br />    vID,<br />    topic<br />  </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"> (<br />    </span><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"><br />        vote.</span><span style="COLOR: #808080">*</span><span style="COLOR: #000000">,<br />        isValid </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff00ff">isNull</span><span style="COLOR: #000000">(isValid,</span><span style="FONT-WEIGHT: bold; COLOR: #800000">0</span><span style="COLOR: #000000">)<br />      </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"> (<br />        </span><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"><br />            vID,<br />            isValid </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> </span><span style="FONT-WEIGHT: bold; COLOR: #800000">1</span><span style="COLOR: #000000"><br />          </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"><br />            vote<br />          </span><span style="COLOR: #0000ff">where</span><span style="COLOR: #000000"><br />            </span><span style="COLOR: #008000">@now</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">between</span><span style="COLOR: #000000"> beginDate </span><span style="COLOR: #808080">and</span><span style="COLOR: #000000"> endDate<br />        ) validVote<br />        </span><span style="COLOR: #ff00ff">right</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">join</span><span style="COLOR: #000000"> vote </span><span style="COLOR: #0000ff">on</span><span style="COLOR: #000000"> validVote.vID </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> vote.vID<br />    ) newVote<br />  </span><span style="COLOR: #0000ff">where</span><span style="COLOR: #000000"><br />    isValid </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff00ff">isNull</span><span style="COLOR: #000000">(</span><span style="COLOR: #008000">@isValid</span><span style="COLOR: #000000">,isValid)</span></span></div><br />   也可以根据需要，改写成子查询语句：<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><img id="Code_Closed_Image_180534" onclick="this.style.display='none'; Code_Closed_Text_180534.style.display='none'; Code_Open_Image_180534.style.display='inline'; Code_Open_Text_180534.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" /><img id="Code_Open_Image_180534" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_180534.style.display='none'; Code_Closed_Image_180534.style.display='inline'; Code_Closed_Text_180534.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" /><span id="Code_Closed_Text_180534" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><strong>子查询</strong></span><span id="Code_Open_Text_180534" style="DISPLAY: none"><br /><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"><br />    vID,<br />    topic<br />  </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"> (<br />    </span><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"><br />      </span><span style="COLOR: #808080">*</span><span style="COLOR: #000000">,<br />      isValid </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff00ff">isnull</span><span style="COLOR: #000000">(<br />                  (</span><span style="COLOR: #0000ff">select</span><span style="COLOR: #000000"><br />                        </span><span style="FONT-WEIGHT: bold; COLOR: #800000">1</span><span style="COLOR: #000000"><br />                      </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"><br />                        vote sub<br />                      </span><span style="COLOR: #0000ff">where</span><span style="COLOR: #000000"><br />                        sub.vid</span><span style="COLOR: #808080">=</span><span style="COLOR: #000000">vote.vid<br />                        </span><span style="COLOR: #808080">and</span><span style="COLOR: #000000"> </span><span style="COLOR: #008000">@now</span><span style="COLOR: #000000"> </span><span style="COLOR: #808080">between</span><span style="COLOR: #000000"> beginDate </span><span style="COLOR: #808080">and</span><span style="COLOR: #000000"> endDate<br />                  )<br />                ,</span><span style="FONT-WEIGHT: bold; COLOR: #800000">0</span><span style="COLOR: #000000">)<br />      </span><span style="COLOR: #0000ff">from</span><span style="COLOR: #000000"><br />        vote<br />    ) newVote<br />  </span><span style="COLOR: #0000ff">where</span><span style="COLOR: #000000"><br />    isValid </span><span style="COLOR: #808080">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #ff00ff">isNull</span><span style="COLOR: #000000">(</span><span style="COLOR: #008000">@isValid</span><span style="COLOR: #000000">,isValid)</span></span></div><br />   使用 exec vote_getAllVote 参数，即可演示效果(sqlServer2000)<br />   当然，生成两个临时表 validVote,invalidVote再联合查询 或者 用IF 加开关分开select也是可以的，这里就不赘述了。<br /><br />   <strong>后记</strong>：<br />      SQL语法上并没有规定解决问题只能使用一种方案，它采用了非常灵活的工作方式，因此具体使用哪一种解决方案取决于具体的应用及个人喜好。</font>
<img src ="http://www.blogjava.net/tw-ddm/aggbug/63748.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/tw-ddm/" target="_blank">大大毛</a> 2006-08-15 18:23 <a href="http://www.blogjava.net/tw-ddm/articles/63748.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>MsSQL中合并条件语句的方法</title><link>http://www.blogjava.net/tw-ddm/articles/63725.html</link><dc:creator>大大毛</dc:creator><author>大大毛</author><pubDate>Tue, 15 Aug 2006 09:06:00 GMT</pubDate><guid>http://www.blogjava.net/tw-ddm/articles/63725.html</guid><wfw:comment>http://www.blogjava.net/tw-ddm/comments/63725.html</wfw:comment><comments>http://www.blogjava.net/tw-ddm/articles/63725.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/tw-ddm/comments/commentRss/63725.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/tw-ddm/services/trackbacks/63725.html</trackback:ping><description><![CDATA[
		<p>
				<br />
				<font face="Verdana" size="2">   <strong>起因：</strong><br />      在工作中经常都会用到根据一个参数的值来做不同的 Where 条件的情况，例如：<br />      1.在提供给用户的查询界面上提供了多个筛选条件，如果用户选了就使用条件，如果不选就放弃该条件；<br />      2.存储过程的预留扩充功能，如 当前要求返回条件为真的记录，为了扩充，让存储过程根据参数可以返回条件为 真/假/全部 的记录等。<br />      在这些情况下，通常有3种实现方法：<br />         a.在外部(相对于数据库)拼装查询语句，我称它为外部动态SQL，嘻嘻；<br />         b.在内部(同上)拼装查询语句，我称它为内部动态SQL；<br />         c.在存储过程中使用大量的 IF 开关，搭配 ctrl+C,ctrl+V,edit操作，写上N条的SQL语句。<br /><br />   <strong>问题的分析</strong>：<br />      a 方法，该方法最多见，因为这是非常easy的办法，而且适用面最广，干啥事都直接来个SQL语句发往连接对象即可。当然这种方法在实现时也是N多的 IF，同时这种方法也会遗留下<strong>隐患</strong>，稍不留神就会成为 <strong>SQL注入</strong> 的牺牲品。<br />      b 方法，只是在存储过程实现了 a 方法，在减少隐患的同时，缩小了适用面，比如低版本的 mysql 就不支持存储过程。<br />      c 方法，是 b 方法的变种，虽然代码量非常的大，但是结构上很清晰，执行速度快。<br /><br />   <strong>解决方法：</strong><br />      在工作中找到的一个方法，适用于<strong>一定的问题域</strong>，可以将N多的SQL合并。<br />      为了能够清楚的说明，举个例子先。<br />      例(<strong>特定值的条件查询</strong>)：<br />            现在提供一个查询界面给管理员，让他能够对 log表 的查询条件进行定制。<br />            查询条件有：<br />               1.组ID，对应字段gID<br />               2.用户ID，对应字段uID<br />               3.记录类型，对应字段logType<br />            条件的判断为：<br />               如果用户选择了条件，那么就在Where中进行筛选，否则就忽略该条件，例如用户如果选择了 gID=1，那么就只选择组ID=1的用户Log，否则就返回全部的用户。通常是用上IF，这样：</font>
		</p>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font size="2">
						<font face="Verdana">
								<span style="COLOR: #0000ff">string</span>
								<span style="COLOR: #000000"> sql </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">select * from log </span>
								<span style="COLOR: #000000">"</span>
						</font>
				</font>
				<font size="2">
						<font face="Verdana">
								<span style="COLOR: #000000">;<br /></span>
								<span style="COLOR: #0000ff">string</span>
								<span style="COLOR: #000000"> where </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">""</span>
						</font>
				</font>
				<font size="2">
						<font face="Verdana">
								<span style="COLOR: #000000">;<br /><br /></span>
								<span style="COLOR: #0000ff">if</span>
								<span style="COLOR: #000000">(gID </span>
								<span style="COLOR: #000000">!=</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">null</span>
						</font>
				</font>
				<font size="2">
						<font face="Verdana">
								<span style="COLOR: #000000">) {<br />  where </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> where </span>
								<span style="COLOR: #000000">+</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000"> gID=</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">+</span>
								<span style="COLOR: #000000"> gID </span>
								<span style="COLOR: #000000">+</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000"> and </span>
								<span style="COLOR: #000000">"</span>
						</font>
				</font>
				<font size="2">
						<font face="Verdana">
								<span style="COLOR: #000000">;<br />}<br /></span>
								<span style="COLOR: #008000">//</span>
						</font>
				</font>
				<font size="2">
						<font face="Verdana">
								<span style="COLOR: #008000">更多的IF<img src="http://www.blogjava.net/images/dot.gif" /><br /></span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000">where字串还要处理一下</span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font size="2">
						<font face="Verdana">
								<span style="COLOR: #000000">sql </span>
								<span style="COLOR: #000000">+=</span>
								<span style="COLOR: #000000"> where;</span>
						</font>
				</font>
		</div>
		<p>
				<font face="Verdana" size="2">      现在使用MS SQL 的存储过程可以这样实现：</font>
		</p>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font size="2">
						<font face="Verdana">
								<img id="Code_Closed_Image_164153" onclick="this.style.display='none'; Code_Closed_Text_164153.style.display='none'; Code_Open_Image_164153.style.display='inline'; Code_Open_Text_164153.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" />
								<img id="Code_Open_Image_164153" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text_164153.style.display='none'; Code_Closed_Image_164153.style.display='inline'; Code_Closed_Text_164153.style.display='inline';" height="16" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" />
								<span id="Code_Closed_Text_164153" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
										<strong>定值条件查询</strong>
								</span>
						</font>
				</font>
				<span id="Code_Open_Text_164153" style="DISPLAY: none">
						<br />
						<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<font size="2">
								<font face="Verdana">
										<span style="COLOR: #008080">--</span>
										<span style="COLOR: #008080">取得Log</span>
								</font>
						</font>
						<span style="COLOR: #008080">
								<br />
								<font face="Verdana" size="2">--</font>
						</span>
						<span style="COLOR: #008080">
								<font face="Verdana" size="2">gID(int):组ID</font>
						</span>
						<span style="COLOR: #008080">
								<br />
								<font face="Verdana" size="2">--</font>
						</span>
						<span style="COLOR: #008080">
								<font face="Verdana" size="2">uID(int):用户ID</font>
						</span>
						<span style="COLOR: #008080">
								<br />
								<font face="Verdana" size="2">--</font>
						</span>
						<span style="COLOR: #008080">
								<font face="Verdana" size="2">logType(int):log类型</font>
						</span>
						<span style="COLOR: #008080">
								<br />
						</span>
						<font size="2">
								<font face="Verdana">
										<span style="COLOR: #0000ff">create</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">procedure</span>
								</font>
						</font>
						<font size="2">
								<font face="Verdana">
										<span style="COLOR: #000000"> p_getLog<br />  </span>
										<span style="COLOR: #008000">@gID</span>
										<span style="COLOR: #000000"> </span>
										<span style="FONT-WEIGHT: bold; COLOR: #000000">int</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #808080">=</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">null</span>
								</font>
						</font>
						<font size="2">
								<font face="Verdana">
										<span style="COLOR: #000000">,<br />  </span>
										<span style="COLOR: #008000">@uID</span>
										<span style="COLOR: #000000"> </span>
										<span style="FONT-WEIGHT: bold; COLOR: #000000">int</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #808080">=</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">null</span>
								</font>
						</font>
						<font size="2">
								<font face="Verdana">
										<span style="COLOR: #000000">,<br />  </span>
										<span style="COLOR: #008000">@logType</span>
										<span style="COLOR: #000000"> </span>
										<span style="FONT-WEIGHT: bold; COLOR: #000000">int</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #808080">=</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #0000ff">null</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<br />
						</span>
						<span style="COLOR: #0000ff">
								<font face="Verdana" size="2">as</font>
						</span>
						<span style="COLOR: #000000">
								<br />
						</span>
						<span style="COLOR: #0000ff">
								<font face="Verdana" size="2">begin</font>
						</span>
						<span style="COLOR: #000000">
								<br />
								<font face="Verdana" size="2">  </font>
						</span>
						<span style="COLOR: #0000ff">
								<font face="Verdana" size="2">select</font>
						</span>
						<span style="COLOR: #000000">
								<br />
								<font face="Verdana" size="2">      </font>
						</span>
						<span style="COLOR: #808080">
								<font face="Verdana" size="2">*</font>
						</span>
						<span style="COLOR: #000000">
								<br />
								<font face="Verdana" size="2">    </font>
						</span>
						<span style="COLOR: #0000ff">
								<font face="Verdana" size="2">from</font>
						</span>
						<span style="COLOR: #000000">
								<br />
								<font face="Verdana" size="2">      </font>
						</span>
						<span style="COLOR: #ff00ff">
								<font face="Verdana" size="2">log</font>
						</span>
						<span style="COLOR: #000000">
								<br />
								<font face="Verdana" size="2">    </font>
						</span>
						<span style="COLOR: #0000ff">
								<font face="Verdana" size="2">where</font>
						</span>
						<span style="COLOR: #000000">
								<br />
								<font face="Verdana" size="2">      gID </font>
						</span>
						<font size="2">
								<font face="Verdana">
										<span style="COLOR: #808080">=</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #ff00ff">isNull</span>
										<span style="COLOR: #000000">(</span>
										<span style="COLOR: #008000">@gID</span>
										<span style="COLOR: #000000">,gID) </span>
										<span style="COLOR: #808080">and</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<br />
								<font face="Verdana" size="2">      uID </font>
						</span>
						<font size="2">
								<font face="Verdana">
										<span style="COLOR: #808080">=</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #ff00ff">isNull</span>
										<span style="COLOR: #000000">(</span>
										<span style="COLOR: #008000">@uID</span>
										<span style="COLOR: #000000">,uID) </span>
										<span style="COLOR: #808080">and</span>
								</font>
						</font>
						<span style="COLOR: #000000">
								<br />
								<font face="Verdana" size="2">      logType </font>
						</span>
						<font size="2">
								<font face="Verdana">
										<span style="COLOR: #808080">=</span>
										<span style="COLOR: #000000"> </span>
										<span style="COLOR: #ff00ff">isNull</span>
										<span style="COLOR: #000000">(</span>
										<span style="COLOR: #008000">@logType</span>
								</font>
						</font>
						<font size="2">
								<font face="Verdana">
										<span style="COLOR: #000000">,logType)<br /></span>
										<span style="COLOR: #0000ff">end</span>
								</font>
						</font>
				</span>
		</div>
		<p>
				<font size="2">      仔细分析一下上面的存储过程不难看出问题所在，实际上就是用了Ms SQL的 isNull系统函数，它在判断特征值或列为空时，就用第2参数所示的值或列去取代，否则就取原值，就拿 gID = isNull ( @gID , gID ) 语句来说吧，<br />      @gID 为一个非空值时，如 @gID = 1，那么表达式成为 where gID = 1<br />      @gID的值为空 =null 时 ,那么该表达式实现上就成了 where gID = gID，多了这个不用担心会增加负荷，数据库还是有点智能会忽略掉这个条件的，嘻。<br />      上面的代码是一个比较简单的例子，可以在存储过程的代码区增加对输入参数的过滤，如 if not exists(select 1 from log where gID = @gID) set @gID=null 等。<br />      这样一来，既可以利用存储过程带来的好处，又利用了 isNull 带来的实惠，一举N得啊。<br /></font>
				<br />
				<font size="2">   <strong>后记：</strong><br />      这个解决方案是存在有局限性的，并不是什么地方都能适用，但是它也不失为一个解决问题的方法。<br />      --是人总要有点自己的想法，呵呵。</font>
		</p>
<img src ="http://www.blogjava.net/tw-ddm/aggbug/63725.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/tw-ddm/" target="_blank">大大毛</a> 2006-08-15 17:06 <a href="http://www.blogjava.net/tw-ddm/articles/63725.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>