Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Towards the innovative SQL ideas
Browse by Tags · View All
ms sql server 119
ms sql 118
sql server 116
sql 115
database 102
tsql 81
#SQL Server 78
t-sql 75
#sql 71
sql server general 67

Archive · View All
April 2011 14
July 2011 12
May 2011 12
August 2011 11
June 2011 10
September 2011 8
December 2011 6
November 2011 6
June 2013 5
April 2013 5

MAXRECURSION option and CTE (Common Table Expression) - SQL Server 2005

Jun 28 2011 12:00AM by Paresh Prajapati   

You know very well about CTE and also I wrote about the MAXRECURSION option with CTE when we have hierarchy depth level grater than 100 and due to that error is raised in previous post. So we need to define MAXRECURSION 0 option there.

But sometime it is also creating issue because of the data and due to that it is going in infinite loop and query execution never end. Means query will found parent and child are same or creating circular chain which never complete the execution.

Here i would like to share my experience which i faced many times and what i have did to resolve it. Let's describe my experience with small example.
IF ( Object_id('CTEMaxLevelTab') > 0 )
  DROP TABLE CTEMaxLevelTab

GO

CREATE TABLE CTEMaxLevelTab
  (
     ChildId   BIGINT IDENTITY(1, 1)
     ,ParentId BIGINT
  )

GO

INSERT INTO CTEMaxLevelTab
            (ParentId)
SELECT TOP 5
  NULL
FROM   sys.sysobjects a
       CROSS JOIN sys.sysobjects b

GO

UPDATE CTEMaxLevelTab
SET  ParentId = ChildId - 1

GO
Let us update one of ParentId and update to same as ChildId.
UPDATE CTEMaxLevelTab
SET  ParentId = 1
where ChildId = 1

SELECT 
* 
FROM CTEMaxLevelTab

Now we use the MAXRECURSION 0 option to get hierarchical data. and see what happen here.
;WITH cteLevels
AS
(
SELECT
  ChildId   AS Child
  ,ParentId AS Parent
  ,1        AS [Level]
FROM   CTEMaxLevelTab 
WHERE CHILDID = 1


UNION ALL

SELECT
  ChildId      AS Child
  ,ParentId    AS Parent
  ,[Level] + 1 AS [Level]
FROM   CTEMaxLevelTab
       INNER JOIN cteLevels
         ON ( ParentId = Child ) 

)


SELECT
  *
FROM   cteLevels
OPTION (MAXRECURSION 0); 


You have an idea hat happened here when we define MAXRECURSION with 0 in the script, 0 means max or unlimited hierarchy depth level.

The resolution of this issue is we should define MAXRECURSION with limied hierarchy level. We have maximum 5 levels in table data, so we can define as MAXRECURSION 5.

At this way it will raise error of cte but never go in infinite loop or not create circular chain. This is the revised script should we use.
;WITH cteLevels
AS
(
SELECT
  ChildId   AS Child
  ,ParentId AS Parent
  ,1        AS [Level]
FROM   CTEMaxLevelTab 
WHERE CHILDID = 1


UNION ALL

SELECT
  ChildId      AS Child
  ,ParentId    AS Parent
  ,[Level] + 1 AS [Level]
FROM   CTEMaxLevelTab
       INNER JOIN cteLevels
         ON ( ParentId = Child ) 

)


SELECT
  *
FROM   cteLevels
OPTION (MAXRECURSION 5); 
The solution is always come out through our experience!

Tags: sql, sql server 2008, sql server 2005, tsql, sql server, ms sql, ms sql server, t-sql, mssql, sql server 2011, database, sql server general, cte, SQL Scripts, sql errors, SQL Server Problems,


Paresh Prajapati
6 · 22% · 7054
4
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

2  Comments  

  • Nice. Thanks for sharing.

    commented on Jul 15 2011 3:18AM
    Hardik Doshi
    20 · 9% · 2839
  • I'd rather try to apply some condition (in this case - WHERE Child <> Parent) to filter out those looping records, if possible. Anyway, leaving MAXRECURSION at 0 is not always the best of ideas.

    commented on Nov 17 2011 1:45AM
    Szymon Wojcik
    67 · 3% · 839

Your Comment


Sign Up or Login to post a comment.

"MAXRECURSION option and CTE (Common Table Expression) - SQL Server 2005" rated 5 out of 5 by 4 readers
MAXRECURSION option and CTE (Common Table Expression) - SQL Server 2005 , 5.0 out of 5 based on 4 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]