I have a WorksOrder table where specific members link to other WorksOrders in the same table.
To do this I have created a linking table that links WorksOrders to another WorksOrder and has the columns WorksOrderAID and WorksOrderBID.
Everything has been working fine so far, however when I need to query a certain works order only the ones with direct links are returned.
This is a problem as they are all supposed to be on the same level and do not follow a parent/child relationship.
If there are WorksOrders 0, 1, 2, 3 and 4 and all are linked directly to 0.
When I query
SELECT WorksOrderBID from LinkedWorksOrders where WorksOrderAID = @WorksOrderID
UNION ALL
SELECT WorksOrderAID from LinkedWorksOrders where WorksOrderBID = @WorksOrderID
And @WorksOrderID is WorksOrder 0, I receive get back all the WorksOrders which is good.
If, however @WorksOrderID is 1 then I only retrieve 0, rather than all of them. This would also come up as a problem if 1, 2, 3 and 4 had links to other works orders that aren't directly linked to 0.
I am having a difficult time working on a stored procedure to return all the related works orders and I am strongly suspecting that the design has gone awry at some point but I just can't find it.