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.