Solved

Update record if already exists

Posted on 2009-05-10
7
470 Views
Last Modified: 2012-05-06
Hi guys,

I have the following table:

CREATE TABLE Cart
(
      CartID INTEGER IDENTITY(1, 1) PRIMARY KEY CLUSTERED NOT NULL,
      CustomerID VARCHAR(255),
      Quantity SMALLINT,
      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?

Thank you.
0
Comment
Question by:RealSnaD
7 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
if you don't want to go that way, you have to do like this:
UPDATE yourtable 
  SET ...
  WHERE ... 
IF @@rowcount = 0
  INSERT INTO yourtable ...

Open in new window

0
 
LVL 12

Expert Comment

by:geowrian
Comment Utility
It looks like you are inserting data using a SQL query generated from code. If so, why not just run a SELECT query to locate the customer by customerID? If a record is found, update the quantity. If not, insert it.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 26

Expert Comment

by:Chris Luttrell
Comment Utility
Are you willing to do this in a stored procedure call where you can or do you want to do multiple sql calls, one to see if it exists and then either do an insert or update?  I would recomend the stored procedure method.
0
 
LVL 12

Expert Comment

by:geowrian
Comment Utility
@CGLuttrell
Seconded. I provided the multiple SQL lookup method as it's more familiar to most web programmers than SQL. The single SQL statement is definitely more efficient and simpler if you understand basic SQL.
0
 
LVL 1

Expert Comment

by:rbodepudi
Comment Utility
Stored proc is the best method to acheive this... you can call it, pass on parameters (keys and values) and you are done.
0
 
LVL 7

Author Comment

by:RealSnaD
Comment Utility
Thanks, angelIII. Here is my working statement:

Dim strSQL As String = "UPDATE Cart SET Quantity+=1 WHERE CustomerID='" & currentUserID & _
                                            "' AND ProductFK1 = " & DetailsView1.DataKey.Value & _
                                                " IF @@rowcount=0 INSERT INTO Cart (CustomerID, Quantity, ProductFK1) VALUES ('" & _
                                                    currentUserID & "', 1, " & DetailsView1.DataKey.Value & ")"
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now