Sql递归查询错误
Sql recursive query error
WITH BomTree (
ITEMNO
,[DESC]
,BOMNO
,BUILDQTY
,UNIT
,COMPONENT
,[Comp Desc]
,[Comp-Qty]
,COMPBOMNO
,Depth
)
AS (
SELECT bomh.ITEMNO
,itm1.DESC]
,bomh.BOMNO
,bomh.BUILDQTY
,bomh.UNIT
,bomd.COMPONENT
,itm.[DESC] [Comp Desc]
,bomd.QTY [Comp-Qty]
,bomd.COMPBOMNO
,0 AS Depth
FROM ICBOMH bomh
INNER JOIN ICBOMD bomd ON bomh.BOMNO = bomd.BOMNO
AND bomh.ITEMNO = bomd.ITEMNO
INNER JOIN ICITEM itm1 ON bomd.ITEMNO = itm1.ITEMNO
INNER JOIN ICITEM itm ON bomd.COMPONENT = itm.ITEMNO
WHERE bomd.BOMNO = '01'
AND bomd.ITEMNO = '300060397'
UNION ALL
SELECT bomh.ITEMNO
,itm1.DESC]
,bomh.BOMNO
,bomh.BUILDQTY
,bomh.UNIT
,bomd.COMPONENT
,itm.[DESC] [Comp Desc]
,(t.[Comp-Qty] * bomd.QTY) AS [Comp-Qty]
,bomd.COMPBOMNO
,t.Depth + 1 AS Depth
FROM ICBOMH bomh
INNER JOIN ICBOMD bomd ON bomh.BOMNO = bomd.BOMNO
AND bomh.ITEMNO = bomd.ITEMNO
INNER JOIN ICITEM itm1 ON bomd.ITEMNO = itm1.ITEMNO
INNER JOIN ICITEM itm ON bomd.COMPONENT = itm.ITEMNO
INNER JOIN BomTree AS t ON bomd.ITEMNO = t.COMPONENT
)
SELECT ITEMNO
,[DESC]
,BOMNO
,BUILDQTY
,UNIT
,COMPONENT
,[Comp Desc]
,[Comp-Qty]
,COMPBOMNO
,Depth
FROM BomTree
GROUP BY ITEMNO
,[DESC]
,BOMNO
,BUILDQTY
,UNIT
,COMPONENT
,[Comp Desc]
,[Comp-Qty]
,COMPBOMNO
,Depth;
error:Types don't match between the anchor and the recursive part in
column "Comp-Qty" of recursive query "BomTree".
首先,这个错误意味着两个联合查询中相同列之间的数据类型不匹配。为了解决这个问题,您可以像这样重写您的查询:
WITH BomTree
AS (
SELECT bomh.ITEMNO
,itm1.[DESC]
,bomh.BOMNO
,bomh.BUILDQTY
,bomh.UNIT
,bomd.COMPONENT
,itm.[DESC] [Comp Desc]
,cast(bomd.QTY as int) [Comp-Qty] --cast as integer
,bomd.COMPBOMNO
,0 AS Depth
FROM ICBOMH bomh
INNER JOIN ICBOMD bomd ON bomh.BOMNO = bomd.BOMNO
AND bomh.ITEMNO = bomd.ITEMNO
INNER JOIN ICITEM itm1 ON bomd.ITEMNO = itm1.ITEMNO
INNER JOIN ICITEM itm ON bomd.COMPONENT = itm.ITEMNO
WHERE bomd.BOMNO = '01'
AND bomd.ITEMNO = '300060397'
UNION ALL
SELECT bomh.ITEMNO
,itm1.DESC]
,bomh.BOMNO
,bomh.BUILDQTY
,bomh.UNIT
,bomd.COMPONENT
,itm.[DESC] [Comp Desc]
,cast((t.[Comp-Qty] * bomd.QTY) as int) AS [Comp-Qty] --cast as integer
,bomd.COMPBOMNO
,t.Depth + 1 AS Depth
FROM ICBOMH bomh
INNER JOIN ICBOMD bomd ON bomh.BOMNO = bomd.BOMNO
AND bomh.ITEMNO = bomd.ITEMNO
INNER JOIN ICITEM itm1 ON bomd.ITEMNO = itm1.ITEMNO
INNER JOIN ICITEM itm ON bomd.COMPONENT = itm.ITEMNO
INNER JOIN BomTree AS t ON bomd.ITEMNO = t.COMPONENT
)
SELECT DISTINCT ITEMNO
,[DESC]
,BOMNO
,BUILDQTY
,UNIT
,COMPONENT
,[Comp Desc]
,[Comp-Qty]
,COMPBOMNO
,Depth
FROM BomTree;
WITH BomTree (
ITEMNO
,[DESC]
,BOMNO
,BUILDQTY
,UNIT
,COMPONENT
,[Comp Desc]
,[Comp-Qty]
,COMPBOMNO
,Depth
)
AS (
SELECT bomh.ITEMNO
,itm1.DESC]
,bomh.BOMNO
,bomh.BUILDQTY
,bomh.UNIT
,bomd.COMPONENT
,itm.[DESC] [Comp Desc]
,bomd.QTY [Comp-Qty]
,bomd.COMPBOMNO
,0 AS Depth
FROM ICBOMH bomh
INNER JOIN ICBOMD bomd ON bomh.BOMNO = bomd.BOMNO
AND bomh.ITEMNO = bomd.ITEMNO
INNER JOIN ICITEM itm1 ON bomd.ITEMNO = itm1.ITEMNO
INNER JOIN ICITEM itm ON bomd.COMPONENT = itm.ITEMNO
WHERE bomd.BOMNO = '01'
AND bomd.ITEMNO = '300060397'
UNION ALL
SELECT bomh.ITEMNO
,itm1.DESC]
,bomh.BOMNO
,bomh.BUILDQTY
,bomh.UNIT
,bomd.COMPONENT
,itm.[DESC] [Comp Desc]
,(t.[Comp-Qty] * bomd.QTY) AS [Comp-Qty]
,bomd.COMPBOMNO
,t.Depth + 1 AS Depth
FROM ICBOMH bomh
INNER JOIN ICBOMD bomd ON bomh.BOMNO = bomd.BOMNO
AND bomh.ITEMNO = bomd.ITEMNO
INNER JOIN ICITEM itm1 ON bomd.ITEMNO = itm1.ITEMNO
INNER JOIN ICITEM itm ON bomd.COMPONENT = itm.ITEMNO
INNER JOIN BomTree AS t ON bomd.ITEMNO = t.COMPONENT
)
SELECT ITEMNO
,[DESC]
,BOMNO
,BUILDQTY
,UNIT
,COMPONENT
,[Comp Desc]
,[Comp-Qty]
,COMPBOMNO
,Depth
FROM BomTree
GROUP BY ITEMNO
,[DESC]
,BOMNO
,BUILDQTY
,UNIT
,COMPONENT
,[Comp Desc]
,[Comp-Qty]
,COMPBOMNO
,Depth;
error:Types don't match between the anchor and the recursive part in column "Comp-Qty" of recursive query "BomTree".
首先,这个错误意味着两个联合查询中相同列之间的数据类型不匹配。为了解决这个问题,您可以像这样重写您的查询:
WITH BomTree
AS (
SELECT bomh.ITEMNO
,itm1.[DESC]
,bomh.BOMNO
,bomh.BUILDQTY
,bomh.UNIT
,bomd.COMPONENT
,itm.[DESC] [Comp Desc]
,cast(bomd.QTY as int) [Comp-Qty] --cast as integer
,bomd.COMPBOMNO
,0 AS Depth
FROM ICBOMH bomh
INNER JOIN ICBOMD bomd ON bomh.BOMNO = bomd.BOMNO
AND bomh.ITEMNO = bomd.ITEMNO
INNER JOIN ICITEM itm1 ON bomd.ITEMNO = itm1.ITEMNO
INNER JOIN ICITEM itm ON bomd.COMPONENT = itm.ITEMNO
WHERE bomd.BOMNO = '01'
AND bomd.ITEMNO = '300060397'
UNION ALL
SELECT bomh.ITEMNO
,itm1.DESC]
,bomh.BOMNO
,bomh.BUILDQTY
,bomh.UNIT
,bomd.COMPONENT
,itm.[DESC] [Comp Desc]
,cast((t.[Comp-Qty] * bomd.QTY) as int) AS [Comp-Qty] --cast as integer
,bomd.COMPBOMNO
,t.Depth + 1 AS Depth
FROM ICBOMH bomh
INNER JOIN ICBOMD bomd ON bomh.BOMNO = bomd.BOMNO
AND bomh.ITEMNO = bomd.ITEMNO
INNER JOIN ICITEM itm1 ON bomd.ITEMNO = itm1.ITEMNO
INNER JOIN ICITEM itm ON bomd.COMPONENT = itm.ITEMNO
INNER JOIN BomTree AS t ON bomd.ITEMNO = t.COMPONENT
)
SELECT DISTINCT ITEMNO
,[DESC]
,BOMNO
,BUILDQTY
,UNIT
,COMPONENT
,[Comp Desc]
,[Comp-Qty]
,COMPBOMNO
,Depth
FROM BomTree;