<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: SQL SERVER &#8211; SQL SERVER &#8211; Simple Example of Recursive CTE &#8211; Part 2 &#8211; MAXRECURSION &#8211; Prevent CTE Infinite Loop</title>
	<atom:link href="http://blog.sqlauthority.com/2008/07/29/sql-server-sql-server-simple-example-of-recursive-cte-part-2-maxrecursion-prevent-cte-infinite-loop/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2008/07/29/sql-server-sql-server-simple-example-of-recursive-cte-part-2-maxrecursion-prevent-cte-infinite-loop/</link>
	<description>Personal Notes of Pinal Dave</description>
	<lastBuildDate>Fri, 17 May 2013 15:26:57 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Dilshod</title>
		<link>http://blog.sqlauthority.com/2008/07/29/sql-server-sql-server-simple-example-of-recursive-cte-part-2-maxrecursion-prevent-cte-infinite-loop/#comment-406558</link>
		<dc:creator><![CDATA[Dilshod]]></dc:creator>
		<pubDate>Fri, 11 Jan 2013 17:40:08 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=751#comment-406558</guid>
		<description><![CDATA[It is very good example for starters. Thank you for posting!]]></description>
		<content:encoded><![CDATA[<p>It is very good example for starters. Thank you for posting!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vishal bhargava</title>
		<link>http://blog.sqlauthority.com/2008/07/29/sql-server-sql-server-simple-example-of-recursive-cte-part-2-maxrecursion-prevent-cte-infinite-loop/#comment-392427</link>
		<dc:creator><![CDATA[Vishal bhargava]]></dc:creator>
		<pubDate>Thu, 13 Dec 2012 09:48:52 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=751#comment-392427</guid>
		<description><![CDATA[I have a CTE Table Category and Sub Category With ID,Parent ID relation.

I Wana to retrieve all Nested Sub Categories In Nested Ordered Manner.

Some one Help me soon The Result Shuld be in Following Manners.

Main Cat

-- Sub A of Main

----Child Sub A of Sub A of Main

----Child Sub B of Sub A of Main

----Child Sub C of Sub A of Main

-- Sub B of Main

----Child Sub A of Sub B of Main

----Child Sub B of Sub B of Main

----Child Sub C of Sub B of Main

-- Sub C of Main

----Child Sub A of Sub C of Main

----Child Sub B of Sub C of Main

----Child Sub C of Sub C of Main]]></description>
		<content:encoded><![CDATA[<p>I have a CTE Table Category and Sub Category With ID,Parent ID relation.</p>
<p>I Wana to retrieve all Nested Sub Categories In Nested Ordered Manner.</p>
<p>Some one Help me soon The Result Shuld be in Following Manners.</p>
<p>Main Cat</p>
<p>&#8211; Sub A of Main</p>
<p>&#8212;-Child Sub A of Sub A of Main</p>
<p>&#8212;-Child Sub B of Sub A of Main</p>
<p>&#8212;-Child Sub C of Sub A of Main</p>
<p>&#8211; Sub B of Main</p>
<p>&#8212;-Child Sub A of Sub B of Main</p>
<p>&#8212;-Child Sub B of Sub B of Main</p>
<p>&#8212;-Child Sub C of Sub B of Main</p>
<p>&#8211; Sub C of Main</p>
<p>&#8212;-Child Sub A of Sub C of Main</p>
<p>&#8212;-Child Sub B of Sub C of Main</p>
<p>&#8212;-Child Sub C of Sub C of Main</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Govind</title>
		<link>http://blog.sqlauthority.com/2008/07/29/sql-server-sql-server-simple-example-of-recursive-cte-part-2-maxrecursion-prevent-cte-infinite-loop/#comment-303044</link>
		<dc:creator><![CDATA[Govind]]></dc:creator>
		<pubDate>Tue, 19 Jun 2012 05:21:31 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=751#comment-303044</guid>
		<description><![CDATA[@Prakash,

there is mention &quot;OPTION (MAXRECURSION 5)&quot;  so it will throw error beyond 5th recursion, while you have made max recursion to 200, so it will throw error after 200th recursion..]]></description>
		<content:encoded><![CDATA[<p>@Prakash,</p>
<p>there is mention &#8220;OPTION (MAXRECURSION 5)&#8221;  so it will throw error beyond 5th recursion, while you have made max recursion to 200, so it will throw error after 200th recursion..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: madhivanan</title>
		<link>http://blog.sqlauthority.com/2008/07/29/sql-server-sql-server-simple-example-of-recursive-cte-part-2-maxrecursion-prevent-cte-infinite-loop/#comment-302778</link>
		<dc:creator><![CDATA[madhivanan]]></dc:creator>
		<pubDate>Mon, 18 Jun 2012 11:01:09 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=751#comment-302778</guid>
		<description><![CDATA[You need to set it to 0]]></description>
		<content:encoded><![CDATA[<p>You need to set it to 0</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Prakash</title>
		<link>http://blog.sqlauthority.com/2008/07/29/sql-server-sql-server-simple-example-of-recursive-cte-part-2-maxrecursion-prevent-cte-infinite-loop/#comment-297521</link>
		<dc:creator><![CDATA[Prakash]]></dc:creator>
		<pubDate>Fri, 08 Jun 2012 14:02:15 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=751#comment-297521</guid>
		<description><![CDATA[It mentioned that &quot;Now if your CTE goes beyond 5th recursion it will throw an error and stop executing.&quot; can you please elaborate a bit as I have made max recursion 200 but didn&#039;t get any error..thnx]]></description>
		<content:encoded><![CDATA[<p>It mentioned that &#8220;Now if your CTE goes beyond 5th recursion it will throw an error and stop executing.&#8221; can you please elaborate a bit as I have made max recursion 200 but didn&#8217;t get any error..thnx</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQL SERVER &#8211; Convert Subquery to CTE &#8211; SQL in Sixty Seconds #001 &#8211; Video &#171; SQL Server Journey with SQL Authority</title>
		<link>http://blog.sqlauthority.com/2008/07/29/sql-server-sql-server-simple-example-of-recursive-cte-part-2-maxrecursion-prevent-cte-infinite-loop/#comment-249752</link>
		<dc:creator><![CDATA[SQL SERVER &#8211; Convert Subquery to CTE &#8211; SQL in Sixty Seconds #001 &#8211; Video &#171; SQL Server Journey with SQL Authority]]></dc:creator>
		<pubDate>Wed, 08 Feb 2012 01:32:14 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=751#comment-249752</guid>
		<description><![CDATA[[...] One SELECT Statement Query Common Table Expression (CTE) and Few Observation Delete Duplicate Rows Simple Example of Recursive CTE – Part 2 – MAXRECURSION – Prevent CTE Infinite Loop T-SQL Paging Query Technique Comparison (OVER and ROW_NUMBER()) – CTE vs. Derived [...]]]></description>
		<content:encoded><![CDATA[<p>[...] One SELECT Statement Query Common Table Expression (CTE) and Few Observation Delete Duplicate Rows Simple Example of Recursive CTE – Part 2 – MAXRECURSION – Prevent CTE Infinite Loop T-SQL Paging Query Technique Comparison (OVER and ROW_NUMBER()) – CTE vs. Derived [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQL SERVER &#8211; Common Gotcha&#8217;s Associated with Common Table Expressions (CTE) &#8211; Quiz &#8211; Puzzle &#8211; 26 of 31 &#171; SQL Server Journey with SQL Authority</title>
		<link>http://blog.sqlauthority.com/2008/07/29/sql-server-sql-server-simple-example-of-recursive-cte-part-2-maxrecursion-prevent-cte-infinite-loop/#comment-244329</link>
		<dc:creator><![CDATA[SQL SERVER &#8211; Common Gotcha&#8217;s Associated with Common Table Expressions (CTE) &#8211; Quiz &#8211; Puzzle &#8211; 26 of 31 &#171; SQL Server Journey with SQL Authority]]></dc:creator>
		<pubDate>Fri, 27 Jan 2012 01:31:26 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=751#comment-244329</guid>
		<description><![CDATA[[...] Multiple CTE in One SELECT Statement Query Delete Duplicate Rows Simple Example of Recursive CTE SQL SERVER – Simple Example of Recursive CTE – Part 2 – MAXRECURSION – Prevent CTE Infinite ... T-SQL Paging Query Technique Comparison (OVER and ROW_NUMBER()) – CTE vs. Derived [...]]]></description>
		<content:encoded><![CDATA[<p>[...] Multiple CTE in One SELECT Statement Query Delete Duplicate Rows Simple Example of Recursive CTE SQL SERVER – Simple Example of Recursive CTE – Part 2 – MAXRECURSION – Prevent CTE Infinite &#8230; T-SQL Paging Query Technique Comparison (OVER and ROW_NUMBER()) – CTE vs. Derived [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dabbas</title>
		<link>http://blog.sqlauthority.com/2008/07/29/sql-server-sql-server-simple-example-of-recursive-cte-part-2-maxrecursion-prevent-cte-infinite-loop/#comment-199407</link>
		<dc:creator><![CDATA[Dabbas]]></dc:creator>
		<pubDate>Sat, 19 Nov 2011 18:07:56 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=751#comment-199407</guid>
		<description><![CDATA[Hi pinaldave
It&#039;s great article and very helpful.
I&#039;ve question.
I&#039;ve sub group and trying to get all the parents so I tried this:
==================================
	WITH Groups_CTE AS (
	SELECT SubG.ParentID,SubG.Name FROM [Groups] AS SubG WHERE SubG.ID = 1
	UNION ALL
	SELECT ParentG.ID,ParentG.Name FROM Groups AS ParentG INNER JOIN Groups_CTE ecte ON ecte.ParentID = ParentG.ID
	)
	
	SELECT * FROM Groups_CTE
==================================
But it didn&#039;t work, it thow exception says:
&quot;The statement terminated. The maximum recursion 100 has been exhausted before statement completion.&quot;

What should I do to make it work properly ?

Thanx]]></description>
		<content:encoded><![CDATA[<p>Hi pinaldave<br />
It&#8217;s great article and very helpful.<br />
I&#8217;ve question.<br />
I&#8217;ve sub group and trying to get all the parents so I tried this:<br />
==================================<br />
	WITH Groups_CTE AS (<br />
	SELECT SubG.ParentID,SubG.Name FROM [Groups] AS SubG WHERE SubG.ID = 1<br />
	UNION ALL<br />
	SELECT ParentG.ID,ParentG.Name FROM Groups AS ParentG INNER JOIN Groups_CTE ecte ON ecte.ParentID = ParentG.ID<br />
	)</p>
<p>	SELECT * FROM Groups_CTE<br />
==================================<br />
But it didn&#8217;t work, it thow exception says:<br />
&#8220;The statement terminated. The maximum recursion 100 has been exhausted before statement completion.&#8221;</p>
<p>What should I do to make it work properly ?</p>
<p>Thanx</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQL SERVER – Common Table Expression (CTE) and Few Observation Journey to SQLAuthority</title>
		<link>http://blog.sqlauthority.com/2008/07/29/sql-server-sql-server-simple-example-of-recursive-cte-part-2-maxrecursion-prevent-cte-infinite-loop/#comment-133698</link>
		<dc:creator><![CDATA[SQL SERVER – Common Table Expression (CTE) and Few Observation Journey to SQLAuthority]]></dc:creator>
		<pubDate>Tue, 10 May 2011 02:17:21 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=751#comment-133698</guid>
		<description><![CDATA[[...] SQL SERVER – SQL SERVER – Simple Example of Recursive CTE – Part 2 – MAXRECURSION – Preven... [...]]]></description>
		<content:encoded><![CDATA[<p>[...] SQL SERVER – SQL SERVER – Simple Example of Recursive CTE – Part 2 – MAXRECURSION – Preven&#8230; [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brijesh Shah</title>
		<link>http://blog.sqlauthority.com/2008/07/29/sql-server-sql-server-simple-example-of-recursive-cte-part-2-maxrecursion-prevent-cte-infinite-loop/#comment-102648</link>
		<dc:creator><![CDATA[Brijesh Shah]]></dc:creator>
		<pubDate>Sun, 28 Nov 2010 15:43:22 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=751#comment-102648</guid>
		<description><![CDATA[Hi there!

I need help with recursive operation like


Parents 1
---------Chield1
---------Chield2
                       ------Sub -Chield1
                       ------Sub -Chield2
Parents 2
Parents3
---------Chield1
---------Chield2

I want to display data exactly like  above example with &#039;-&#039; operator.
Please provide an query.
thanks &amp; regards
Brijesh Shah]]></description>
		<content:encoded><![CDATA[<p>Hi there!</p>
<p>I need help with recursive operation like</p>
<p>Parents 1<br />
&#8212;&#8212;&#8212;Chield1<br />
&#8212;&#8212;&#8212;Chield2<br />
                       &#8212;&#8212;Sub -Chield1<br />
                       &#8212;&#8212;Sub -Chield2<br />
Parents 2<br />
Parents3<br />
&#8212;&#8212;&#8212;Chield1<br />
&#8212;&#8212;&#8212;Chield2</p>
<p>I want to display data exactly like  above example with &#8216;-&#8217; operator.<br />
Please provide an query.<br />
thanks &amp; regards<br />
Brijesh Shah</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marko Parkkola</title>
		<link>http://blog.sqlauthority.com/2008/07/29/sql-server-sql-server-simple-example-of-recursive-cte-part-2-maxrecursion-prevent-cte-infinite-loop/#comment-69040</link>
		<dc:creator><![CDATA[Marko Parkkola]]></dc:creator>
		<pubDate>Tue, 04 May 2010 17:32:37 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=751#comment-69040</guid>
		<description><![CDATA[Well, of course. Just keep a counter which tells how deep in the recursion you are and stop there. For example like the following (which is taken and modified from example in my blog). 

Take notice that &#039;lvl&#039; field there which tells how deep we are. In recursion part I check that lvl &lt; 2 and recursion stops there if it ever goes so deep.



WITH 
cte(emp_id, emp_name, sup_id, sup_name, lvl) AS
(
           SELECT e.emp_id, e.emp_name, s.emp_id, s.emp_name, 1
           FROM t_employee e
           LEFT OUTER JOIN t_employee s ON s.emp_id = e.supervisor_id
),
cte_start(emp_id, emp_name, sup_id, sup_name, lvl) AS
(
           SELECT e.emp_id, e.emp_name, s.emp_id, s.emp_name, 1
           FROM t_employee e
           JOIN t_employee s ON s.emp_id = e.supervisor_id
           LEFT OUTER JOIN t_employee sub ON sub.supervisor_id = e.emp_id
           WHERE sub.emp_id IS NULL
),
cte_recur(emp_id, emp_name, sup_id, sup_name, lvl) AS
(
           SELECT emp_id, emp_name, sup_id, sup_name, lvl
           FROM cte_start
           UNION ALL
           SELECT c.emp_id, c.emp_name, c.sup_id, c.sup_name, r.lvl + 1
           FROM cte c
           JOIN cte_recur r ON r.sup_id = c.emp_id
           where r.lvl &lt; 2
)
SELECT DISTINCT emp_name &#039;Employee name&#039;, sup_name &#039;Supervisor name&#039;, lvl 
FROM cte_recur]]></description>
		<content:encoded><![CDATA[<p>Well, of course. Just keep a counter which tells how deep in the recursion you are and stop there. For example like the following (which is taken and modified from example in my blog). </p>
<p>Take notice that &#8216;lvl&#8217; field there which tells how deep we are. In recursion part I check that lvl &lt; 2 and recursion stops there if it ever goes so deep.</p>
<p>WITH<br />
cte(emp_id, emp_name, sup_id, sup_name, lvl) AS<br />
(<br />
           SELECT e.emp_id, e.emp_name, s.emp_id, s.emp_name, 1<br />
           FROM t_employee e<br />
           LEFT OUTER JOIN t_employee s ON s.emp_id = e.supervisor_id<br />
),<br />
cte_start(emp_id, emp_name, sup_id, sup_name, lvl) AS<br />
(<br />
           SELECT e.emp_id, e.emp_name, s.emp_id, s.emp_name, 1<br />
           FROM t_employee e<br />
           JOIN t_employee s ON s.emp_id = e.supervisor_id<br />
           LEFT OUTER JOIN t_employee sub ON sub.supervisor_id = e.emp_id<br />
           WHERE sub.emp_id IS NULL<br />
),<br />
cte_recur(emp_id, emp_name, sup_id, sup_name, lvl) AS<br />
(<br />
           SELECT emp_id, emp_name, sup_id, sup_name, lvl<br />
           FROM cte_start<br />
           UNION ALL<br />
           SELECT c.emp_id, c.emp_name, c.sup_id, c.sup_name, r.lvl + 1<br />
           FROM cte c<br />
           JOIN cte_recur r ON r.sup_id = c.emp_id<br />
           where r.lvl &lt; 2<br />
)<br />
SELECT DISTINCT emp_name &#039;Employee name&#039;, sup_name &#039;Supervisor name&#039;, lvl<br />
FROM cte_recur</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Luke</title>
		<link>http://blog.sqlauthority.com/2008/07/29/sql-server-sql-server-simple-example-of-recursive-cte-part-2-maxrecursion-prevent-cte-infinite-loop/#comment-68872</link>
		<dc:creator><![CDATA[Luke]]></dc:creator>
		<pubDate>Tue, 04 May 2010 11:39:22 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=751#comment-68872</guid>
		<description><![CDATA[I think you can set a where clause on the level.

UNION ALL 

SELECT
 whatever...
WHERE
  menulevel &lt; 5]]></description>
		<content:encoded><![CDATA[<p>I think you can set a where clause on the level.</p>
<p>UNION ALL </p>
<p>SELECT<br />
 whatever&#8230;<br />
WHERE<br />
  menulevel &lt; 5</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Bailey</title>
		<link>http://blog.sqlauthority.com/2008/07/29/sql-server-sql-server-simple-example-of-recursive-cte-part-2-maxrecursion-prevent-cte-infinite-loop/#comment-68797</link>
		<dc:creator><![CDATA[John Bailey]]></dc:creator>
		<pubDate>Tue, 04 May 2010 07:46:31 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=751#comment-68797</guid>
		<description><![CDATA[I was hoping to restrict the recursion without throwing an error.  Is there a way to get it to just stop at lets say 5 levels deep without throwing an error?]]></description>
		<content:encoded><![CDATA[<p>I was hoping to restrict the recursion without throwing an error.  Is there a way to get it to just stop at lets say 5 levels deep without throwing an error?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Luke Williamson</title>
		<link>http://blog.sqlauthority.com/2008/07/29/sql-server-sql-server-simple-example-of-recursive-cte-part-2-maxrecursion-prevent-cte-infinite-loop/#comment-64238</link>
		<dc:creator><![CDATA[Luke Williamson]]></dc:creator>
		<pubDate>Thu, 01 Apr 2010 14:33:00 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=751#comment-64238</guid>
		<description><![CDATA[I&#039;m creating a .net cms application. I want a user to be able to build a menu by select multiple parent menu items. Each menu item can have two levels of inheritance.

Using cte I can get the tree structure for the menu, however I want to be able to insert the tree structure with positioning. As users with also be able to change the positioning of the menu items.

As under the news sub menu World news should be before UK news.

Any help and guidance will be greatly appreciated.

Is this even possible using CTE?

DECLARE @menu TABLE
(
id int, parentId int, title varchar(128)
)


INSERT @menu 
SELECT 1,NULL,&#039;Home Page&#039; UNION ALL 
SELECT 2,1,&#039;News&#039; UNION ALL 
SELECT 3,2,&#039;World&#039; UNION ALL
SELECT 4,2,&#039;UK&#039; UNION ALL 
SELECT 5,2,&#039;Business&#039; UNION ALL
SELECT 6,1,&#039;Sports&#039; UNION ALL
SELECT 7,6, &#039;Football&#039; UNION ALL
SELECT 8,6,&#039;Tennis&#039; UNION ALL
SELECT 9,6, &#039;Formula One&#039; UNION ALL
SELECT 10,6, &#039;Boxing&#039; UNION ALL
SELECT 11,2, &#039;Politics&#039; UNION ALL
SELECT 12,6, &#039;Cricket&#039;


;WITH MenuTree AS
(    
	SELECT
		M.id,
		M.parentId,
		M.title,
		0 AS menuLevel,
		Cast(M.title + &#039;\\&#039; AS varchar(max)) AS treePath,
		null AS position
	FROM
		@menu AS M
	WHERE
		M.id = 1

UNION ALL        

	SELECT	
		M.id,
		M.parentId,
		M.title,
		menuLevel + 1,
		Cast(MenuTree.treePath + M.title + &#039;\\&#039; AS varchar(max)) AS treePath,
		null AS position
FROM @menu AS M    
INNER JOIN MenuTree    ON M.ParentID = MenuTree.id
)

SELECT * FROM menuTree order by treePath

how do i get the positioning...

id - title   - position
1    Home Page - 1
2    News          - 1
5    Business     - 2
11  Politics        -  3
4    UK              -  4
3    World         -  5 
6    Sports        - 2
10  Boxing        - 1
12  Cricket        - 2
7    Football      -  3
9    Formula One - 4
8    Tennis           -5

Thanks for your time.]]></description>
		<content:encoded><![CDATA[<p>I&#8217;m creating a .net cms application. I want a user to be able to build a menu by select multiple parent menu items. Each menu item can have two levels of inheritance.</p>
<p>Using cte I can get the tree structure for the menu, however I want to be able to insert the tree structure with positioning. As users with also be able to change the positioning of the menu items.</p>
<p>As under the news sub menu World news should be before UK news.</p>
<p>Any help and guidance will be greatly appreciated.</p>
<p>Is this even possible using CTE?</p>
<p>DECLARE @menu TABLE<br />
(<br />
id int, parentId int, title varchar(128)<br />
)</p>
<p>INSERT @menu<br />
SELECT 1,NULL,&#8217;Home Page&#8217; UNION ALL<br />
SELECT 2,1,&#8217;News&#8217; UNION ALL<br />
SELECT 3,2,&#8217;World&#8217; UNION ALL<br />
SELECT 4,2,&#8217;UK&#8217; UNION ALL<br />
SELECT 5,2,&#8217;Business&#8217; UNION ALL<br />
SELECT 6,1,&#8217;Sports&#8217; UNION ALL<br />
SELECT 7,6, &#8216;Football&#8217; UNION ALL<br />
SELECT 8,6,&#8217;Tennis&#8217; UNION ALL<br />
SELECT 9,6, &#8216;Formula One&#8217; UNION ALL<br />
SELECT 10,6, &#8216;Boxing&#8217; UNION ALL<br />
SELECT 11,2, &#8216;Politics&#8217; UNION ALL<br />
SELECT 12,6, &#8216;Cricket&#8217;</p>
<p>;WITH MenuTree AS<br />
(<br />
	SELECT<br />
		M.id,<br />
		M.parentId,<br />
		M.title,<br />
		0 AS menuLevel,<br />
		Cast(M.title + &#8216;\\&#8217; AS varchar(max)) AS treePath,<br />
		null AS position<br />
	FROM<br />
		@menu AS M<br />
	WHERE<br />
		M.id = 1</p>
<p>UNION ALL        </p>
<p>	SELECT<br />
		M.id,<br />
		M.parentId,<br />
		M.title,<br />
		menuLevel + 1,<br />
		Cast(MenuTree.treePath + M.title + &#8216;\\&#8217; AS varchar(max)) AS treePath,<br />
		null AS position<br />
FROM @menu AS M<br />
INNER JOIN MenuTree    ON M.ParentID = MenuTree.id<br />
)</p>
<p>SELECT * FROM menuTree order by treePath</p>
<p>how do i get the positioning&#8230;</p>
<p>id &#8211; title   &#8211; position<br />
1    Home Page &#8211; 1<br />
2    News          &#8211; 1<br />
5    Business     &#8211; 2<br />
11  Politics        &#8211;  3<br />
4    UK              &#8211;  4<br />
3    World         &#8211;  5<br />
6    Sports        &#8211; 2<br />
10  Boxing        &#8211; 1<br />
12  Cricket        &#8211; 2<br />
7    Football      &#8211;  3<br />
9    Formula One &#8211; 4<br />
8    Tennis           -5</p>
<p>Thanks for your time.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: katie</title>
		<link>http://blog.sqlauthority.com/2008/07/29/sql-server-sql-server-simple-example-of-recursive-cte-part-2-maxrecursion-prevent-cte-infinite-loop/#comment-62888</link>
		<dc:creator><![CDATA[katie]]></dc:creator>
		<pubDate>Sat, 13 Mar 2010 20:13:05 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=751#comment-62888</guid>
		<description><![CDATA[Btw, you need to replace the slanted single quotes to match the straight single quotes if you try to copy and paste the query into SSMS.]]></description>
		<content:encoded><![CDATA[<p>Btw, you need to replace the slanted single quotes to match the straight single quotes if you try to copy and paste the query into SSMS.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: katie</title>
		<link>http://blog.sqlauthority.com/2008/07/29/sql-server-sql-server-simple-example-of-recursive-cte-part-2-maxrecursion-prevent-cte-infinite-loop/#comment-62887</link>
		<dc:creator><![CDATA[katie]]></dc:creator>
		<pubDate>Sat, 13 Mar 2010 20:11:51 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=751#comment-62887</guid>
		<description><![CDATA[I tried this example, but it didn&#039;t go into the recursive statements. I modified it to parse the entire string and return each element of the string.

DECLARE @txt as nvarchar(100)
SET @txt = &#039;Banana;Chocolate;Milk;Bread;&#039;;

WITH ParseText( Ltxt, Rtxt) AS (

SELECT 
Ltxt = LEFT (@txt, CHARINDEX( &#039;;&#039;, @txt)-1 )
,Rtxt = RIGHT(@txt, LEN(@txt) - CHARINDEX( &#039;;&#039;,@txt) )

UNION ALL 
SELECT
Ltxt = LEFT(tn.Rtxt, CHARINDEX( &#039;;&#039;,tn.Rtxt)-1 )
,Rtxt = RIGHT(tn.Rtxt, LEN(tn.Rtxt) - CHARINDEX( &#039;;&#039;,tn.Rtxt) )
FROM ParseText tn 
where Rtxt  &#039;&#039;
)
SELECT
Ltxt = CASE WHEN Ltxt = &#039;&#039; THEN RTxt ELSE Ltxt END
FROM ParseText]]></description>
		<content:encoded><![CDATA[<p>I tried this example, but it didn&#8217;t go into the recursive statements. I modified it to parse the entire string and return each element of the string.</p>
<p>DECLARE @txt as nvarchar(100)<br />
SET @txt = &#8216;Banana;Chocolate;Milk;Bread;&#8217;;</p>
<p>WITH ParseText( Ltxt, Rtxt) AS (</p>
<p>SELECT<br />
Ltxt = LEFT (@txt, CHARINDEX( &#8216;;&#8217;, @txt)-1 )<br />
,Rtxt = RIGHT(@txt, LEN(@txt) &#8211; CHARINDEX( &#8216;;&#8217;,@txt) )</p>
<p>UNION ALL<br />
SELECT<br />
Ltxt = LEFT(tn.Rtxt, CHARINDEX( &#8216;;&#8217;,tn.Rtxt)-1 )<br />
,Rtxt = RIGHT(tn.Rtxt, LEN(tn.Rtxt) &#8211; CHARINDEX( &#8216;;&#8217;,tn.Rtxt) )<br />
FROM ParseText tn<br />
where Rtxt  &#8221;<br />
)<br />
SELECT<br />
Ltxt = CASE WHEN Ltxt = &#8221; THEN RTxt ELSE Ltxt END<br />
FROM ParseText</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Niranjan</title>
		<link>http://blog.sqlauthority.com/2008/07/29/sql-server-sql-server-simple-example-of-recursive-cte-part-2-maxrecursion-prevent-cte-infinite-loop/#comment-62789</link>
		<dc:creator><![CDATA[Niranjan]]></dc:creator>
		<pubDate>Fri, 12 Mar 2010 05:54:37 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=751#comment-62789</guid>
		<description><![CDATA[Hello Pinal Dave,

I have a problem with MAXRECURSION hint of CTE query
below
declare @id int
set @id=1;

with cte(member_id,lft,rgt) as (
select member_id,0,0
from [MultilevelChain].[dbo].[member_info]
where member_id=@id 

union all

select t.member_id,1-t.position, t.position
from [MultilevelChain].[dbo].[member_info] t
inner join cte c on c.member_id = t.sponser_id)
select sum(lft) as LeftNode,
       sum(rgt) as RightNode

from cte 
option (maxrecursion 32767)

it gives error message like
Msg 530, Level 16, State 1, Line 5
The statement terminated. The maximum recursion 32767 has been exhausted before statement completion.


Here all below levels are searched but problem with top levelit has attach with 6 node under it. all levels are ok but not show id as 1 pls help me. i waitig to ur reply

Tanks regards
Niranjan Singh Pune]]></description>
		<content:encoded><![CDATA[<p>Hello Pinal Dave,</p>
<p>I have a problem with MAXRECURSION hint of CTE query<br />
below<br />
declare @id int<br />
set @id=1;</p>
<p>with cte(member_id,lft,rgt) as (<br />
select member_id,0,0<br />
from [MultilevelChain].[dbo].[member_info]<br />
where member_id=@id </p>
<p>union all</p>
<p>select t.member_id,1-t.position, t.position<br />
from [MultilevelChain].[dbo].[member_info] t<br />
inner join cte c on c.member_id = t.sponser_id)<br />
select sum(lft) as LeftNode,<br />
       sum(rgt) as RightNode</p>
<p>from cte<br />
option (maxrecursion 32767)</p>
<p>it gives error message like<br />
Msg 530, Level 16, State 1, Line 5<br />
The statement terminated. The maximum recursion 32767 has been exhausted before statement completion.</p>
<p>Here all below levels are searched but problem with top levelit has attach with 6 node under it. all levels are ok but not show id as 1 pls help me. i waitig to ur reply</p>
<p>Tanks regards<br />
Niranjan Singh Pune</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paresh Prajapati</title>
		<link>http://blog.sqlauthority.com/2008/07/29/sql-server-sql-server-simple-example-of-recursive-cte-part-2-maxrecursion-prevent-cte-infinite-loop/#comment-61311</link>
		<dc:creator><![CDATA[Paresh Prajapati]]></dc:creator>
		<pubDate>Wed, 17 Feb 2010 06:40:36 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=751#comment-61311</guid>
		<description><![CDATA[Hello Pinal,

It is really working.

Thanks a lot.]]></description>
		<content:encoded><![CDATA[<p>Hello Pinal,</p>
<p>It is really working.</p>
<p>Thanks a lot.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pinal Dave</title>
		<link>http://blog.sqlauthority.com/2008/07/29/sql-server-sql-server-simple-example-of-recursive-cte-part-2-maxrecursion-prevent-cte-infinite-loop/#comment-61288</link>
		<dc:creator><![CDATA[Pinal Dave]]></dc:creator>
		<pubDate>Tue, 16 Feb 2010 18:13:44 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=751#comment-61288</guid>
		<description><![CDATA[Hi Paresh,

Set the MAXRECURSION hint of CTE query to any nonnegative value between 0 and 32767 as below:

SELECT EmployeeID, ManagerID, Title
FROM cteEmployee
OPTION (MAXRECURSION 200);

When 0 is specified, no limit is applied. If this option is not specified, the default limit is 100. 

Regards,
Pinal Dave]]></description>
		<content:encoded><![CDATA[<p>Hi Paresh,</p>
<p>Set the MAXRECURSION hint of CTE query to any nonnegative value between 0 and 32767 as below:</p>
<p>SELECT EmployeeID, ManagerID, Title<br />
FROM cteEmployee<br />
OPTION (MAXRECURSION 200);</p>
<p>When 0 is specified, no limit is applied. If this option is not specified, the default limit is 100. </p>
<p>Regards,<br />
Pinal Dave</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paresh Prajapati</title>
		<link>http://blog.sqlauthority.com/2008/07/29/sql-server-sql-server-simple-example-of-recursive-cte-part-2-maxrecursion-prevent-cte-infinite-loop/#comment-61220</link>
		<dc:creator><![CDATA[Paresh Prajapati]]></dc:creator>
		<pubDate>Tue, 16 Feb 2010 06:48:05 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=751#comment-61220</guid>
		<description><![CDATA[Hi Pinal, 


How can we avoid the error 
&quot;Msg 530, Level 16, State 1, Line 1
The statement terminated. The maximum recursion 3 has been exhausted before statement completion. &quot;

if cte goes above 100 levels, then what need to do?]]></description>
		<content:encoded><![CDATA[<p>Hi Pinal, </p>
<p>How can we avoid the error<br />
&#8220;Msg 530, Level 16, State 1, Line 1<br />
The statement terminated. The maximum recursion 3 has been exhausted before statement completion. &#8221;</p>
<p>if cte goes above 100 levels, then what need to do?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alain Holloway</title>
		<link>http://blog.sqlauthority.com/2008/07/29/sql-server-sql-server-simple-example-of-recursive-cte-part-2-maxrecursion-prevent-cte-infinite-loop/#comment-57557</link>
		<dc:creator><![CDATA[Alain Holloway]]></dc:creator>
		<pubDate>Thu, 12 Nov 2009 16:39:20 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=751#comment-57557</guid>
		<description><![CDATA[Hi there!

I need help with recursive operation

I do have 2 tables

#1 Inspections
ID
PARENT_ID
POSITION

#2 Resources
RESOURCES_ID
CULTURE_LANGUAGE
CAPTION

DATA SAMPLE (OUTPUT)

SELECT a.id, a.parent_id, a.position, b.culture_language, b.caption 
FROM Inspections AS a 
INNER JOIN InspectionsResources AS b ON 
a.id = b.resource_id where culture_language=&#039;en&#039;

VEHICLES        ID = 1, PARENT_ID = 0, POSITION=1
FORKLIFTS       ID = 2, PARENT_ID = 0, POSITION=2
CARS               ID = 3, PARENT_ID = 1, POSITION=1
MOTORCYCLES ID = 4, PARENT_ID = 1, POSITION=2
DOORS            ID = 5, PARENT_ID = 3, POSITION=1
HANDLES          ID = 6, PARENT_ID = 3, POSITION=2
...

I need to load all data for CARS only!!

Thank you Pinal

Alain]]></description>
		<content:encoded><![CDATA[<p>Hi there!</p>
<p>I need help with recursive operation</p>
<p>I do have 2 tables</p>
<p>#1 Inspections<br />
ID<br />
PARENT_ID<br />
POSITION</p>
<p>#2 Resources<br />
RESOURCES_ID<br />
CULTURE_LANGUAGE<br />
CAPTION</p>
<p>DATA SAMPLE (OUTPUT)</p>
<p>SELECT a.id, a.parent_id, a.position, b.culture_language, b.caption<br />
FROM Inspections AS a<br />
INNER JOIN InspectionsResources AS b ON<br />
a.id = b.resource_id where culture_language=&#8217;en&#8217;</p>
<p>VEHICLES        ID = 1, PARENT_ID = 0, POSITION=1<br />
FORKLIFTS       ID = 2, PARENT_ID = 0, POSITION=2<br />
CARS               ID = 3, PARENT_ID = 1, POSITION=1<br />
MOTORCYCLES ID = 4, PARENT_ID = 1, POSITION=2<br />
DOORS            ID = 5, PARENT_ID = 3, POSITION=1<br />
HANDLES          ID = 6, PARENT_ID = 3, POSITION=2<br />
&#8230;</p>
<p>I need to load all data for CARS only!!</p>
<p>Thank you Pinal</p>
<p>Alain</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Tkatch</title>
		<link>http://blog.sqlauthority.com/2008/07/29/sql-server-sql-server-simple-example-of-recursive-cte-part-2-maxrecursion-prevent-cte-infinite-loop/#comment-57071</link>
		<dc:creator><![CDATA[Brian Tkatch]]></dc:creator>
		<pubDate>Tue, 27 Oct 2009 13:21:26 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=751#comment-57071</guid>
		<description><![CDATA[@Raghuveer

A recursive CTE can potentially go on forever. So, SQL Server limits it to 100 cycles. MAXRECURSION allows you to change that limit between 1 and 32767 or 0 which means unlimited.]]></description>
		<content:encoded><![CDATA[<p>@Raghuveer</p>
<p>A recursive CTE can potentially go on forever. So, SQL Server limits it to 100 cycles. MAXRECURSION allows you to change that limit between 1 and 32767 or 0 which means unlimited.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Raghuveer</title>
		<link>http://blog.sqlauthority.com/2008/07/29/sql-server-sql-server-simple-example-of-recursive-cte-part-2-maxrecursion-prevent-cte-infinite-loop/#comment-57055</link>
		<dc:creator><![CDATA[Raghuveer]]></dc:creator>
		<pubDate>Tue, 27 Oct 2009 02:41:40 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=751#comment-57055</guid>
		<description><![CDATA[Hi Pinal,

I am student learning SQL server. I actaully didnot know what exactly maxrecursion function do?

can you please be precise.

Thanks&amp;Regards,

Raghuveer.]]></description>
		<content:encoded><![CDATA[<p>Hi Pinal,</p>
<p>I am student learning SQL server. I actaully didnot know what exactly maxrecursion function do?</p>
<p>can you please be precise.</p>
<p>Thanks&amp;Regards,</p>
<p>Raghuveer.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ravi kiran</title>
		<link>http://blog.sqlauthority.com/2008/07/29/sql-server-sql-server-simple-example-of-recursive-cte-part-2-maxrecursion-prevent-cte-infinite-loop/#comment-54846</link>
		<dc:creator><![CDATA[Ravi kiran]]></dc:creator>
		<pubDate>Fri, 14 Aug 2009 06:57:11 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=751#comment-54846</guid>
		<description><![CDATA[Hi pinal,

Thank you for the content, i was looking for the same.]]></description>
		<content:encoded><![CDATA[<p>Hi pinal,</p>
<p>Thank you for the content, i was looking for the same.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Tkatch</title>
		<link>http://blog.sqlauthority.com/2008/07/29/sql-server-sql-server-simple-example-of-recursive-cte-part-2-maxrecursion-prevent-cte-infinite-loop/#comment-50305</link>
		<dc:creator><![CDATA[Brian Tkatch]]></dc:creator>
		<pubDate>Tue, 31 Mar 2009 19:58:38 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=751#comment-50305</guid>
		<description><![CDATA[@Prasant

Please provide an example query.]]></description>
		<content:encoded><![CDATA[<p>@Prasant</p>
<p>Please provide an example query.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
