0

I have a table Cart with ItemID and UserID as primary key, with a quantity column. I want to increase the Quantity by the value of the inserted Quantity when inserted is a duplicate. I know it use INSTEAD OF INSERT in some way but actually writing it is above my abilities.

For example:

Inserted UserID 1, ItemID 1, Quantity 10
Cart     UserID 1, ItemID 1, Quantity 2
Result   UserID 1, ItemID 1, Quantity 12
  • Does this answer your question? [Does SQL Server Offer Anything Like MySQL's ON DUPLICATE KEY UPDATE](https://stackoverflow.com/questions/1197733/does-sql-server-offer-anything-like-mysqls-on-duplicate-key-update) – nbk May 09 '22 at 17:11
  • Why not use an upsert instead? – Thom A May 09 '22 at 17:28
  • 1
    Don't try to mislead your application. If an item can exist within a cart only once, then that should firstly be enforce with the proper constraint. You might want to consider not requiring this - why is it a "problem" if I (as a user) place the same item twice in my cart? If this is a requirement, then it is the user interface that needs to "help" the user. When an application inserts a row into a table, that must either work correctly (and the row exists) or fail. Trying to change an insert into an update of an existing row is a poor design IMO. – SMor May 09 '22 at 19:09

0 Answers0