-- ================================================ -- Product Assemblies Recursion Example template -- ================================================ -- Uses a common table expression to selectively step through a recursive -- relationship in a SELECT statement. -- The following example shows the hierarchy of product assemblies and -- components that are required to build the bicycle for ProductAssemblyID = 800. USE ; GO WITH (, , PerAssemblyQty, EndDate, ) AS ( SELECT b.ProductAssemblyID, b.ComponentID, b.PerAssemblyQty, b.EndDate, 0 AS FROM Production.BillOfMaterials AS b WHERE b.ProductAssemblyID = 800 AND b.EndDate IS NULL UNION ALL SELECT bom.ProductAssemblyID, bom.ComponentID, p.PerAssemblyQty, bom.EndDate, + 1 FROM Production.BillOfMaterials AS bom INNER JOIN AS p ON bom.ProductAssemblyID = p. AND bom.EndDate IS NULL ) SELECT , , Name, PerAssemblyQty, EndDate, FROM AS p INNER JOIN Production.Product AS pr ON p. = pr.ProductID ORDER BY , , ; GO