我有一个 CTE 查询,但我对此一无所知。我是编程新手。实际上我的查询中有 Qty 列,我想重复行直到 qty。表示如果 qty 等于 50,则数据集返回 50 条时间记录。如果数量等于 100 万,则查询返回 1000 万条记录。以下查询对我来说很好,直到 <99 rcords 我怎样才能成长为 1000 rocords。
提前谢谢。
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