SET NOCOUNT ON
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
DECLARE @ProductId uniqueidentifier
SET @ProductId = '75a42220-ef03-42ec-a8d7-8f212654a646'
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
CREATE TABLE #TempProducts
(
IndexId int IDENTITY (0, 1) NOT NULL,
ProductId uniqueidentifier,
Quantity int,
TotalQuantity int,
Depth int,
RankLine nvarchar(MAX)
)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
DECLARE @Depth int
DECLARE @IndexId int
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET @Depth = 0
SET @IndexId = -1
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
INSERT #TempProducts(ProductId, Quantity, TotalQuantity, Depth, RankLine)
VALUES (@ProductId, 1, 1, @Depth, CAST(@ProductId AS nvarchar(36)))
A:
SET @IndexId = @IndexId + 1
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
IF (@IndexId <= SCOPE_IDENTITY())
BEGIN
SET @Depth = @Depth + 1
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
INSERT #TempProducts (ProductId, Quantity, TotalQuantity, Depth, RankLine)
SELECT c.ChildProductId, c.Quantity, t.TotalQuantity * c.Quantity, @Depth, t.RankLine + STR(c.Rank) + CAST(c.ChildProductId AS nvarchar(36))
FROM dbo.mappor_ChildProducts c INNER JOIN
#TempProducts t ON c.ProductId = t.ProductId AND t.Depth = @Depth - 1
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
GOTO A
END
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SELECT *
FROM #TempProducts
ORDER BY t.RankLine