22,295
社区成员
发帖
与我相关
我的任务
分享
WITH CTE(CODE, NAME, PCODE, PNAME, QTY, RIndex, SEQ)
AS
(
SELECT Distinct CODE, NAME, PCODE, PNAME, CONVERT(FLOAT, PLMRELQTY), RowIndex, PLMSEQ
FROM bomleafTempTable
WHERE CODE = '34C04-1-3-(3)'
--WHERE CODE IN (SELECT CODE FROM bomleafTempTable)
UNION ALL
SELECT P.CODE, P.NAME, P.PCODE, P.PNAME, CONVERT(FLOAT, P.PLMRELQTY), P.RowIndex, P.PLMSEQ
FROM bomTempTable AS P
INNER JOIN CTE AS C ON C.PCODE = P.CODE
WHERE LEN(P.PCODE) > 0
)
SELECT * FROM CTE
--由父项递归下级
WITH CTE(CODE, PCODE, NAME, QTY)
AS
(
-- 父项
SELECT CODE, PCODE, NAME, CONVERT(FLOAT, PLMRELQTY)
FROM bomTempTable
--WHERE len(ltrim(rtrim(PCODE))) = 0
WHERE CODE = '34C01-19-0'
UNION ALL
--递归结果集中的下级
SELECT C.CODE, C.PCODE, C.NAME, CONVERT(FLOAT, C.PLMRELQTY)
FROM bomTempTable AS C
INNER JOIN CTE AS P ON C.PCODE = P.CODE
)
SELECT * FROM CTE