I have a CTE Query but I have nothing concepts about it. And I am new in programming. Actually i have Qty column in my query and i want repeat rows until qty. means if qty is equal to 50 then data set returns me 50 time records. if qty is equal to 1000 thousand then query returns me 10000 thousand records. Following query works fine for me till <99 rcords how can i grow for 1000 rocords.
Thanx in advance.
SELECT * INTO #TableA
FROM
(
SELECT MIS_Request_Detail.vno, MIS_Request_Detail.IID
, MIS_Request_Detail.Qty-MIS_Request_Detail.BalQty qty,
1 RcvdQty, pk,
ItmMstr.IName, ItmMstr.UName, '' SrNo,
MIS_Request_Detail.Qty As ReqQty, MIS_Request_Detail.BalQty
, MIS_Request_Detail.Qty-MIS_Request_Detail.BalQty RemainQty
FROM MIS_Request_Detail INNER JOIN
view_MIS_Item_Master ItmMstr ON ItmMstr.IID=MIS_Request_Detail.IID
WHERE ReqType=1 AND MIS_Request_Detail.vno=@vno
) T
;WITH Nbrs ( Number ) AS (
SELECT 1 UNION ALL
SELECT 1 + Number FROM Nbrs WHERE Number < 99
)
SELECT A.vno,A.IID, A.IName, A.UName, CAST(SrNo AS VARCHAR(50)) SrNo
, CAST(N.Number AS varchar(50)) QTY, pk ReqDetpk, RcvdQty,
A.ReqQty, A.BalQty, A.RemainQty, 0 Color, 0 chked, 0 Pk, 0 AS isupdate
, '' AS isdeleted
FROM #TableA A
JOIN Nbrs N ON N.Number <= A.qty
ORDER BY IID, N.Number, pk
As you probably discovered, the recursion limit is 99 for recursive CTE’s. To get around this, you have to use a series of CROSS JOINs, as found in the SQL Magazine article by Itzik Ben-Gan.
This gives you numbers all the way up to 4 billion.