I have an UPDATE statement I've written that will take a @CHANGE
(positive or negative integer value) and update the CURRENT_INVENTORY
for that type of item.
UPDATE ITEM_INVENTORY
SET CURRENT_INVENTORY = MAX(CURRENT_INVENTORY + @CHANGE, 0),
LAST_UPD_TSTAMP = CURRENT_TIMESTAMP,
LAST_UPD_SOURCE = @SOURCE
WHERE LOCATION = @LOCATION
AND ITEM_TYPE = @ITEM_TYPE;
I'd like to know what CURRENT_INVENTORY
is before and after I run this update. Is there a way to select both the OLD TABLE
and FINAL TABLE
value of CURRENT_INVENTORY
?
I'm wanting to display a message along the lines of "{location} inventory changed from {old} to {new}". I could use a separate SELECT to get the old/current value, do the math in my code, and then UPDATE the row on the table. I believe that'd be susceptible to race conditions though (since the first SELECT won't lock the row) so I'm attempting to do it in a single query.
I don't think I can select both OLD and FINAL so I believe the best solutions I have are either:
- Select value from
OLD TABLE
and duplicate the logic from the CASE statement to derive the new value - Select value from
OLD TABLE
and select the new value (within the same transaction now that my UPDATE has locked the row)
Is there a better/correct way to accomplish this?