Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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.
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

610 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