3

我有一个 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 
4

2 回答 2

0

如果您的临时表不包含标识符和超过 1 的行数,我会在 #TableA 中添加 Id

SELECT * INTO #TableA
FROM
(
 SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS Id,
        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(Id, vno, IID, IName, UName, SrNo, QTY, ReqDetpk, RcvdQty, ReqQty, 
           BalQty, RemainQty, Color, chked, Pk, isupdate, isdeleted, [Level]) AS (
SELECT A.Id, 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, 1 AS [Level]
FROM #TableA A
UNION ALL
SELECT A.Id, 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, n.[Level] + 1
FROM #TableA A JOIN Nbrs n ON A.Id = n.Id
WHERE A.Number > n.[Level]
)
SELECT *
FROM Nbrs
ORDER BY IID, N.Number, pk 
于 2012-11-19T06:13:31.673 回答
0

正如您可能发现的那样,递归 CTE 的递归限制为 99。要解决这个问题,您必须使用一系列 CROSS JOIN,如Itzik Ben-Gan 的 SQL Magazine 文章中所述。

WITH
L0   AS(SELECT 1 AS c UNION ALL SELECT 1),
L1   AS(SELECT 1 AS c FROM L0 AS A CROSS JOIN L0 AS B),
L2   AS(SELECT 1 AS c FROM L1 AS A CROSS JOIN L1 AS B),
L3   AS(SELECT 1 AS c FROM L2 AS A CROSS JOIN L2 AS B),
L4   AS(SELECT 1 AS c FROM L3 AS A CROSS JOIN L3 AS B),
L5   AS(SELECT 1 AS c FROM L4 AS A CROSS JOIN L4 AS B),
Nums AS(SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS n FROM L5)
SELECT n FROM Nums

这为您提供了高达 40 亿的数字。

于 2012-11-19T03:18:58.790 回答