Solved

Update record if already exists

Posted on 2009-05-10
7
471 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]
ID: 24347969
0
 
LVL 142

Accepted Solution

by:
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

0
 
LVL 12

Expert Comment

by:geowrian
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.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 26

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.
0
 
LVL 12

Expert Comment

by:geowrian
ID: 24348015
@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
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.
0
 
LVL 7

Author Comment

by:RealSnaD
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 & ")"
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

930 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

15 Experts available now in Live!

Get 1:1 Help Now