Update record if already exists

Posted on 2009-05-10
Medium Priority
Last Modified: 2012-05-06
Hi guys,

I have the following table:

      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.
Question by:Yury Merezhkov
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24347969
LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 2000 total points
ID: 24347972
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

LVL 12

Expert Comment

ID: 24347977
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.
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

LVL 27

Expert Comment

by:Chris Luttrell
ID: 24347993
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.
LVL 12

Expert Comment

ID: 24348015
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.

Expert Comment

ID: 24348059
Stored proc is the best method to acheive this... you can call it, pass on parameters (keys and values) and you are done.

Author Comment

by:Yury Merezhkov
ID: 24348344
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 & ")"

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

764 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