Home Forums SQL Server 2008 T-SQL (SS2K8) Add variable number of rows into a table based on the values in another table (without cursors/while loops) RE: Add variable number of rows into a table based on the values in another table (without cursors/while loops)

  • Megha P (3/22/2013)


    Hello ,

    you can also try below

    ;WITH CTE AS

    (

    SELECT docid,pages FROM #x

    UNION ALL

    SELECT C.docid,C.pages-1

    FROM #x X INNER JOIN CTE C

    ON X.docid = C.docid

    AND C.pages-1 >0

    )

    SELECT * FROM CTE ORDER BY docid,Pages

    Your recursive CTE will be slower and won't scale as well as the straight tally method.