Home Forums SQL Server 2008 T-SQL (SS2K8) Recursive Update - Help identifiying Where MAXRECURSION limit is blown RE: Recursive Update - Help identifiying Where MAXRECURSION limit is blown

  • Mark-101232 (11/5/2012)


    You don't need to use recursion to find the ranges of 1753s starts and their lengths. Try this instead

    DECLARE @CutOffDate DATETIME = '2009-01-01';

    WITH CTE AS (

    SELECT ID, VisitDate,

    ROW_NUMBER() OVER(ORDER BY ID) -

    ROW_NUMBER() OVER(PARTITION BY CASE WHEN VisitDate < @CutOffDate THEN 1 ELSE 0 END ORDER BY ID) AS rnDiff

    FROM WebRequest)

    SELECT MIN(ID) AS StartID,

    COUNT(*) AS RunLength

    FROM CTE

    WHERE VisitDate < @CutOffDate

    GROUP BY rnDiff

    ORDER BY StartID;

    Nicely done, Mark.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)