July 4, 2007 at 5:40 pm
I have Bill of Material table which can have n-levels.
I want to create a view that shows the fully exploded part list for any finished good.
The tables that hold this information are:
BOMMAST
- FINISHED_GOOD_CODE
BOMTRAN
- FINISHED_GOOD_CODE
- BILL_LINE_NUMBER
- STOCK_CODE
- FIXEDVARIABLE
- QUANTITY
My problem is that the ttock code on the BOMTRAN can be a manufactured item and I need the query to drill down further to show the parts of this level.
What is my best approach?
July 5, 2007 at 11:25 am
Look at the topic "expanding hierarchies" on Books on Line. I have adapted this to my particular problem myself and it works well.
HTH
Dave J
July 5, 2007 at 6:22 pm
And, don't use recursion to solve this... SQL Server 2000 cannot go "N" levels... it's limited to only 32 levels and then "BOOOOOOM!"
--Jeff Moden
Change is inevitable... Change for the better is not.
July 5, 2007 at 11:34 pm
Thanks for the advice, now reading through the many "Expanding Hierachies" articles.
Looks a lot more promising.
July 6, 2007 at 12:15 am
The one David was talking about is actually available in "Help" in Query Analyzer... it's also known as "Books OnLine".
--Jeff Moden
Change is inevitable... Change for the better is not.
July 6, 2007 at 2:12 am
Good catch Jeff.
Sorry Shane, I should have said "look *in* Books Online"
Dave J
July 8, 2007 at 4:55 pm
Thanks guys,
I did understand what you meant and found the article and then did some further research on what other comments were available on the subject.
What I have come up with so far is:
ALTER PROCEDURE dbo.SP_CASE_Explode_BOM (@item CHAR(15)) AS
SET NOCOUNT ON
DECLARE @lvl int,
@master_item char(15)
SET @master_item = @item
CREATE TABLE #stack (
item char(15),
lvl int)
CREATE TABLE #stack2 (
master_item char(15),
bill_header_item char(15) ,
bill_item char(15),
bill_line int,
bill_qty numeric(12,4),
fixed_or_variable char(1),
bottom_level char(1))
INSERT INTO #stack (
item,
lvl)
VALUES (
@item,
1)
SELECT
@lvl = 1
WHILE @lvl > 0
BEGIN
IF EXISTS (SELECT * FROM #stack WHERE lvl = @lvl)
BEGIN
SELECT
@item = item
FROM
#stack
WHERE
lvl = @lvl
DELETE FROM
#stack
WHERE
lvl = @lvl AND
item = @item
INSERT
#stack
SELECT
STOCK_CODE,
@lvl + 1
FROM
BOMTRAN
WHERE
BILL_ORDER_CODE = @item
INSERT
#stack2
SELECT
@master_item,
BOMT.BILL_ORDER_CODE,
BOMT.STOCK_CODE,
BOMT.BILL_ORDER_LINE,
BOMT.BILL_ORDER_QTY,
BOMT.FIXED_VARIABLE,
CASE
WHEN EXISTS(SELECT BOMM.BILL_ORDER_CODE FROM BOMMAST BOMM WHERE BOMM.BILL_ORDER_CODE = BOMT.STOCK_CODE AND BOMM.BILL_ORDER = 'B')
THEN 'N'
ELSE 'Y'
END
FROM
BOMTRAN BOMT
WHERE
BOMT.BILL_ORDER_CODE = @item AND
BOMT.BILL_ORDER = 'B'
IF @@ROWCOUNT > 0
BEGIN
SELECT @lvl = @lvl + 1
END
END
ELSE
BEGIN
SELECT @lvl = @lvl - 1
END
END
SELECT
*
FROM
#stack2
WHERE
bottom_level = 'Y'
Can I incorporate the results into a View?
July 9, 2007 at 5:58 pm
No... but you could place the results of the proc in a temp table...
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 8 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply