Quantcast
Viewing all articles
Browse latest Browse all 4871

MS SQL Recursive Bill of Material query that can switch to a different table

My ERP system has a Product Structure table for standard bills of material and another table for customized items in a custom bill of material. The tables are StandardBoM and CustomBoM

I built a separage recursive query for each product structure that goes like this (StandardBoM ABC-123 as example):

WITH CTE
AS
(SELECT
1 AS iLevel,
Item,
Position,
Component,
Qty,
CustOrStandard,
CAST(Item + '-' + RIGHT('0000' + CAST(Position AS varchar(4)), 4) AS varchar(100)) AS Sorter

FROM dbo.StandardBoM
WHERE (Item = 'ABC-123')

UNION ALL

SELECT

Parent.iLevel + 1 AS Expr1,

Component.Item,
Component.Position,
Component.Component,
Component.Qty,
CustOrStandard,
CAST(Parent.Sorter + '-' + RIGHT('0000' + CAST(Component.t_pono AS varchar(4)), 4) AS varchar(100)) AS Expr2

FROM dbo.StandardBoM AS Component

INNER JOIN CTE AS Parent ON Parent.Item...


Viewing all articles
Browse latest Browse all 4871


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>