Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 MAXRECURSION option in CTE

Author  Topic 

shan
Yak Posting Veteran

84 Posts

Posted - 2009-12-29 : 10:02:25
Hi Can some one help me understand OPTION (MAXRECURSION 2); in common table expression, I am trying to understand from SQL help but i am not getting it.

-Shan

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-12-29 : 10:07:00
it limits the recursion of the CTE to 2 levels. From the bundled documentation:
quote:

G. Using MAXRECURSION to cancel a statement
MAXRECURSION can be used to prevent a poorly formed recursive CTE from entering into an infinite loop. The following example intentionally creates an infinite loop and uses the MAXRECURSION hint to limit the number of recursion levels to two.

Copy Code
USE AdventureWorks;
GO
--Creates an infinite loop
WITH cte (EmployeeID, ManagerID, Title) as
(
SELECT EmployeeID, ManagerID, Title
FROM HumanResources.Employee
WHERE ManagerID IS NOT NULL
UNION ALL
SELECT cte.EmployeeID, cte.ManagerID, cte.Title
FROM cte
JOIN HumanResources.Employee AS e
ON cte.ManagerID = e.EmployeeID
)
--Uses MAXRECURSION to limit the recursive levels to 2
SELECT EmployeeID, ManagerID, Title
FROM cte
OPTION (MAXRECURSION 2);
GO


After the coding error is corrected, MAXRECURSION is no longer required. The following example shows the corrected code.





Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION


Here the forced MAXRECURSION limits the number of times the UNION ALL operation is performed (to 2)
Go to Top of Page

shan
Yak Posting Veteran

84 Posts

Posted - 2009-12-29 : 10:14:24
Sorry i am still not clear what it means, Does it limits the number of rows it returning?

-Shan
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-12-29 : 10:35:29
No. It limits the number of times the Recursive definitions is called. You can play with this:

DECLARE @Geneology TABLE (
[personID] INT PRIMARY KEY
, [surname] VARCHAR(255)
, [firstname] VARCHAR(255)
, [parentID] INT
)

INSERT @Geneology ([personID], [surname], [firstname], [parentID])
SELECT 1, 'Smith', 'Fred', NULL -- Fred Smith
UNION SELECT 2, 'Smith', 'David', 1 -- David, Son of Fred
UNION SELECT 3, 'Smith', 'Judith', 2 -- Judith, Daughter of David
UNION SELECT 4, 'Jones', 'Mary', 3 -- Mary, Daughter of Judith
UNION SELECT 10, 'Brown', 'Harry' , NULL -- Harry Brown
UNION SELECT 11, 'Brown', 'Charles', 10 -- Charles, Son of Harry

-- Family Tree CTE (simplified)
; WITH family (
[personId]
, [Surname]
, [Firstname]
, [paternaty Path]
, [parentID]
, [level]
)
AS (
-- Anchor Definition (Gets the starts of all families where the parent is not known)
SELECT
[personID]
, [surname]
, [firstname]
, CAST([firstname] + ' ' + [surname] AS VARCHAR(MAX))
, [parentID]
, 1
FROM
@geneology
WHERE
[parentID] IS NULL
-- Recursive Definition
UNION ALL SELECT
g.[personID]
, g.[surname]
, g.[firstname]
, f.[paternaty Path] + ' -> ' + CAST(g.[firstname] + ' ' + g.[surname] AS VARCHAR(MAX))
, g.[parentID]
, f.[level] + 1
FROM
@geneology g
JOIN family f ON f.[personID] = g.[parentID]
)
SELECT * FROM family OPTION (MAXRECURSION 0)


This created a really simple recursive call to make up a simple family tree. Run it as it is and you'll get the complete picture. Start limiting the recursion and it will stop before it gets to all the generations.

If you still have questions feel free to ask.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

shan
Yak Posting Veteran

84 Posts

Posted - 2009-12-29 : 10:52:36
I really appreciate you for the example, I got it. Thanks much

-Shan
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-12-29 : 11:17:31
you're welcome.

It takes a bit of thought getting comfortable with recursive CTE but they are very useful!


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -