﻿<?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博客-路漫漫其修远兮，吾将上下而求索-随笔分类-SQL应用</title><link>http://www.cnitblog.com/ma3qiang2/category/6855.html</link><description /><language>zh-cn</language><lastBuildDate>Tue, 27 Sep 2011 00:24:09 GMT</lastBuildDate><pubDate>Tue, 27 Sep 2011 00:24:09 GMT</pubDate><ttl>60</ttl><item><title>遍历子树_公司组织一览</title><link>http://www.cnitblog.com/ma3qiang2/archive/2008/03/07/40645.html</link><dc:creator>马强</dc:creator><author>马强</author><pubDate>Fri, 07 Mar 2008 10:16:00 GMT</pubDate><guid>http://www.cnitblog.com/ma3qiang2/archive/2008/03/07/40645.html</guid><wfw:comment>http://www.cnitblog.com/ma3qiang2/comments/40645.html</wfw:comment><comments>http://www.cnitblog.com/ma3qiang2/archive/2008/03/07/40645.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ma3qiang2/comments/commentRss/40645.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ma3qiang2/services/trackbacks/40645.html</trackback:ping><description><![CDATA[---SQL2000---<br>
--测试数据<br>
CREATE TABLE dbo.tbtest(col1 int,col2 int)<br>
INSERT dbo.tbtest SELECT 1,0<br>
UNION ALL SELECT 2,1<br>
UNION ALL SELECT 3,1<br>
UNION ALL SELECT 4,2<br>
UNION ALL SELECT 5,2<br>
UNION ALL SELECT 6,2<br>
UNION ALL SELECT 7,3<br>
UNION ALL SELECT 8,3<br>
UNION ALL SELECT 9,3<br>
UNION ALL SELECT 10,7<br>
<br>
--把这个表变为<br>
/*<br>
1&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp; &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bsp; &nbsp;&nbsp;&nbsp; 5&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>
2&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 6&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>
3&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 3&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>
3&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 7&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>
3&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 8&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>
3&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 9&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>
7&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 7<br>
7&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 10<br>
8&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 8<br>
9&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 9<br>
10&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 10<br>
这种形式的数据<br>
*/<br>
<br>
--建立函数(此函数参考邹建大哥的)<br>
&nbsp; alter&nbsp;&nbsp; function&nbsp;&nbsp; f_cid(&nbsp; <br>
&nbsp; @id&nbsp;&nbsp; int&nbsp; <br>
&nbsp; )returns&nbsp;&nbsp; @re&nbsp;&nbsp; table(col1&nbsp;&nbsp; int,[level]&nbsp;&nbsp; int)&nbsp; <br>
&nbsp; as&nbsp; <br>
&nbsp; begin&nbsp; <br>
&nbsp; declare&nbsp;&nbsp; @l&nbsp;&nbsp; int&nbsp; <br>
&nbsp; set&nbsp;&nbsp; @l=0&nbsp; <br>
&nbsp; insert&nbsp;&nbsp; @re&nbsp;&nbsp; select&nbsp;&nbsp; col1,@l&nbsp;&nbsp; from&nbsp; dbo.tbtest&nbsp;&nbsp;&nbsp; where&nbsp;&nbsp; col2=@id&nbsp; <br>
&nbsp; while&nbsp;&nbsp; @@rowcount&gt;0&nbsp; --知道没有子插入为止<br>
&nbsp; begin&nbsp; <br>
&nbsp; set&nbsp;&nbsp; @l=@l+1&nbsp; <br>
&nbsp; insert&nbsp;&nbsp; @re&nbsp;&nbsp; select&nbsp;&nbsp; a.col1,@l&nbsp; <br>
&nbsp; from&nbsp;&nbsp; dbo.tbtest&nbsp;&nbsp; a,@re&nbsp;&nbsp; b&nbsp; <br>
&nbsp; where&nbsp;&nbsp; a.col2=b.col1&nbsp;&nbsp; and&nbsp;&nbsp; b.[level]=@l-1&nbsp; <br>
&nbsp; end&nbsp; <br>
&nbsp; return&nbsp; <br>
&nbsp; end&nbsp; <br>
go<br>
<br>
<br>
IF object_id('tempdb..#BB') IS not null&nbsp;&nbsp; DROP TABLE #BB<br>
CREATE table #BB(col1 int,col2 int)<br>
<br>
DECLARE tnames_cursor CURSOR<br>
FOR<br>
&nbsp;&nbsp; SELECT col1 FROM tbtest ORDER BY col1<br>
OPEN tnames_cursor<br>
DECLARE @cur1 INT<br>
FETCH NEXT FROM tnames_cursor INTO @cur1<br>
WHILE(@@fetch_status=0)<br>
BEGIN<br>
--&nbsp; print @cur1<br>
-- select&nbsp;&nbsp; a.*&nbsp; from&nbsp;&nbsp; tbtest&nbsp;&nbsp; a,f_cid(1) b&nbsp;&nbsp; where&nbsp;&nbsp; a.col1=b.col1<br>
&nbsp;&nbsp; INSERT&nbsp;&nbsp; #BB&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SELECT&nbsp;&nbsp; a.col1,@cur1 FROM&nbsp;&nbsp; #aa&nbsp;&nbsp; a,f_cid(@cur1) b&nbsp;&nbsp; WHERE&nbsp;&nbsp; a.col1=b.col1<br>
FETCH NEXT FROM tnames_cursor&nbsp; INTO&nbsp; @cur1<br>
END<br>
CLOSE tnames_cursor<br>
DEALLOCATE&nbsp;&nbsp; tnames_cursor<br>
<br>
SELECT re.*<br>
FROM (<br>
SELECT * FROM #bb --不包括自己的所有遍历<br>
UNION <br>
SELECT DISTINCT col1,col1 AS col2 FROM tbtest ) AS re<br>
ORDER BY re.col2<br>
<br>
<br>
IF object_id('tempdb..#BB') IS not null&nbsp;&nbsp; DROP TABLE #BB<br><img src ="http://www.cnitblog.com/ma3qiang2/aggbug/40645.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ma3qiang2/" target="_blank">马强</a> 2008-03-07 18:16 <a href="http://www.cnitblog.com/ma3qiang2/archive/2008/03/07/40645.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>