Update record if already exists
Posted on 2009-05-10
I have the following table:
CREATE TABLE Cart
CartID INTEGER IDENTITY(1, 1) PRIMARY KEY CLUSTERED NOT NULL,
ProductFK1 INTEGER REFERENCES Products(ProductID) NOT NULL
I insert into it using the following statement:
INSERT INTO Cart (CustomerID, Quantity, ProductFK1) VALUES ('" & currentUserID & "', 1, " & DetailsView1.DataKey.Value & ")"
Before inserting I want to check first if the record with the same CustomerID and the same ProductFK1 already exists in the table. If it does, I would like to update its quantity by adding 1 to the existing record's quantity but not insert new record.
Is there a way to do it in SQL?