I have two temp tables, both populated by TSQL queries.
Temp Table A containing a Location, an Item, and a total inventory count.
Temp Table B containing a Location, an Item, a Sales Order Number, and negative inventory count.
I would like to loop through each item in Table B and subtract the negative inventory count from table A where the location and item match. Once the inventory reaches <=0 in Table B, I want to output each order into a 3rd table which holds the Location, Item, and Sales Order Number.
EXAMPLE TABLE A
Item|Location|Inventory
1 A 10
2 B 20
EXAMPLE TABLE B
Order|Item|Location|QuanityUsed
ABC |1 |A |5
ZYX |2 |B |10
DEF |1 |A |6
DEF would be output into TABLE C because there is not enough inventory to fill the order after subtracting order ABC.
How can I accomplish this?