Good morning. I’ll do my best to explain my question without posting the SQL (it’s 650 lines). Let me know if more information is needed.
We have an in-house fulfillment system that is allocating inventory in real time. For allocation to work properly, we need to know how much inventory is available each time a user asks what they should be working on (by loading/reloading their task list). The data would look something like this:
ID ItemID QtyOrdered QtyAvailableAfterAllocation ParentID
1 1234 5 500 NULL
2 1234 15 485 1
3 1234 10 475 2
Currently a while loop is being used to set the QtyAvailableAfterAllocation column. The example above demonstrates the need for the loop. Row 2’s QtyAvailableAfterAllocation is dependent on the value of row 1’s QtyAvailableAfterAllocation. Row 3 is dependent on row 2 and so on.
This is the (very) simplified version of the logic. It gets infinitely more complicated when you take into account kits (groups of inventory items that belong to a single parent item). There are times that inventory does not need to be allocated to the item because it exists inside of a kit that has sufficient inventory to fulfill the order. This is why we can’t do a running total. Also, kits could be nested inside of kits to the Nth level. Therein lies the problem. When dealing with a large amount of orders that have nested kits, the performance of the query is very poor. I believe that the loop is to blame (testing has proved this). So, here’s the question:
Is it possible to commit an update, one row at a time and in a specific order (without a loop), so that the child record(s) below can access the updated column (QtyAvailAfterOrder_AllocationScope) in the parent record?
EDIT
Here is a small portion of the SQL. It’s the actual while loop. Maybe this will help show the logic that’s needed to determine the allocation for each record.
Based on the comments/answers above and my inability to accurately represent this complicated issue properly, I’ve rewritten the processing in C#. Using PLINQ, I’ve reduced the processing time from 15 seconds to 4. Thanks to all those who tried to help!
If this isn’t the appropriate way to close a question, let me know (and let me know the appropriate way so I can do that instead).