﻿<?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>IT博客-啊啦神灯-随笔分类-数据库</title><link>http://www.cnitblog.com/myshare/category/3829.html</link><description /><language>zh-cn</language><lastBuildDate>Thu, 06 Oct 2011 05:08:57 GMT</lastBuildDate><pubDate>Thu, 06 Oct 2011 05:08:57 GMT</pubDate><ttl>60</ttl><item><title>SqlServer如何生成动态交叉表查询</title><link>http://www.cnitblog.com/myshare/archive/2006/09/18/17013.html</link><dc:creator>啊啦神灯</dc:creator><author>啊啦神灯</author><pubDate>Mon, 18 Sep 2006 15:26:00 GMT</pubDate><guid>http://www.cnitblog.com/myshare/archive/2006/09/18/17013.html</guid><wfw:comment>http://www.cnitblog.com/myshare/comments/17013.html</wfw:comment><comments>http://www.cnitblog.com/myshare/archive/2006/09/18/17013.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/myshare/comments/commentRss/17013.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/myshare/services/trackbacks/17013.html</trackback:ping><description><![CDATA[
		<p>为了说明问题，我们用SqlServer自带的事例数据库(Northwind)来进行验证，所有的例子请放到Northwind中运行，我可能会省略Use语句，所引用的表，都是Northwind中的，下面我就不再说明了</p>
		<p>这里指的交叉表，就是象Access的交叉表查询一样的效果，比如Employees表中City字段代表了城市的名称，TitleOfCourtesy代表称呼，我们希望按照City和TitleOfCourtesy的情况来统计ReportsTo字段的合计数（本统计没有任何实际意义，只是挑选一些记录包含重复内容的字段来说明情况），并显示成以下格式：（TitleOfCourtesy作为行，City作为列）<br />探讨这个问题之前，我们首先来看一下如何建立静态的交叉表，也就是说列数固定的交叉表，这种情况其实只要一句简单的Select查询就可以搞定：<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">
				<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				<span style="COLOR: #0000ff">SELECT</span>
				<span style="COLOR: #000000"> TitleOfCourtesy, <br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />  </span>
				<span style="COLOR: #ff00ff">SUM</span>
				<span style="COLOR: #000000">(</span>
				<span style="COLOR: #ff00ff">CASE</span>
				<span style="COLOR: #000000"> City </span>
				<span style="COLOR: #0000ff">WHEN</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #ff0000">'</span>
				<span style="COLOR: #ff0000">London</span>
				<span style="COLOR: #ff0000">'</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">THEN</span>
				<span style="COLOR: #000000"> ReportsTo </span>
				<span style="COLOR: #0000ff">ELSE</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">NULL</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">END</span>
				<span style="COLOR: #000000">) </span>
				<span style="COLOR: #0000ff">AS</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #ff0000">[</span>
				<span style="COLOR: #ff0000">London City</span>
				<span style="COLOR: #ff0000">]</span>
				<span style="COLOR: #000000">, <br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /> </span>
				<span style="COLOR: #ff00ff">SUM</span>
				<span style="COLOR: #000000">(</span>
				<span style="COLOR: #ff00ff">CASE</span>
				<span style="COLOR: #000000"> City </span>
				<span style="COLOR: #0000ff">WHEN</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #ff0000">'</span>
				<span style="COLOR: #ff0000">Redmond</span>
				<span style="COLOR: #ff0000">'</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">THEN</span>
				<span style="COLOR: #000000"> ReportsTo </span>
				<span style="COLOR: #0000ff">ELSE</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">NULL</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">END</span>
				<span style="COLOR: #000000">) </span>
				<span style="COLOR: #0000ff">AS</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #ff0000">[</span>
				<span style="COLOR: #ff0000">Redmond City</span>
				<span style="COLOR: #ff0000">]</span>
				<span style="COLOR: #000000">, <br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /> </span>
				<span style="COLOR: #ff00ff">SUM</span>
				<span style="COLOR: #000000">(</span>
				<span style="COLOR: #ff00ff">CASE</span>
				<span style="COLOR: #000000"> City </span>
				<span style="COLOR: #0000ff">WHEN</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #ff0000">'</span>
				<span style="COLOR: #ff0000">Seattle</span>
				<span style="COLOR: #ff0000">'</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">THEN</span>
				<span style="COLOR: #000000"> ReportsTo </span>
				<span style="COLOR: #0000ff">ELSE</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">NULL</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">END</span>
				<span style="COLOR: #000000">) </span>
				<span style="COLOR: #0000ff">AS</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #ff0000">[</span>
				<span style="COLOR: #ff0000">Seattle City</span>
				<span style="COLOR: #ff0000">]</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #0000ff">FROM</span>
				<span style="COLOR: #000000"> Employees </span>
				<span style="COLOR: #0000ff">GROUP</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">BY</span>
				<span style="COLOR: #000000"> TitleOfCourtesy</span>
		</div>
		<p>其中利用了CASE语句判断，如果是相应的列，则取需要统计的ReportsTo数值，否则取NULL，然后再合计<br />其中有两个常见问题说明一下：<br />a、用NULL而不用0是有道理的，假如用0，虽然求和函数SUM可以取到正确的数，但类似COUNT函数（取记录个数），结果就不对了，因为Null不算一条记录，而0要算，同理空字串（""）也是这样，总之在这里应该用NULL,这样任何函数都没问题。</p>
		<p>b、假如在视图的设计界面保存以上的查询，则会报错“没有输出列”，从而无法保存，其实只要在查询前面加上一段：Create View ViewName AS ...，ViewName是你准备给查询起的名称，...就是我们的查询，然后运行一下，就可以生成视图了，对于其他一些设计器不支持的语法，也可以这样保存。</p>
		<p>以上查询作用也很大，对于很多情况，比如按照季度统计、按照月份统计等列头内容固定的情况，这样就行了，但往往大多数情况下列头内容是不固定的，象City，用户随时可能删除、添加一些城市，这种情况，我们就需要用存储过程来解决：</p>
		<p>总体思路其实很简单，首先检索列头信息，形成一个游标，然后遍历游标，将上面查询语句里Case判断的内容用游标里的值替代，形成一条新的Sql查询，然后执行，返回结果，就可以了，以下是我写的一个存储过程，供大家参考：<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">
				<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				<span style="COLOR: #0000ff">CREATE</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">procedure</span>
				<span style="COLOR: #000000"> CorssTab <br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #008000">@strTabName</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">as</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">50</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">Employees</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.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #008000">@strCol</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">as</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">50</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">City</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.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #008000">@strGroup</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">as</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">50</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">TitleOfCourtesy</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.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #008000">@strNumber</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">as</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">50</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">ReportsTo</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.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #008000">@strSum</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">as</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">10</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">Sum</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.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #0000ff">AS</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #0000ff">DECLARE</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #008000">@strSql</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">as</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">1000</span>
				<span style="COLOR: #000000">), </span>
				<span style="COLOR: #008000">@strTmpCol</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">as</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">100</span>
				<span style="COLOR: #000000">)<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /></span>
				<span style="COLOR: #0000ff">EXECUTE</span>
				<span style="COLOR: #000000"> (</span>
				<span style="COLOR: #ff0000">'</span>
				<span style="COLOR: #ff0000">DECLARE corss_cursor CURSOR FOR SELECT DISTINCT </span>
				<span style="COLOR: #ff0000">'</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #808080">+</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #008000">@strCol</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"> from </span>
				<span style="COLOR: #ff0000">'</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #808080">+</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #008000">@strTabName</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"> for read only </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.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #0000ff">begin</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />  </span>
				<span style="COLOR: #0000ff">SET</span>
				<span style="COLOR: #000000"> nocount </span>
				<span style="COLOR: #0000ff">ON</span>
				<span style="COLOR: #000000"> <br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />  </span>
				<span style="COLOR: #0000ff">SET</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #008000">@strsql</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #808080">=</span>
				<span style="COLOR: #ff0000">'</span>
				<span style="COLOR: #ff0000">select </span>
				<span style="COLOR: #ff0000">'</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #808080">+</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #008000">@strGroup</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">, </span>
				<span style="COLOR: #ff0000">'</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #808080">+</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #008000">@strSum</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">(</span>
				<span style="COLOR: #ff0000">'</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #808080">+</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #008000">@strNumber</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">) AS [</span>
				<span style="COLOR: #ff0000">'</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #808080">+</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #008000">@strSum</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"> of </span>
				<span style="COLOR: #ff0000">'</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #808080">+</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #008000">@strNumber</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">]</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.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />  </span>
				<span style="COLOR: #0000ff">OPEN</span>
				<span style="COLOR: #000000"> corss_cursor<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />  </span>
				<span style="COLOR: #0000ff">while</span>
				<span style="COLOR: #000000"> (</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">0</span>
				<span style="COLOR: #808080">=</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">0</span>
				<span style="COLOR: #000000">)<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />  </span>
				<span style="COLOR: #0000ff">BEGIN</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
				<span style="COLOR: #0000ff">FETCH</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">NEXT</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">FROM</span>
				<span style="COLOR: #000000"> corss_cursor </span>
				<span style="COLOR: #008080">--</span>
				<span style="COLOR: #008080">遍历游标，将列头信息放入变量@strTmpCol</span>
				<span style="COLOR: #008080">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">    </span>
				<span style="COLOR: #0000ff">INTO</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #008000">@strTmpCol</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
				<span style="COLOR: #0000ff">if</span>
				<span style="COLOR: #000000"> (</span>
				<span style="FONT-WEIGHT: bold; COLOR: #008000">@@fetch_status</span>
				<span style="COLOR: #808080">&lt;&gt;</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">0</span>
				<span style="COLOR: #000000">) </span>
				<span style="COLOR: #0000ff">break</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />          </span>
				<span style="COLOR: #0000ff">SET</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #008000">@strsql</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #808080">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #008000">@strsql</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">, </span>
				<span style="COLOR: #ff0000">'</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #808080">+</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #008000">@strSum</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">(CASE </span>
				<span style="COLOR: #ff0000">'</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #808080">+</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #008000">@strCol</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"> WHEN </span>
				<span style="COLOR: #ff0000">'''</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #808080">+</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #008000">@strTmpCol</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"> THEN </span>
				<span style="COLOR: #ff0000">'</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #808080">+</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #008000">@strNumber</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"> ELSE Null END) AS [</span>
				<span style="COLOR: #ff0000">'</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #808080">+</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #008000">@strTmpCol</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"> </span>
				<span style="COLOR: #ff0000">'</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #808080">+</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #008000">@strCol</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">]</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.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">  </span>
				<span style="COLOR: #0000ff">END</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />        </span>
				<span style="COLOR: #0000ff">SET</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #008000">@strsql</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #808080">=</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #008000">@strsql</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"> from </span>
				<span style="COLOR: #ff0000">'</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #808080">+</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #008000">@strTabname</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"> group by </span>
				<span style="COLOR: #ff0000">'</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #808080">+</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #008000">@strGroup</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.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />  </span>
				<span style="COLOR: #0000ff">EXECUTE</span>
				<span style="COLOR: #000000">(</span>
				<span style="COLOR: #008000">@strsql</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.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">  </span>
				<span style="COLOR: #0000ff">IF</span>
				<span style="COLOR: #000000"> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #008000">@@error</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #808080">&lt;&gt;</span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">0</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">RETURN</span>
				<span style="COLOR: #000000"> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #008000">@@error</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.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">  </span>
				<span style="COLOR: #0000ff">CLOSE</span>
				<span style="COLOR: #000000"> corss_cursor <br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />  </span>
				<span style="COLOR: #0000ff">DEALLOCATE</span>
				<span style="COLOR: #000000"> corss_cursor </span>
				<span style="COLOR: #0000ff">RETURN</span>
				<span style="COLOR: #000000"> </span>
				<span style="FONT-WEIGHT: bold; COLOR: #800000">0</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #008080">--</span>
				<span style="COLOR: #008080">释放游标，返回0表示成功</span>
				<span style="COLOR: #008080">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #0000ff">end</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #0000ff">GO</span>
		</div>
		<p>几点说明：<br />a、这是一个通用存储过程，使用时@strTabName、@strCol、@strGroup、@strNumber、@strSum几个变量设置一下就可以用到其他表上，其中结果集的第二列我加了个合计列<br />b、为了测试方便，我在存储过程中设置了默认值，就是前面提到的Employees表，这样直接运行时就可以出来我上面提到的结果。<br />c、使用时，可以把上面的代码复制到企业管理器的查询设计界面Sql窗格，或者查询分析器里运行一下（注意正确选择NorthWind数据库），就可以生成一个存储过程：CorssTab，然后直接运行CorssTab，如果出现本文前面类似的窗格，就表示运行成功了。<br />d、假如用于其它表，首先需要在你的用户数据库里生成此存储过程（当然也可以放到Master里，然后再加个变量：@DataBase，赋值为数据库名称，然后在上面代码打开指定数据库，这样所有的数据库都可以调用它），当你调用时，采取以下格式：</p>
		<p>CorssTab @strTabName = 'Orders', @strCol = 'DATEPART(yy, OrderDate)',@strGroup = 'CustomerID', @strNumber = 'OrderID', @strSum = 'Count'</p>
		<p>上面这条语句统计了NorthWind中Orders表里每个客户年度订单数量，大家可以运行试一下效果，虽然列头显示的名称不恰当，但基本效果出来了，相信大家通过对我的代码再作简单修改，可以达到满意的交叉表效果。</p>
<img src ="http://www.cnitblog.com/myshare/aggbug/17013.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/myshare/" target="_blank">啊啦神灯</a> 2006-09-18 23:26 <a href="http://www.cnitblog.com/myshare/archive/2006/09/18/17013.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>实现九九乘法表</title><link>http://www.cnitblog.com/myshare/archive/2006/09/14/16845.html</link><dc:creator>啊啦神灯</dc:creator><author>啊啦神灯</author><pubDate>Thu, 14 Sep 2006 08:35:00 GMT</pubDate><guid>http://www.cnitblog.com/myshare/archive/2006/09/14/16845.html</guid><wfw:comment>http://www.cnitblog.com/myshare/comments/16845.html</wfw:comment><comments>http://www.cnitblog.com/myshare/archive/2006/09/14/16845.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/myshare/comments/commentRss/16845.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/myshare/services/trackbacks/16845.html</trackback:ping><description><![CDATA[
		<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" onclick="this.style.display='none'; Code_Closed_Text.style.display='none'; Code_Open_Image.style.display='inline'; Code_Open_Text.style.display='inline';" height="16" src="http://www.cnitblog.com/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top" />
				<img id="Code_Open_Image" style="DISPLAY: none" onclick="this.style.display='none'; Code_Open_Text.style.display='none'; Code_Closed_Image.style.display='inline'; Code_Closed_Text.style.display='inline';" height="16" src="http://www.cnitblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top" />
				<span id="Code_Closed_Text" 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" style="DISPLAY: none">
						<br />
						<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
						<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
						<span style="COLOR: #0000ff">declare</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@a</span>
						<span style="COLOR: #000000"> </span>
						<span style="FONT-WEIGHT: bold; COLOR: #000000">int</span>
						<span style="COLOR: #000000">, </span>
						<span style="COLOR: #008000">@b</span>
						<span style="COLOR: #000000"> </span>
						<span style="FONT-WEIGHT: bold; COLOR: #000000">int</span>
						<span style="COLOR: #000000">, </span>
						<span style="COLOR: #008000">@output</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">100</span>
						<span style="COLOR: #000000">)<br /><img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" /></span>
						<span style="COLOR: #0000ff">set</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@a</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.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
						</span>
						<span style="COLOR: #0000ff">while</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@a</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">&lt;=</span>
						<span style="COLOR: #000000"> </span>
						<span style="FONT-WEIGHT: bold; COLOR: #800000">9</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
						</span>
						<span style="COLOR: #0000ff">begin</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />        </span>
						<span style="COLOR: #0000ff">set</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@b</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.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />        </span>
						<span style="COLOR: #0000ff">set</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@output</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #ff0000">''</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />        </span>
						<span style="COLOR: #0000ff">while</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@b</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">&lt;=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@a</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />        </span>
						<span style="COLOR: #0000ff">begin</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />        </span>
						<span style="COLOR: #0000ff">set</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@output</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@output</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">+</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #ff00ff">convert</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">100</span>
						<span style="COLOR: #000000">), </span>
						<span style="COLOR: #008000">@b</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">*</span>
						<span style="COLOR: #ff0000">'</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">+</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #ff00ff">convert</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">100</span>
						<span style="COLOR: #000000">), </span>
						<span style="COLOR: #008000">@a</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">=</span>
						<span style="COLOR: #ff0000">'</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">+</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #ff00ff">convert</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">100</span>
						<span style="COLOR: #000000">), </span>
						<span style="COLOR: #008000">@a</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">*</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@b</span>
						<span style="COLOR: #000000">) </span>
						<span style="COLOR: #808080">+</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #ff00ff">space</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">space(2)表示空格个数的正整数,这里为2个空格  </span>
						<span style="COLOR: #008080">
								<br />
								<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
						</span>
						<span style="COLOR: #000000">        </span>
						<span style="COLOR: #0000ff">set</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@b</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@b</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.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />        </span>
						<span style="COLOR: #0000ff">end</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />        </span>
						<span style="COLOR: #0000ff">print</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@output</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />        </span>
						<span style="COLOR: #0000ff">set</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@a</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #808080">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">@a</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.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
						</span>
						<span style="COLOR: #0000ff">end</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.cnitblog.com/images/OutliningIndicators/None.gif" align="top" />
						</span>
				</span>
		</div>
<img src ="http://www.cnitblog.com/myshare/aggbug/16845.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/myshare/" target="_blank">啊啦神灯</a> 2006-09-14 16:35 <a href="http://www.cnitblog.com/myshare/archive/2006/09/14/16845.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>