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
SSCommitted
Points: 1976
More actions
March 22, 2013 at 7:39 am
#1599772
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