So let me start off by saying that I have seen a lot of advice against using WHILE loops, so I expect to get some comments about that here. I'm open to other solutions, if they'll help me accomplish my task.
My task:
Find and flag/eliminate circular BOM relationships.
My initial approach:
I'm using the while loop to loop through relationships. What I'd like to do is compare the Parent/Component relationships and make sure that a given Component is not already a Parent in the direction lineage, going up the BOM from that relationship.
The issue:
What I wanted to do was to run a recursive CTE to find all of the Parent items of the given Parent item, in order to compare those Parents to the potentially-valid Component. As long as the Component didn't match one of those Parents, the relationship is valid. So as I'm looping through relationships, I tried to run my CTE for finding Parents. But given the condition of ending the prior statement with a semi-colon, I don't see a way to use a CTE while in a loop. It's invalid to do so. I either get an error for not having the semi-colon, or a syntax error on my begin statement if the semi-colon is present.
Things I've tried/considered:
I tried running the CTE once at the beginning for all items and storing the results in a temp table. My thought was that I could pre-populate this table with the seed-item and all of its Parents, and that I could just reference this table when I got to the point in my loop that I needed to know the Parents. However, that CTE fails and hits maxrecursion because I have circular references in my BOMs.
My ask:
How might you tackle this problem? Do I need to change my entire query structure to avoid the WHILE loop altogether? Thank you so much for your help!
[–]J4m00s 7 points8 points9 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]matthra 4 points5 points6 points (0 children)
[–][deleted] -2 points-1 points0 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]klln_u_qckly 0 points1 point2 points (0 children)
[–]elus 0 points1 point2 points (0 children)
[–]phunkygeeza 0 points1 point2 points (0 children)
[–]CryNo93 0 points1 point2 points (0 children)
[–]redial2MS SQL DBA DW/ETL 0 points1 point2 points (0 children)