LevelPath順に表示したい場合
ItemNo+LevelのItemNoを追加した
ただし、ItemNoが数字属性なので、すべてにおいてCASTしないと
下記のエラーが表示される
Types don't match between the anchor and the recursive part in column "ColumnName" of recursive
query "CTE_Name"
WITH BOM(Lv1, ItemNO, ChildPN, ChildPartName, Quantity,
ParentPN, LevelPath)
AS
(SELECT 1 AS Lv1, ItemNo, ChildPN, PartName, Quantity, ParentPN, CAST('/' + RIGHT('000' + CAST(ItemNo as varchar(256)),3) as varchar(256)) as LevelPath
FROM UP_BOM
INNER JOIN UP_Item I1 ON i1.PartNo = ChildPN
WHERE ParentPN = 'J073-185 '
UNION ALL
SELECT h.Lv1 + 1, d.ItemNo, d.ChildPN, i2.PartName, d.Quantity, d.ParentPN, CAST((RTRIM(h.LevelPath) + '/' + RIGHT('000' + CAST(d.ItemNo as varchar(256)),3)) as varchar(256)) as levelPath
FROM UP_BOM d
INNER JOIN BOM h ON h.ChildPN = d.ParentPN
INNER JOIN UP_Item I2 ON i2.PartNo = d.ChildPN)
SELECT Lv1, ItemNo, ChildPN, ChildPartName, Quantity, ParentPN, LevelPath
FROM BOM
ORDER By LevelPath
0 件のコメント:
コメントを投稿