jeudi 10 mars 2016

SQLite how to get result of conditional column update?

So I'm building a barebones E-commerce site in ASP.NET Core with SQLite for my repository. I keep track of the stock of a product in my store with what I'm calling the 'ProductInventory' table which holds has an integer column called 'Stock'. Now I never want stock to be negative so even though I have checks in software prior to an update query it's still possible that two users could be checking out at roughly the same time resulting two update queries that could result in a negative. So I figure I use a case statement to determine how much to subtract. Here's what I've come up with so far.

UPDATE [ProductInventory] 
SET [Stock] = CASE WHEN [Stock] >= X Then [Stock] - X Else [Stock] = 0
END WHERE [ID] = 1

In the example above X is a placeholder for the amount I wish to deduct from the Stock column.

What I can't figure out is how can I get the information back as to how much was deducted? My guess is I'm using the wrong SQL query here or I have the wrong idea entirely, any help would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire