WITH
TREE AS( SELECT a.FItemID cfitemid,b.FItemID pfitemid FROM dbo.ICBOMChild a,dbo.ICBOM b WHERE a.FInterID = b.FInterID and a.FInterID = 1320 -- 要查询的子 id UNION ALLSELECT a.FItemID,b.FItemID FROM ICBOMChild a, dbo.ICBOM b,TREE WHERE TREE.pfitemid = a.FItemID AND a.FInterID = b.FInterID) SELECT cfitemid,pfitemid FROM TREESELECT *FROM ICBOMChild WHERE FItemID = 76431
WITH
TREE AS( SELECT a.FItemID cfitemid,b.FItemID pfitemid FROM dbo.ICBOMChild a,dbo.ICBOM b WHERE a.FInterID = b.FInterID and a.FInterID = 1320 -- 要查询的父 id UNION ALLSELECT a.FItemID,b.FItemID FROM ICBOMChild a, dbo.ICBOM b,TREE WHERE TREE.cfitemid = b.FItemID AND a.FInterID = b.FInterID) SELECT cfitemid,pfitemid FROM TREE