Update record if already exists

Posted on 2009-05-10
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:RealSnaD
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 500 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.
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
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. …
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL ( and MongoDB (…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

691 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