I´ve been struggling for several days with a problem. I have a couple of tables in an Oracle DB (10g), one that shows a list of orders sorted by priority and the other an inventory of products. What I want to do is assign the available products to the orders based on the priority. My questions are:
- Each order will receive only one product. How can I select only the orders that can receive a product based on the available existence?
- To make the procedure more efficient, is there a way of storing the inventory changes while selecting the products, and apply the update at the end of the procedure?
Both tables will have thousands of items, so I think a PL/SQL stored procedure would be more efficient.
Thanks for your time and help.
PS: This is an example of my tables.
Orders
-------------------------------------------
| ID | Priority | Product
-------------------------------------------
| Order1 | 50 | 1
| Order7 | 48 | 3
| Order3 | 45 | 1
| Order2 | 40 | 1
| Order9 | 30 | 2
| Order4 | 15 | 3
Inventory
-------------------------------------------
| ProductID | Qty
-------------------------------------------
| 1 | 2
| 2 | 4
| 3 | 1
After running the stored procedure I need to get the following result and need to update the inventory.
-------------------------------------------
| ID | Priority | Product
-------------------------------------------
| Order1 | 50 | 1
| Order7 | 48 | 3
| Order3 | 45 | 1
| Order9 | 30 | 2
Updated Inventory
-------------------------------------------
| ProductID | Qty
-------------------------------------------
| 1 | 0
| 2 | 3
| 3 | 0
Yep a procedure would be the best way to handle the transaction.
Open a cursor and populate it with your ordered Order table. Loop through the cursor and update the Inventory table as you fulfill the orders.