<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Linchi Shea : Recursive CTEs</title><link>http://sqlblog.com/blogs/linchi_shea/archive/tags/Recursive+CTEs/default.aspx</link><description>Tags: Recursive CTEs</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Recursive SQL queries: How do they work?</title><link>http://sqlblog.com/blogs/linchi_shea/archive/2009/04/16/recursive-sql-queries-how-do-they-work.aspx</link><pubDate>Thu, 16 Apr 2009 04:36:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:13314</guid><dc:creator>Linchi Shea</dc:creator><slash:comments>12</slash:comments><comments>http://sqlblog.com/blogs/linchi_shea/comments/13314.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/linchi_shea/commentrss.aspx?PostID=13314</wfw:commentRss><description>&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;FONT face="Times New Roman" size=3&gt;Recursive SQL queries are built with recursive common table expressions (CTEs). You can look at the execution semantics of a recursive CTE&amp;nbsp;from two different, but equivalent, perspectives. &lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;FONT face="Times New Roman" size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;FONT face="Times New Roman" size=3&gt;We can call the first one the UNION ALL view. The following pseudo code that illustrates how a recursive CTE executes is taken from the SQL Server 2005 Books Online:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;FONT face="Times New Roman" size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;OL style="MARGIN-TOP:0in;"&gt;
&lt;LI class=MsoNormal style="MARGIN:0in 0in 3pt;mso-list:l1 level1 lfo1;tab-stops:list .5in;"&gt;&lt;FONT face="Times New Roman" size=3&gt;Split the CTE expression into anchor and recursive members.&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI class=MsoNormal style="MARGIN:0in 0in 3pt;mso-list:l1 level1 lfo1;tab-stops:list .5in;"&gt;&lt;FONT face="Times New Roman" size=3&gt;Run the anchor member(s) creating the first invocation or base result set (T0).&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI class=MsoNormal style="MARGIN:0in 0in 3pt;mso-list:l1 level1 lfo1;tab-stops:list .5in;"&gt;&lt;FONT face="Times New Roman" size=3&gt;Run the recursive member(s) with Ti as an input and Ti+1 as an output.&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI class=MsoNormal style="MARGIN:0in 0in 3pt;mso-list:l1 level1 lfo1;tab-stops:list .5in;"&gt;&lt;FONT face="Times New Roman" size=3&gt;Repeat step 3 until an empty set is returned.&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI class=MsoNormal style="MARGIN:0in 0in 3pt;mso-list:l1 level1 lfo1;tab-stops:list .5in;"&gt;&lt;FONT size=3&gt;&lt;FONT face="Times New Roman"&gt;Return the result set. This is a UNION ALL of T0 to Tn.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/LI&gt;&lt;/OL&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;FONT face="Times New Roman" size=3&gt;This is an intuitive, and—some may even argue—is very much a set-based view of recursive CTEs because it is described with query resultsets and the UNION ALL operator.&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;FONT face="Times New Roman" size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;FONT face="Times New Roman" size=3&gt;The second approach can be labeled as the stack view. The following pseudo code defines the execution semantics of a recursive CTE:&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;FONT face="Times New Roman" size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;OL style="MARGIN-TOP:0in;"&gt;
&lt;LI class=MsoNormal style="MARGIN:0in 0in 3pt;mso-list:l0 level1 lfo2;tab-stops:list .5in;"&gt;&lt;FONT face="Times New Roman" size=3&gt;Run the anchor member and push the resultset in a stack.&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI class=MsoNormal style="MARGIN:0in 0in 3pt;mso-list:l0 level1 lfo2;tab-stops:list .5in;"&gt;&lt;FONT face="Times New Roman" size=3&gt;Let a cursor point to the first row from the bottom of the stack.&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI class=MsoNormal style="MARGIN:0in 0in 3pt;mso-list:l0 level1 lfo2;tab-stops:list .5in;"&gt;&lt;FONT face="Times New Roman" size=3&gt;Use the current row to evaluate the recursive member, and push the resultset into the stack.&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI class=MsoNormal style="MARGIN:0in 0in 3pt;mso-list:l0 level1 lfo2;tab-stops:list .5in;"&gt;&lt;FONT face="Times New Roman" size=3&gt;Move the cursor one row up in the stack.&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI class=MsoNormal style="MARGIN:0in 0in 3pt;mso-list:l0 level1 lfo2;tab-stops:list .5in;"&gt;&lt;FONT face="Times New Roman" size=3&gt;Repeat Step 3 until the cursor is moved beyond the top of the stack.&lt;/FONT&gt;&lt;/LI&gt;&lt;/OL&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;FONT size=3&gt;&lt;FONT face="Times New Roman"&gt;If you don’t like the word ‘cursor’, replace it with the word ‘pointer’ &lt;/FONT&gt;&lt;SPAN style="FONT-FAMILY:Wingdings;mso-ascii-font-family:'Times New Roman';mso-hansi-font-family:'Times New Roman';mso-char-type:symbol;mso-symbol-font-family:Wingdings;"&gt;&lt;SPAN style="mso-char-type:symbol;mso-symbol-font-family:Wingdings;"&gt;J&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT face="Times New Roman"&gt;.&amp;nbsp; As far as I know, the stack view is much less talked about in the SQL Server community. But it's always good to have different perspectives on the same thing. &lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;FONT face="Times New Roman" size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;FONT face="Times New Roman" size=3&gt;I personally find the stack view less intuitive, though it is actually closer to how a recursive CTE is implemented. When I think about solving a problem with a recursive CTE, I always think with the UNION ALL view. However, one of the DB2 gurus I used to work with insists that the stack view suits him just fine, and he finds it intuitive enough that it is how he thinks when approaching a problem with recursive CTEs.&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;o:p&gt;&lt;FONT face="Times New Roman" size=3&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN:0in 0in 0pt;"&gt;&lt;FONT face="Times New Roman" size=3&gt;For you folks reading this post,&amp;nbsp;most of you probably think in terms of the UNION ALL of the resultsets when working with recursive CTEs. But I could be wrong.&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=13314" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/linchi_shea/archive/tags/Query+Processing/default.aspx">Query Processing</category><category domain="http://sqlblog.com/blogs/linchi_shea/archive/tags/Recursive+CTEs/default.aspx">Recursive CTEs</category></item></channel></rss>